[ 8 posts ]

Inactivist

  • Username: Inactivist
  • Joined: Tue Apr 03, 2012 8:40 pm
  • Posts: 12
  • GitHub: inactivist
  • Gists: inactivist
  • Offline
  • Profile

Autocomplete: Handle 401 errors with JSON source?

Post Posted: Tue Apr 03, 2012 8:52 pm
+0-
I'm using the YUI3 Autocomplete widget with a URL source returning JSON data.

I'm looking for a simple way to detect HTTP error responses, such as when the user is not authenticated. (401 response from server.)

Example:
Code:
    Y.one('#ac-input').plug(Y.Plugin.AutoComplete, {
        resultHighlighter : 'phraseMatch',
        source : "http://example.com/api/item/search/?q={query}",
    });

If the user hasn't authenticated on the server, the Autocomplete widget receives a 401 response and fails silently but logs the error to the browser's JavaScript console.

I'd like to detect any error response so I can show an appropriate status or otherwise handle the error.

I've searched but haven't found documentation specific to this issue - source diving didn't help, either. I *think* I'll have to roll my own datasource object so I can issue the request and detect the error. If that's the case, is there an online example for this method?

Or is there a simpler way to pass an error event handler for this type of data source?

Dav Glass

  • Username: davglass
  • Joined: Thu Aug 28, 2008 9:28 am
  • Posts: 2088
  • Location: Marion, IL, US
  • Twitter: davglass
  • GitHub: davglass
  • Gists: davglass
  • IRC: davglass
  • Offline
  • Profile

Re: Autocomplete: Handle 401 errors with JSON source?

Post Posted: Wed Apr 04, 2012 4:36 am
+0-
I'm not an expert in AutoComplete, but you "should" be able to add a "resultListLocator" to check the results as they come in and verify that the user needs to be warned.

http://yuilibrary.com/yui/docs/autocomp ... tocomplete

Inactivist

  • Username: Inactivist
  • Joined: Tue Apr 03, 2012 8:40 pm
  • Posts: 12
  • GitHub: inactivist
  • Gists: inactivist
  • Offline
  • Profile

Re: Autocomplete: Handle 401 errors with JSON source?

Post Posted: Wed Apr 04, 2012 10:01 am
+0-
@davglass: Thanks, I'll check out that suggestion.

Inactivist

  • Username: Inactivist
  • Joined: Tue Apr 03, 2012 8:40 pm
  • Posts: 12
  • GitHub: inactivist
  • Gists: inactivist
  • Offline
  • Profile

Re: Autocomplete: Handle 401 errors with JSON source?

Post Posted: Wed Apr 04, 2012 10:34 am
+0-
@davglass: I tried the callable resultListLocator suggestion, but when a 401 error is returned by the server, the resultListLocator function is never invoked (I verified that it is called when the user is authenticated.)

Here's my test resultListLocator:
Code:
function myLocator(response) {
    console.log(arguments);
    return response.objects;
};

This message appears in the Chrome console log:
Code:
GET http://example.com/api/item/search/?q=test 401 (UNAUTHORIZED)


Along with a bunch of YUI3 diagnostic traces (file/stack info).

I'm using YUI 3.4.1.

Inactivist

  • Username: Inactivist
  • Joined: Tue Apr 03, 2012 8:40 pm
  • Posts: 12
  • GitHub: inactivist
  • Gists: inactivist
  • Offline
  • Profile

Re: Autocomplete: Handle 401 errors with JSON source?

Post Posted: Wed Apr 04, 2012 10:37 am
+0-
I'm hoping it's a simple as supplying an
Code:
on: { failure: myHandler }
parameter to the Autocomplete widget. But that seems not to be the case.

Inactivist

  • Username: Inactivist
  • Joined: Tue Apr 03, 2012 8:40 pm
  • Posts: 12
  • GitHub: inactivist
  • Gists: inactivist
  • Offline
  • Profile

Re: Autocomplete: Handle 401 errors with JSON source?

Post Posted: Fri Apr 06, 2012 6:32 am
+0-
Someone provided a working solution on StackOverflow:

Code:
var ds = new Y.DataSource.IO({
  source: 'http://example.com/api/item/search/'
});

ds.set('ioConfig', {
    on: {
        failure: function (transactionID, ioResponseObj, args) {
            ...
        }
    }
};

Y.one('#ac-input').plug(Y.Plugin.AutoComplete, {
  requestTemplate: '?q={query}',
  source: ds
});

http://stackoverflow.com/a/10023472/1309774

Inactivist

  • Username: Inactivist
  • Joined: Tue Apr 03, 2012 8:40 pm
  • Posts: 12
  • GitHub: inactivist
  • Gists: inactivist
  • Offline
  • Profile

Re: Autocomplete: Handle 401 errors with JSON source?

Post Posted: Tue May 01, 2012 8:40 pm
+0-
Another way to do this without resorting to the use of the IO object adapter: use a global IO failure event handler.

While I would rather handle the error locally in the AutoComplete widget handlers, alas, it's not that simple. Using Y.on('io:failure') event handling may be a much more direct approach if all you need to do is display info about comms errors.

Code:
   //
   // Handle IO failure event
   function ioFailureFunction(transaction, xhrObject) {
      console.log("IO:FAILURE", arguments);
      errCode = xhrObject.status;
                // Insert the current error status into an HTML element...
      Y.one('#error').setContent(errCode);
   };
   Y.on('io:failure', ioFailureFunction);

Inactivist

  • Username: Inactivist
  • Joined: Tue Apr 03, 2012 8:40 pm
  • Posts: 12
  • GitHub: inactivist
  • Gists: inactivist
  • Offline
  • Profile

Re: Autocomplete: Handle 401 errors with JSON source?

Post Posted: Tue May 01, 2012 8:51 pm
+0-
I'm still trying to figure out why I can't attach a local handler to the AutoComplete widget's 'on' map. It should be just that simple, shouldn't it?
  [ 8 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum