Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing a global error handler/documenting pitfall of {ajax: {error}} as an option #28

Open
adstage-david opened this issue Sep 3, 2014 · 1 comment

Comments

@adstage-david
Copy link
Contributor

I've been trying to find the reason I can't properly handle an AJAX error using hyperagent and traced it back to a global configuration setting somebody placed in our project to catch 401s and redirect.

Seems that the configuration option below breaks the promise that Hyperagent sets up, so I'll need to reconfigure that, but it might be nice to allow defining a global error handler that can resolve alongside or bubble up the reject?

If nothing else, might be good to just document this in the README as a potential pitfall - don't set ajax.error in the hyperagent config.

var Resource = require('hyperagent').Resource;
var api = new Resource({
  url: 'https://api.example.com/',
  ajax: {
    error: function(xhr, status, error){ 
      if(status == "error" && xhr.status == 401){ window.location = '/login' }
    }
  }
});

@adstage-david
Copy link
Contributor Author

In my case, I'm just going to change to using the statusCode: {} config option jquery provides, since that's more appropriate anyway. I'm not even sure how you'd want a global error handler option to act, so I think the better resolution here is just a warning in the README that it is a bad idea and will break the way hyperagent resolves ajax promises.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant