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

Can no longer check if an error is instance of AccessTokenError #535

Closed
iliketomatoes opened this issue Nov 10, 2021 · 1 comment · Fixed by #543
Closed

Can no longer check if an error is instance of AccessTokenError #535

iliketomatoes opened this issue Nov 10, 2021 · 1 comment · Fixed by #543
Labels
bug Something isn't working

Comments

@iliketomatoes
Copy link

iliketomatoes commented Nov 10, 2021

Description

Using the instanceof operator against the AccessTokenError constructor always returns false, even if the object that is being tested is actually an instance of AccessTokenError.

This is an issue introduced by nextjs-auth0 v1.5.0.

Reproduction

With nextjs-auth0 v1.4.2 I'm able to check if an error occurred inside a NextJS API handler is an instance of AccessTokenError by doing so:

import { getAccessToken } from '@auth0/nextjs-auth0';
import { AccessTokenError } from '@auth0/nextjs-auth0/dist/utils/errors';
import { ApiError } from 'src/lib/apiClient';

try {
	const tokenInfo = await getAccessToken(req, res);

        // ...

} catch (err: any) {

         // Re-throw an API error in a more convenient format
	throw new ApiError({

                 // Decide what status to return depending on the type of exception that was thrown
		status: err instanceof AccessTokenError ? 401 : 500,
		message: `Code: ${err?.code}, Message: ${err?.message}.`,
	});
}

With nextjs-auth0 v1.5.0, I always get a 500 error because errors thrown by getAccessToken are always an instance of Error, instead AccessTokenError.

At least, this is the reason why our unit test on that API endpoint handler is currently failing.

Environment

  • Library version: v1.5.0
  • Platform: macOS Big Sur
  • Languages: Node v14.15.4, TS 4.4.4 (target ES6)
  • Other modules/plugins/libraries that might be involved: Jest 26.6.3
@adamjmcgrath
Copy link
Contributor

Thanks for raising this @iliketomatoes

Looks like this was caused by microsoft/TypeScript#13965 when we downgraded the compilation target to es5 to support IE11 in c248efe#diff-3ae20d611c1c7263a9c1bce0449291ebfea1c5d578b3fcdbb596353ad885db25R19

There's a workaround here https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work so I'll take a look at fixing it when I can

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants