mikenicholson/passport-jwt

Throw a named error instead of a generic error, when no auth token is provided

abdatta opened this issue · 1 comments

Hi, I have a requirement where I want to catch only the No auth token errors on authentication failure.
Right now I have to check exactly the message property like this: err.message === 'No auth token'.
However, I'm afraid any future change in the error message might break my app.

return self.fail(new Error("No auth token"));

So I request to define an error class for it like this:

class NoAuthTokenError extends Error {
    message = 'No auth token';
}

and then throw that instead

return self.fail(new NoAuthTokenError()); 

This helps me to check the error using: err instanceof NoAuthTokenError, and any future change to the error message won't cause any breaking change.

Lemme know if this sounds reasonable. I'll create a PR for the same then.

This is a good point, either error classes (wich is against the offical passport documentation) or error messages should be put in an enum. Please add a PR (if you can directly to the rewrite)