Skip to main content

Custom error response using GENERAL_ERROR status

Sometimes, you may want to send a custom error message from your API override to display to the user on the frontend. This can be done by sending the following JSON response from the API:

{
"status": "GENERAL_ERROR",
"message": "Some custom error message"
}

If you are using our pre-built ReactJS UI, the above response will render the mesage "Some custom error message" on the frontend UI. For custom UI, you can read this response and display the message in an error UI. This response can be returned from most of the APIs exposed by the backend SDK.

Let's take an example in which we want to prevent the user from signing up unless their email is preapproved by the app's admin. For this, we will override the sign up API to check if the input email is approved or not, and if not, we prevent the sign up, and send a custom error message.

import ThirdParty from "supertokens-node/recipe/thirdparty";

ThirdParty.init({
signInAndUpFeature: {
providers: [/* ... */]
},
override: {
functions: (oI) => {
return {
...oI,
signInUp: async function (input) {
if (emailNotAllowed(input.email)) {
// this error will signal to the API to send a GENERAL_ERROR message
throw new Error("Email not allowed");
}
return oI.signInUp(input);
}
}
},
apis: (oI) => {
return {
...oI,
signInUpPOST: async function (input) {
try {
return oI.signInUpPOST!(input);
} catch (err: any) {
if (err.message === "Email not allowed") {
return {
status: "GENERAL_ERROR",
message: "You are not allowed to sign up. Please contact the app's admin to get permission"
}
}
throw err;
}
}
}
}
}
})

function emailNotAllowed(email: string) {
// TODO: your impl to check if email is allowed or not
return true;
}
Which frontend SDK do you use?
supertokens-web-js / mobile
supertokens-auth-react