jonasroussel / dart_jsonwebtoken

A dart implementation of the famous javascript library 'jsonwebtoken'
MIT License
87 stars 29 forks source link

Unable to capture JWTExpiredError exception #9

Closed graphicbeacon closed 3 years ago

graphicbeacon commented 3 years ago

Hello Jonas,

First of all, thanks for putting together this package. I've been using this in a YouTube series at the moment.

I've got an issue related to the JWT.verify() static method in /lib/src/jwt.dart. A generic error object is thrown on line 135:

throw JWTInvalidError('invalid token');

instead of the specific error types like JWTExpiredError on line 66 and JWTNotActiveError() on line 76. Ideally line 135 should throw the specific error object:

throw ex;

What do you think?

jonasroussel commented 3 years ago

Hello !

Thanks for your support and good luck for your Youtube channel !

I already had a PR for this kind of feature (https://github.com/jonasroussel/jsonwebtoken/pull/2)

For now, to rethrow exceptions you can specify the throwUndefinedErrors to true in the verify method.

This is not the default behaviour because I like to have normalized errors.

But I thought about a specific exception : JWTUndefinedError that will contains the real error:

try {
  ...
} catch (ex) {
  throw JWTUndefinedError(ex);
}

Like that, you can catch this specific error:

try {
  let token = jwt.verify(...);
} on JWTUndefinedError (ex) {
  ...
}
jonasroussel commented 3 years ago

https://pub.dev/packages/dart_jsonwebtoken/versions/2.1.0