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

Not seeing error events on a browser build #553

Open
jdub opened this issue Feb 22, 2015 · 7 comments
Open

Not seeing error events on a browser build #553

jdub opened this issue Feb 22, 2015 · 7 comments

Comments

@jdub
Copy link

jdub commented Feb 22, 2015

Here's what I'm doing:

var rpc = superagent
  .post('http://localhost/pants')
  .set('X-Bananas', 'B.A.N.A.N.A.S.')
  .on('error', function(error) {
    console.log({when: 'event', error: error});
  });

Then deep in some other code:

    rpc
      .send({method:"bananas"}) // this is 409-ing
      .end(function(res) { // callback function expects res only
        console.log({when: 'end', res: res});
      });

The error event is never raised, so I only see a log from the .end() callback.

Am I doing something wrong? Is this a browser feature deficiency?

@defunctzombie
Copy link
Contributor

I am not sure the error event is a thing on the browser build tho we certainly could add it. Sounds like it could be a bug for the error event not to occur.

Also, I would strongly recommend that your end callback use function(err, res) since future versions will have the arity magic removed.

@jdub
Copy link
Author

jdub commented Feb 22, 2015

OK, in that case I'll do without for the time being. Thanks! 😃

@jdub
Copy link
Author

jdub commented Feb 22, 2015

Oh! Perhaps this is why… in this case, no error is passed to the end callback. It's null. That's a pretty good reason to not raise an error event, really.

Is it because the status code is 409? Thus recognised as a client error, not a server error? I wasn't expecting only certain categories of errors to be emitted. 😇

Select bits from the Response object:

{
  clientError: true,
  error: {
    message: "cannot POST http://localhost/bananas (409)",
  },
  serverError: false,
  status: 409,
  text: "this server says you are bananas",
  type: "text/html"
}

@defunctzombie
Copy link
Contributor

This will be changed in the next version. Any non success code will be an
error. I too agree that the current behavior is confusing.

On Saturday, February 21, 2015, Jeff Waugh [email protected] wrote:

Oh! Perhaps this is why… in this case, no error is passed to the end
callback. It's null. That's a pretty good reason to not raise an error
event, really.

Is it because the status code is 409? Thus recognised as a client error,
not a server error? I wasn't expecting only certain categories of errors to
be emitted. [image: 😇]

Select bits from the Response object:

{
clientError: true,
error: {
message: "cannot POST http://localhost/bananas (409)",
},
serverError: false,
status: 409,
text: "this server says you are bananas",
type: "text/html"
}


Reply to this email directly or view it on GitHub
#553 (comment)
.

@jdub
Copy link
Author

jdub commented Feb 23, 2015

By the way, I'm still not seeing events in the browser, be it response, error (for server errors), etc. I would still regard that as a bug. Prefer a new issue opened?

@defunctzombie defunctzombie reopened this Feb 23, 2015
@amishshah
Copy link

This is still happening for me... Any updates?

@miklschmidt
Copy link

Just ran into the same problem, i'd be happy to send a pull request? The server version does a bunch of stuff in the Request.prototype.callback method that you'd expect would happen in the client aswell. The 'error' event being one of them.

The following line would fix the problem in the client:

if (err && this.listeners('error').length > 0) this.emit('error', err);

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

No branches or pull requests

4 participants