Throw new Error()
Answered
Yves posted this in #help-forum
YvesOP
Is it possible to throw a custom error so that it reaches the error.js file?
I need to include some keys like 'operationId' 'message' and 'errorName', the problem is that if I do "throw new Error("Internal Error")" it won't reach the error.js file
I need to include some keys like 'operationId' 'message' and 'errorName', the problem is that if I do "throw new Error("Internal Error")" it won't reach the error.js file
Answered by LuisLl
it seems like your code isn't even reaching the error throwing logic, that's why you never reach the
Instead of relying in the AxiosError
error.jsx
boundary. The code inside the if
statement never gets executed. Instead of relying in the AxiosError
e
instance, if you know you want to fallback to a 500 error (HttpStatusCodes.InternalServerError
) then let it fallback to it when e.status
is undefined.const treatedError: ActionError = {
operationId: e.response?.data.operationId || "unknown",
httpStatus: e.status! ?? HttpStatusCodes.InternalServerError, // <= Try this here
isSuccess: false,
isFailure: true,
error: {
code: e.response?.data?.error?.code || "UNKNOWN_ERROR",
message: e.response?.data?.error?.message || DEFAULT_SERVER_ERROR_MESSAGE
}
};
9 Replies
YvesOP
When I tried throw an error if he status is greater than 499, he doens't reach at error.js, how can I resolve this?
YvesOP
You seem to only be throwing the error if the error code is greater than 499 (
treatedError.httpStatus > 499
) but httpStatus
is actually undefined, so you never enter the conditional.it seems like your code isn't even reaching the error throwing logic, that's why you never reach the
Instead of relying in the AxiosError
error.jsx
boundary. The code inside the if
statement never gets executed. Instead of relying in the AxiosError
e
instance, if you know you want to fallback to a 500 error (HttpStatusCodes.InternalServerError
) then let it fallback to it when e.status
is undefined.const treatedError: ActionError = {
operationId: e.response?.data.operationId || "unknown",
httpStatus: e.status! ?? HttpStatusCodes.InternalServerError, // <= Try this here
isSuccess: false,
isFailure: true,
error: {
code: e.response?.data?.error?.code || "UNKNOWN_ERROR",
message: e.response?.data?.error?.message || DEFAULT_SERVER_ERROR_MESSAGE
}
};
Answer
@Yves its work!
Glad to hear! You’re welcome;)