Skip to content

Commit

Permalink
[MS-752] feat: Improve logs & error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrgrundas committed Sep 24, 2024
1 parent 0295be3 commit a6847b9
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 21 deletions.
10 changes: 10 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -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
5 changes: 0 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ yarn-error.log*

# local env files
.env*
!.env.example*
!.env.example
!.env.test

# turbo
.turbo
Expand Down
23 changes: 18 additions & 5 deletions src/api/rest/saleor/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -39,4 +37,19 @@ export const webhooks: FastifyPluginAsync = async (fastify) => {
},
]
);

fastify.withTypeProvider<ZodTypeProvider>().post(
"/test",
{
name: "saleor:webhooks:test",
},
async (request, reply) => [
{
amount: 5,
currency: "USD",
id: "my-id",
name: "Pete's method",
},
]
);
};
5 changes: 4 additions & 1 deletion src/lib/api/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(" > "),
})),
});
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
});
Expand Down
22 changes: 13 additions & 9 deletions src/lib/plugins/winstonLoggingPlugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ 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();
});

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();
Expand Down
13 changes: 13 additions & 0 deletions src/lib/plugins/winstonLoggingPlugin/types.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
File renamed without changes.
File renamed without changes.

0 comments on commit a6847b9

Please sign in to comment.