Error Handling Overview
All our functions will throw one of these four types of errors:
To detect if the error thrown from supertokens-node lib, use the function
isErrorFromAuth
. If the error is thrown by supertokens-node library, it will always be a supertokens.ERROR object.
The above are all enums
and their number value, as seen on the console, are:
GENERAL_ERROR
:1000
UNAUTHORISED
:2000
TRY_REFRESH_TOKEN
:3000
TOKEN_THEFT_DETECTED
:4000
Example
const supertokens = require("supertokens-node");
app.post("/like-comment", function (req, res) {
supertokens.getSession(req, res, true).then(session => {
// ...
}).catch(err => {
if (supertokens.Error.isErrorFromAuth(err)) { // we check if this error was generated from the SuperTokens lib
if (err.errType === supertokens.Error.GENERAL_ERROR) {
res.status(500).send("Something went wrong");
} else if (err.errType === supertokens.Error.UNAUTHORISED) {
// all cookies have been cleared
res.status(440).send("Session expired! Please login again");
} else { // TRY_REFRESH_TOKEN
// cookies are not cleared since we only need to refresh the session
res.status(440).send("Please call refresh token endpoint");
}
} else {
res.status(500).send(err); // Something went wrong.
}
});
});
import * as supertokens from 'supertokens-node';
import { Request, Response } from "express";
app.post("/like-comment", function (req: Request, res: Response) {
supertokens.getSession(req, res, true).then(session: supertokens.Session => {
// ...
}).catch(err: any => {
if (supertokens.Error.isErrorFromAuth(err)) { // we check if this error was generated from the SuperTokens lib
if (err.errType === supertokens.Error.GENERAL_ERROR) {
res.status(500).send("Something went wrong");
} else if (err.errType === supertokens.Error.UNAUTHORISED) {
// all cookies have been cleared
res.status(440).send("Session expired! Please login again");
} else { // TRY_REFRESH_TOKEN
// cookies are not cleared since we only need to refresh the session
res.status(440).send("Please call refresh token endpoint");
}
} else {
res.status(500).send(err); // Something went wrong.
}
});
});