fastify / fastify-bearer-auth

A Fastify plugin to require bearer Authorization headers
Other
153 stars 29 forks source link

Please exports your errors type #182

Open FrancoRATOVOSON opened 4 months ago

FrancoRATOVOSON commented 4 months ago

Prerequisites

🚀 Feature Proposal

I have my custom way to handle errors depending on their type. I know that by using verifyBearerAuth there are 2 types of errors that can be thrown and I have a custom way to handle booth cases separately. But now I cannot check if the error is an FST_BEARER_AUTH_INVALID_AUTHORIZATION_HEADER or an FST_BEARER_AUTH_MISSING_AUTHORIZATION_HEADER because none of them is exported

Motivation

Handling errors a custom way.

Example

if(error instanceof FST_BEARER_AUTH_MISSING_AUTHORIZATION_HEADER) {
  // handle missing authorization header
} else if (error instance of FST_BEARER_AUTH_INVALID_AUTHORIZATION_HEADER) {
  // handle invalid token
} else {
 // Probably a 500 error or any you have defined
}
jsumners commented 4 months ago

Do not rely on the instanceof operator https://github.com/fastify/fastify-error/pull/86#issuecomment-1301466407

FrancoRATOVOSON commented 4 months ago

Do not rely on the instanceof operator fastify/fastify-error#86 (comment)

https://github.com/fastify/fastify-error/pull/86#issuecomment-1301339441 is supposed to be the best solution

If somebody wants all Fastify core errors, they can do err.code?.indexOf('FST_') === 0.

But that doesn't really help with errorResponse especialy with typescript when the type of the error is Error and property code does not exist on this type