-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Misc refactors / improvements (#100)
- improved error handling / reporting - moved initializers to separate files - improved test case reliability - improved request / response typing
- Loading branch information
Miłosz Skaza
authored
Apr 8, 2023
1 parent
f65f0ae
commit 884f40c
Showing
18 changed files
with
182 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { FastifyError, FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; | ||
import * as Sentry from "@sentry/node"; | ||
|
||
export const initErrorHandler = (app: FastifyInstance) => { | ||
if (app.config.SENTRY_DSN) { | ||
app.log.info("Sentry Reporting Enabled"); | ||
Sentry.init({ | ||
dsn: app.config.SENTRY_DSN, | ||
tracesSampleRate: app.config.SENTRY_TRACES_SAMPLE, | ||
}); | ||
} | ||
|
||
app.setErrorHandler( | ||
async (error: FastifyError, request: FastifyRequest, reply: FastifyReply) => { | ||
if (!reply.statusCode || reply.statusCode === 200) { | ||
if (error.statusCode && error.statusCode >= 400) { | ||
return reply.code(error.statusCode).send(error); | ||
} | ||
} | ||
|
||
// only log and capture 500s - not validation errors | ||
request.log.error(error); | ||
if (app.config.SENTRY_DSN) { | ||
Sentry.captureException(error); | ||
} | ||
|
||
return reply.code(500).send(new Error("Internal Server Error")); | ||
}, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
import { FastifyInstance, FastifyRegisterOptions } from "fastify"; | ||
import fastifySwagger, { FastifyDynamicSwaggerOptions } from "@fastify/swagger"; | ||
import fastifySwaggerUI from "@fastify/swagger-ui"; | ||
|
||
const PkgPath: string = path.normalize( | ||
path.join(__dirname, "..", "..", "package.json"), | ||
); | ||
const PKG: { name: string; description: string; version: string } = JSON.parse( | ||
fs.readFileSync(PkgPath).toString(), | ||
); | ||
|
||
const SwaggerConfig: FastifyRegisterOptions<FastifyDynamicSwaggerOptions> = { | ||
openapi: { | ||
info: { | ||
title: PKG.name, | ||
description: PKG.description, | ||
version: PKG.version, | ||
}, | ||
}, | ||
}; | ||
|
||
export const initSwagger = (app: FastifyInstance) => { | ||
app.register(fastifySwagger, SwaggerConfig); | ||
app.register(fastifySwaggerUI, { | ||
routePrefix: "/docs", | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.