Skip to content

Commit

Permalink
feat: added core settings
Browse files Browse the repository at this point in the history
  • Loading branch information
pclokcer committed May 2, 2024
1 parent 848025a commit 98c6ea8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export * from './src/RequestType'
export * from './src/Modules'
export { HandleErrorResponse } from './src/HandleErrorResponse'
export { HandleSuccessResponse } from './src/HandleSuccessResponse'
export * from './src/Events'
export * from './src/Events'
export * from "./src/Settings"
57 changes: 33 additions & 24 deletions src/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import enums from "./libs/enums"
import { RateLimitOptions } from "@fastify/rate-limit"
import { moduleList } from "./Modules"
import { handleErrorFunction } from "./HandleErrorResponse"
import { coreSettings } from "./Settings"

export const requestMetadataKey = Symbol("Request")
export const bodyMetadataKey = Symbol("Body")
Expand Down Expand Up @@ -53,12 +54,14 @@ async function runPrefixMiddleware<T extends readonly object[]>(req: TCustomFast
}
}
} catch (error) {
pino().error({
trace_id: req.trace_id,
timestamp: new Date(),
message: 'Prefix middleware Error',
error: typeof error === 'object' ? JSON.stringify(error) : error
})
if (coreSettings.logger) {
pino().error({
trace_id: req.trace_id,
timestamp: new Date(),
message: 'Prefix middleware Error',
error: typeof error === 'object' ? JSON.stringify(error) : error
})
}

throw error
}
Expand All @@ -74,12 +77,14 @@ async function runPermissionControl(req: TCustomFastifyRequest, res: FastifyRepl
}
}
} catch (error) {
pino().error({
trace_id: req.trace_id,
timestamp: new Date(),
message: 'Permission Middleware Error',
error: typeof error === 'object' ? JSON.stringify(error) : error
})
if (coreSettings.logger) {
pino().error({
trace_id: req.trace_id,
timestamp: new Date(),
message: 'Permission Middleware Error',
error: typeof error === 'object' ? JSON.stringify(error) : error
})
}

throw error
}
Expand All @@ -100,12 +105,14 @@ async function runMiddleware(req: TCustomFastifyRequest, res: FastifyReply, targ
}
}
} catch (error) {
pino().error({
trace_id: req.trace_id,
timestamp: new Date(),
message: 'Controller Middleware Error',
error: typeof error === 'object' ? JSON.stringify(error) : error
})
if (coreSettings.logger) {
pino().error({
trace_id: req.trace_id,
timestamp: new Date(),
message: 'Controller Middleware Error',
error: typeof error === 'object' ? JSON.stringify(error) : error
})
}

throw error
}
Expand Down Expand Up @@ -141,12 +148,14 @@ async function runController(req: TCustomFastifyRequest, res: FastifyReply, targ

return res.status(response.status_code || 200).send(response)
} catch (error) {
pino().error({
trace_id: req.trace_id,
timestamp: new Date(),
message: 'Controller Error',
error: typeof error === 'object' ? JSON.stringify(error) : error
})
if (coreSettings.logger) {
pino().error({
trace_id: req.trace_id,
timestamp: new Date(),
message: 'Controller Error',
error: typeof error === 'object' ? JSON.stringify(error) : error
})
}

throw error
}
Expand Down
7 changes: 7 additions & 0 deletions src/Settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const coreSettings: TSettings = {
logger: true
}

type TSettings = {
logger: boolean
}

0 comments on commit 98c6ea8

Please sign in to comment.