Skip to content

Commit ead6328

Browse files
committed
Merge branch 'main' of github.com:prefabs-tech/fastify into feat/error-handler
2 parents d5ad4bf + b3bbbfc commit ead6328

3 files changed

Lines changed: 22 additions & 18 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: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +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 {
14-
FastifyError,
15-
FastifyInstance,
16-
FastifyReply,
17-
FastifyRequest,
18-
} from "fastify";
11+
import type { FastifyInstance } from "fastify";
1912

2013
const plugin = async (fastify: FastifyInstance) => {
2114
const { config, log } = fastify;
@@ -25,14 +18,7 @@ const plugin = async (fastify: FastifyInstance) => {
2518
init(fastify);
2619

2720
if (config.user.supertokens.setErrorHandler !== false) {
28-
fastify.setErrorHandler(
29-
errorHandler() as unknown as (
30-
this: FastifyInstance,
31-
error: FastifyError,
32-
request: FastifyRequest,
33-
reply: FastifyReply,
34-
) => void,
35-
);
21+
fastify.setErrorHandler(errorHandler);
3622
}
3723

3824
await fastify.register(cors, {

0 commit comments

Comments
 (0)