From af46d144a13ea05d5618aae51e5a9f5f363e98fe Mon Sep 17 00:00:00 2001 From: Ramesh Date: Tue, 12 Aug 2025 17:02:00 +0545 Subject: [PATCH] feat: add config to toggle setting error handler while register supertokens plugin --- packages/user/src/index.ts | 1 + packages/user/src/supertokens/errorHandler.ts | 17 +++++++++++++++ packages/user/src/supertokens/plugin.ts | 21 ++++++------------- packages/user/src/supertokens/types/index.ts | 1 + 4 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 packages/user/src/supertokens/errorHandler.ts diff --git a/packages/user/src/index.ts b/packages/user/src/index.ts index 8431a4391..592b5758b 100644 --- a/packages/user/src/index.ts +++ b/packages/user/src/index.ts @@ -58,6 +58,7 @@ export { default as hasUserPermission } from "./lib/hasUserPermission"; export { default as ProfileValidationClaim } from "./supertokens/utils/profileValidationClaim"; export { default as createUserContext } from "./supertokens/utils/createUserContext"; export { default as userSchema } from "./graphql/schema"; +export { errorHandler as supertokensErrorHandler } from "./supertokens/errorHandler"; export * from "./migrations/queries"; diff --git a/packages/user/src/supertokens/errorHandler.ts b/packages/user/src/supertokens/errorHandler.ts new file mode 100644 index 000000000..f7a848835 --- /dev/null +++ b/packages/user/src/supertokens/errorHandler.ts @@ -0,0 +1,17 @@ +import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; +import { errorHandler as supertokensErrorHandler } from "supertokens-node/framework/fastify"; + +export const errorHandler = ( + error: Error, + request: FastifyRequest, + reply: FastifyReply, +) => { + return ( + supertokensErrorHandler() as unknown as ( + this: FastifyInstance, + error: Error, + request: FastifyRequest, + reply: FastifyReply, + ) => Promise + ).call(request.server, error, request, reply); +}; diff --git a/packages/user/src/supertokens/plugin.ts b/packages/user/src/supertokens/plugin.ts index 93dce4a4c..9e9e4eead 100644 --- a/packages/user/src/supertokens/plugin.ts +++ b/packages/user/src/supertokens/plugin.ts @@ -2,15 +2,13 @@ import cors from "@fastify/cors"; import formDataPlugin from "@fastify/formbody"; import FastifyPlugin from "fastify-plugin"; import supertokens from "supertokens-node"; -import { - errorHandler, - plugin as supertokensPlugin, -} from "supertokens-node/framework/fastify"; +import { plugin as supertokensPlugin } from "supertokens-node/framework/fastify"; import { verifySession } from "supertokens-node/recipe/session/framework/fastify"; +import { errorHandler } from "./errorHandler"; import init from "./init"; -import type { FastifyError, FastifyInstance } from "fastify"; +import type { FastifyInstance } from "fastify"; const plugin = async (fastify: FastifyInstance) => { const { config, log } = fastify; @@ -19,16 +17,9 @@ const plugin = async (fastify: FastifyInstance) => { init(fastify); - // Explicitly cast the errorHandler to the correct type - // [RL 2025-04-01] This should be fixed when supertokens-node is updated - fastify.setErrorHandler( - errorHandler() as unknown as ( - this: FastifyInstance, - error: FastifyError, - request: import("fastify").FastifyRequest, - reply: import("fastify").FastifyReply, - ) => void, - ); + if (config.user.supertokens.setErrorHandler !== false) { + fastify.setErrorHandler(errorHandler); + } await fastify.register(cors, { origin: config.appOrigin, diff --git a/packages/user/src/supertokens/types/index.ts b/packages/user/src/supertokens/types/index.ts index 495e2aaee..b22ad8302 100644 --- a/packages/user/src/supertokens/types/index.ts +++ b/packages/user/src/supertokens/types/index.ts @@ -47,6 +47,7 @@ interface SupertokensConfig { resetPasswordPath?: string; emailVerificationPath?: string; sendUserAlreadyExistsWarning?: boolean; + setErrorHandler?: boolean; } export type { SupertokensConfig, SupertokensRecipes };