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

Use error handler #19

Merged
merged 4 commits into from
Nov 11, 2016
Merged

Conversation

mjsalinger
Copy link
Contributor

Add a new option, useErrorHandler which will invoke next(err) if set to true on any errors. If not set or set to false, the library will continue to send the response.

@etodanik
Copy link

Is there any reason this isn't merged? Any way to help?

@etodanik
Copy link

On second thought, after testing this it doesn't work at all.
On this line:
if (this.useErrorHandler === true) {

this is the actual global node environment? How does that make sense?

Also, this:

    if (e instanceof UnauthorizedRequestError) {
      return res.status(e.code);
    }

Just leaves the request hanging without resolving it ever at all.

@visvk
Copy link

visvk commented Aug 22, 2016

This code binds this to the handleError function.

ExpressOAuthServer.prototype.token = function(options) {
  return (function(req, res, next) {
    var request = new Request(req);
    var response = new Response(res);

    return Promise.bind(this)
      .then(function() {
        return this.server.token(request, response, options);
      })
      .tap(function(token) {
        res.locals.oauth = { token: token };
      })
      .then(function() {
        return handleResponse(req, res, response);
      })
      .catch(function(e) {
        return handleError.call(this, e, req, res, response, next);
      })
      .finally(next);
  }).bind(this);
};

Copy link
Member

@maxtruxa maxtruxa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See review comments.


res.send({ error: e.name, error_description: e.message });
res.status(e.code);
if (e instanceof UnauthorizedRequestError) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return res.status(e.code); leaves the response hanging.

How about:

res.status(e.code);

if (e instanceof UnauthorizedRequestError) {
  res.send();
} else {
  res.send({ error: e.name, error_description: e.message });
}

if (e instanceof UnauthorizedRequestError) {
return res.status(e.code);
}
if (this.useErrorHandler === true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Down here, this === global.

We need to either pass useErrorHandler as an argument or invoke handleError (and to be consistent handleResponse) using handleError.call(this, ...).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, updating to use handlerError.call(this, ...) and handleResponse.call(this, ...)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not enough. It's nested function, so there will not be this === ExpressOAuthServer value until you bind this to the nested function.
I recommend to use strict mode, so you will see this problem clearly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I just realized what you're talking about. The problem is that this is already wrong by the time handleError.call(this, ...) is called. Promise.bind(this) is supposed to help with that, but this refers to the returned middleware.

This can be solved by changing

var server = this.server;

to

var that = this;

and

return Promise.bind(this)

to

return Promise.bind(that)

in all 3 handlers.

Obviously references to server have to be replaced by this.server after this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I see it.. Pushing changes...

Copy link
Member

@maxtruxa maxtruxa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Michael Salinger added 2 commits November 11, 2016 06:17
* Bind ExpressOAuthServer context to handleResponse and handleError methods
* Fixed bug where particular error never sent
Copy link
Member

@maxtruxa maxtruxa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See review comments.

return handleError(e, req, res);
});
return handleError.call(this, e, req, res, null, next);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's two spaces missing.

@mjsalinger mjsalinger merged commit 4b701d2 into oauthjs:master Nov 11, 2016
@mjsalinger mjsalinger deleted the use-error-handler branch November 11, 2016 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants