From a6847b980438d53e758d5761b6c88518f3384590 Mon Sep 17 00:00:00 2001 From: Piotr Grundas Date: Tue, 24 Sep 2024 12:05:40 +0200 Subject: [PATCH] [MS-752] feat: Improve logs & error handling --- .env.test | 10 ++++++++ .github/workflows/test.yml | 5 ---- .gitignore | 3 ++- src/api/rest/saleor/webhooks.ts | 23 +++++++++++++++---- src/lib/api/errorHandler.ts | 5 +++- src/lib/config/schema.ts | 1 + .../plugins/winstonLoggingPlugin/plugin.ts | 22 ++++++++++-------- .../plugins/winstonLoggingPlugin/types.d.ts | 13 +++++++++++ {declarations => types}/fastify.d.ts | 0 {declarations => types}/global.d.ts | 0 10 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 .env.test create mode 100644 src/lib/plugins/winstonLoggingPlugin/types.d.ts rename {declarations => types}/fastify.d.ts (100%) rename {declarations => types}/global.d.ts (100%) diff --git a/.env.test b/.env.test new file mode 100644 index 0000000..da0e2e6 --- /dev/null +++ b/.env.test @@ -0,0 +1,10 @@ +DATABASE_URL=postgresql+asyncpg://example:example@localhost:5555/example +SALEOR_URL=https://test.eu.saleor.cloud +LOG_LEVEL=fatal +AWS_REGION=mock +AWS_ACCESS_KEY_ID=mock +AWS_SECRET_ACCESS_KEY=mock +AWS_SESSION_TOKEN=mock +SECRET_MANAGER_APP_CONFIG_PATH=mock +SQS_QUEUE_URL=https://sqs.queue.url +STATIC_URL=https://static.url diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 76d9bf7..bdcbc85 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,9 +35,4 @@ jobs: run: pnpm lint:tsc - name: Run tests - env: - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_REGION: ${{ secrets.AWS_REGION }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - SECRET_MANAGER_APP_CONFIG_PATH: ${{ secrets.SECRET_MANAGER_APP_CONFIG_PATH }} run: pnpm test diff --git a/.gitignore b/.gitignore index 2441491..2c67ffd 100644 --- a/.gitignore +++ b/.gitignore @@ -35,7 +35,8 @@ yarn-error.log* # local env files .env* -!.env.example* +!.env.example +!.env.test # turbo .turbo diff --git a/src/api/rest/saleor/webhooks.ts b/src/api/rest/saleor/webhooks.ts index 5b97138..ef380dc 100644 --- a/src/api/rest/saleor/webhooks.ts +++ b/src/api/rest/saleor/webhooks.ts @@ -9,12 +9,10 @@ import { getJWKSProvider } from "@/providers/jwks"; export const webhooks: FastifyPluginAsync = async (fastify) => { await fastify.register(rawBody); - /** - * Better to validate webhooks signature in one place. - * TODO: Create one common schema/type for responses - */ fastify.addHook("preHandler", async (request) => { - const parsedHeaders = saleorWebhookHeaders.parse(request.headers); + const parsedHeaders = saleorWebhookHeaders.parse(request.headers, { + path: ["headers"], + }); await verifyWebhookSignature({ forceRefresh: true, @@ -39,4 +37,19 @@ export const webhooks: FastifyPluginAsync = async (fastify) => { }, ] ); + + fastify.withTypeProvider().post( + "/test", + { + name: "saleor:webhooks:test", + }, + async (request, reply) => [ + { + amount: 5, + currency: "USD", + id: "my-id", + name: "Pete's method", + }, + ] + ); }; diff --git a/src/lib/api/errorHandler.ts b/src/lib/api/errorHandler.ts index 57d1405..fbf6cc9 100644 --- a/src/lib/api/errorHandler.ts +++ b/src/lib/api/errorHandler.ts @@ -52,7 +52,10 @@ export const errorHandler: FastifyInstance["errorHandler"] = ( errors: err.issues.map(({ code, message, path }) => ({ code: formatCode(code), message, - context: [err.validationContext, ...path].join(" > "), + context: (err.validationContext + ? [err.validationContext, ...path] + : path + ).join(" > "), })), }); } diff --git a/src/lib/config/schema.ts b/src/lib/config/schema.ts index c885bf9..65bb77b 100644 --- a/src/lib/config/schema.ts +++ b/src/lib/config/schema.ts @@ -6,6 +6,7 @@ export const commonConfigSchema = z.object({ .default("local"), IS_BROWSER: z.boolean().default(typeof window !== "undefined"), IS_DEVELOPMENT: z.boolean().default(process.env.NODE_ENV === "development"), + IS_TEST: z.boolean().default(process.env.NODE_ENV === "test"), IS_SSR: z.boolean().default(typeof window === "undefined"), NODE_ENV: z.enum(["development", "test", "production"]).default("production"), }); diff --git a/src/lib/plugins/winstonLoggingPlugin/plugin.ts b/src/lib/plugins/winstonLoggingPlugin/plugin.ts index ac4343d..9c97898 100644 --- a/src/lib/plugins/winstonLoggingPlugin/plugin.ts +++ b/src/lib/plugins/winstonLoggingPlugin/plugin.ts @@ -4,12 +4,14 @@ import fastifyPlugin from "fastify-plugin"; const plugin: FastifyPluginCallback = (fastify, {}, next) => { fastify.addHook("onRequest", (req, reply, done) => { req.log.info({ - body: req.body, - method: req.method, - query: req.query, + message: { + body: req.body, + method: req.method, + query: req.query, + url: req.raw.url, + }, statusCode: reply.raw.statusCode, - type: "request", - url: req.raw.url, + type: "REQUEST", }); done(); @@ -17,11 +19,13 @@ const plugin: FastifyPluginCallback = (fastify, {}, next) => { fastify.addHook("onResponse", (req, reply, done) => { req.log.info({ - elapsedTime: reply.elapsedTime, - method: req.method, + message: { + method: req.method, + url: req.raw.url, + }, statusCode: reply.raw.statusCode, - type: "response", - url: req.raw.url, + elapsedTime: reply.elapsedTime, + type: "RESPONSE", }); done(); diff --git a/src/lib/plugins/winstonLoggingPlugin/types.d.ts b/src/lib/plugins/winstonLoggingPlugin/types.d.ts new file mode 100644 index 0000000..38f2436 --- /dev/null +++ b/src/lib/plugins/winstonLoggingPlugin/types.d.ts @@ -0,0 +1,13 @@ +import { FastifyBaseLogger, FastifyInstance } from "fastify"; +import { type LeveledLogMethod } from "winston"; + +declare module fastify { + interface FastifyBaseLogger { + debug: LeveledLogMethod; + error: LeveledLogMethod; + fatal: LeveledLogMethod; + info: LeveledLogMethod; + trace: LeveledLogMethod; + warn: LeveledLogMethod; + } +} diff --git a/declarations/fastify.d.ts b/types/fastify.d.ts similarity index 100% rename from declarations/fastify.d.ts rename to types/fastify.d.ts diff --git a/declarations/global.d.ts b/types/global.d.ts similarity index 100% rename from declarations/global.d.ts rename to types/global.d.ts