Skip to content

Commit b3bbbfc

Browse files
authored
refactor(user): add config to toggle setting supertokens error handler as global error handler (#1007)
1 parent 9557382 commit b3bbbfc

4 files changed

Lines changed: 25 additions & 15 deletions

File tree

packages/user/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export { default as hasUserPermission } from "./lib/hasUserPermission";
5858
export { default as ProfileValidationClaim } from "./supertokens/utils/profileValidationClaim";
5959
export { default as createUserContext } from "./supertokens/utils/createUserContext";
6060
export { default as userSchema } from "./graphql/schema";
61+
export { errorHandler as supertokensErrorHandler } from "./supertokens/errorHandler";
6162

6263
export * from "./migrations/queries";
6364

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
2+
import { errorHandler as supertokensErrorHandler } from "supertokens-node/framework/fastify";
3+
4+
export const errorHandler = (
5+
error: Error,
6+
request: FastifyRequest,
7+
reply: FastifyReply,
8+
) => {
9+
return (
10+
supertokensErrorHandler() as unknown as (
11+
this: FastifyInstance,
12+
error: Error,
13+
request: FastifyRequest,
14+
reply: FastifyReply,
15+
) => Promise<void>
16+
).call(request.server, error, request, reply);
17+
};

packages/user/src/supertokens/plugin.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ import cors from "@fastify/cors";
22
import formDataPlugin from "@fastify/formbody";
33
import FastifyPlugin from "fastify-plugin";
44
import supertokens from "supertokens-node";
5-
import {
6-
errorHandler,
7-
plugin as supertokensPlugin,
8-
} from "supertokens-node/framework/fastify";
5+
import { plugin as supertokensPlugin } from "supertokens-node/framework/fastify";
96
import { verifySession } from "supertokens-node/recipe/session/framework/fastify";
107

8+
import { errorHandler } from "./errorHandler";
119
import init from "./init";
1210

13-
import type { FastifyError, FastifyInstance } from "fastify";
11+
import type { FastifyInstance } from "fastify";
1412

1513
const plugin = async (fastify: FastifyInstance) => {
1614
const { config, log } = fastify;
@@ -19,16 +17,9 @@ const plugin = async (fastify: FastifyInstance) => {
1917

2018
init(fastify);
2119

22-
// Explicitly cast the errorHandler to the correct type
23-
// [RL 2025-04-01] This should be fixed when supertokens-node is updated
24-
fastify.setErrorHandler(
25-
errorHandler() as unknown as (
26-
this: FastifyInstance,
27-
error: FastifyError,
28-
request: import("fastify").FastifyRequest,
29-
reply: import("fastify").FastifyReply,
30-
) => void,
31-
);
20+
if (config.user.supertokens.setErrorHandler !== false) {
21+
fastify.setErrorHandler(errorHandler);
22+
}
3223

3324
await fastify.register(cors, {
3425
origin: config.appOrigin,

packages/user/src/supertokens/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ interface SupertokensConfig {
4747
resetPasswordPath?: string;
4848
emailVerificationPath?: string;
4949
sendUserAlreadyExistsWarning?: boolean;
50+
setErrorHandler?: boolean;
5051
}
5152

5253
export type { SupertokensConfig, SupertokensRecipes };

0 commit comments

Comments
 (0)