Can I get the error type inside error.tsx?
Unanswered
Woodwasp posted this in #help-forum
WoodwaspOP
Let's say I have a custom error:
And I want to show a different message if the error is from that type. Is there a way to do it inside the
I saw that we cannot do
Is there a workaround for this?
throw new NotAuthorizedError();And I want to show a different message if the error is from that type. Is there a way to do it inside the
error.tsx?I saw that we cannot do
error instanceof NotAuthorizedError .Is there a workaround for this?
8 Replies
create a route to /error accepting a searchParam
then instead of throw, use
then instead of throw, use
redirect(`/error?msg${errormesssage}`)
//or
router.push(`/error?msg${errormessage}`)better yet just create /unauthorized in your case
WoodwaspOP
It is not possible then?
Ideally I would have a lot of functions in a lot of places that would throw this error (or other errors) and I would use the error boundary to handle it properly without changing the url.
Ideally I would have a lot of functions in a lot of places that would throw this error (or other errors) and I would use the error boundary to handle it properly without changing the url.
btw... thanks for the fast reply
I tried this:
It seems that nextjs is always wrapping errors to be a regular Error and it looses it's own type. However when I console log the error in the catch I can still see traces of CustomError, I have no idea how to get/parse it though...
export class CustomError extends Error {
constructor(message: string | undefined) {
super(message);
this.name = 'CustomError'
}
}It seems that nextjs is always wrapping errors to be a regular Error and it looses it's own type. However when I console log the error in the catch I can still see traces of CustomError, I have no idea how to get/parse it though...
@5euro I tried this:
export class CustomError extends Error {
constructor(message: string | undefined) {
super(message);
this.name = 'CustomError'
}
}
It seems that nextjs is always wrapping errors to be a regular Error and it looses it's own type. However when I console log the error in the catch I can still see traces of CustomError, I have no idea how to get/parse it though...
WoodwaspOP
Yes! this is the same conclusion I got.
I was actually using the message in the error to figure out the type of the error, but then I discovered that in production the error message is also masked to avoid accidental leaks.
I really wish there was a way to do that.. but maybe it's just not possible.
I was actually using the message in the error to figure out the type of the error, but then I discovered that in production the error message is also masked to avoid accidental leaks.
I really wish there was a way to do that.. but maybe it's just not possible.
You could alwasy (ab)use the error mesage 😅