Hi, how can i handle supertokens error? I have a m...
# support-questions-legacy
a
Hi, how can i handle supertokens error? I have a middleware that needs to detect an error from supertokens and get the status and message fields.
r
Hey which SDK and framework are you using?
a
supertokens-node v10.0.1 @middy/core v3.1.0 aws lambda
r
right. So you can do something like this:
Copy code
ts
import { Error as STError } from "supertokens-node"

let err: any;
if (STError.isErrorFromSuperTokens(err)) {
    let {fromRecipe, message, payload, type} = err;
}
a
ok thanks
How can I detect when an error has occurred? Because some responses like INCORRECT_USER_INPUT_CODE_ERROR are returned as status 200
Can I just check if response.status is different from "OK"?,
r
yea. You can. But those are not really generic errors. For those, you want to handle them by informing the user about what went wrong.
a
Yes but I would like to modify the response body on error cases
Is it safe to check if the status is other than "OK"?
r
Depends on how you want to modify the response body. Im not really sure what you are trying to do here.
could you maybe give an example of what you want the API to return in case of OK vs non OK response?
a
Copy code
.after((request) => {
    const response = request.response as APIGatewayProxyResult;
    const body = JSON.parse(response.body);

    if (body.status !== 'OK') {
      throw new SuperTokensError(body);
    }
  })
  .onError((request) => {
    log('ERROR:: authentication::', request.error);
    const handledError = handleError(request.error);
    request.response = errorResponse(handledError.statusCode, handledError);
  });
Basically I'm converting the non ok response from Supertokens to a custom error class. So I can add new fields in the response and change the status code
This implementation is working, I would just like to know if all successful responses will have "OK" status
r
I see. Yea. All successful will have OK result
OK status I mean
Other than session refreshing. I think that returns an empty JSON back.
So you want to throw if response.status !== undefined and !== OK.
a
OK thank you!