-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update template structures & code quality
- Loading branch information
phucvinh57
committed
Aug 7, 2023
1 parent
8b49c1c
commit 114ed2e
Showing
31 changed files
with
228 additions
and
158 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
File renamed without changes.
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,76 @@ | ||
import { TRY_LATER } from '@constants'; | ||
import { FastifyError, FastifyReply, FastifyRequest } from 'fastify'; | ||
|
||
export function customErrorHandler(err: FastifyError, _req: FastifyRequest, res: FastifyReply) { | ||
if (err.statusCode === undefined || err.statusCode >= 500) { | ||
err.message = TRY_LATER; | ||
return res.send(err); | ||
} | ||
if (!err.validation || err.validation.length === 0) { | ||
return res.send(err); | ||
} | ||
|
||
const validation = err.validation[0]; | ||
if (validation.keyword === 'required') { | ||
err.message = `${validation.params.missingProperty[0].toUpperCase() + validation.params.missingProperty.slice(1)} is required !`; | ||
return res.send(err); | ||
} | ||
// Error occurs on PathParams | ||
else if (validation.instancePath.length === 0) { | ||
err.message = 'Invalid path parameters !'; | ||
return res.send(err); | ||
} | ||
|
||
const instanceAccesses = validation.instancePath.split('/'); | ||
let rawErrorField: string; | ||
|
||
// Occurs if error on an item of an array | ||
if (!isNaN(parseInt(instanceAccesses[instanceAccesses.length - 1]))) { | ||
// If not have field name of input | ||
if (instanceAccesses[instanceAccesses.length - 2].length === 0) rawErrorField = 'item'; | ||
else { | ||
rawErrorField = `An item of${ | ||
instanceAccesses[instanceAccesses.length - 2][0].toUpperCase() + instanceAccesses[instanceAccesses.length - 2].slice(1) | ||
}`; | ||
} | ||
} else rawErrorField = instanceAccesses[instanceAccesses.length - 1]; | ||
|
||
const errorField = rawErrorField | ||
// For snake case | ||
.replaceAll('_', ' ') | ||
// For camel case | ||
.replace(/([A-Z])/g, ' $1') | ||
.trim() | ||
.split(' ') | ||
.map((word) => word[0].toLowerCase() + word.slice(1)) | ||
.join(' '); | ||
|
||
const capitalizedErrorField = errorField[0].toUpperCase() + errorField.slice(1); | ||
|
||
if (validation.keyword === 'maxLength' || validation.keyword === 'minLength') { | ||
err.message = `${capitalizedErrorField} ${validation.message ? validation.message : 'is invalid'} !`; | ||
} else if (validation.keyword === 'enum') { | ||
err.message = `${capitalizedErrorField} must be one of allowed values: ${ | ||
validation.params.allowedValues instanceof Array | ||
? validation.params.allowedValues.map((value: string) => `"${value}"`).join(', ') | ||
: validation.params.allowedValues | ||
} !`; | ||
} else if (validation.keyword === 'minItems' || validation.keyword === 'maxItems') { | ||
err.message = `${capitalizedErrorField} ${err.message} !`; | ||
} else if (validation.keyword === 'minimum') { | ||
err.message = `${capitalizedErrorField} must be greater than or equal ${validation.params.limit} !`; | ||
} else if (validation.keyword === 'maximum') { | ||
err.message = `${capitalizedErrorField} must be less than or equal ${validation.params.limit} !`; | ||
} else if (validation.keyword === 'type') { | ||
err.message = `${capitalizedErrorField} ${err.message} !`; | ||
} else if (validation.keyword === 'uniqueItems') { | ||
err.message = `Items of ${errorField} must be unique !`; | ||
} else if (validation.keyword === 'format') { | ||
err.message = `${capitalizedErrorField} has invalid format !`; | ||
} else if (validation.keyword === 'const') { | ||
err.message = `${capitalizedErrorField} has invalid value !`; | ||
} | ||
// Default | ||
else err.message = 'Bad request !'; | ||
return res.send(err); | ||
} |
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 |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
*/ | ||
|
||
export * from './env'; | ||
export * from './errorHandler'; | ||
export * from './logger'; | ||
export * from './swagger'; |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
import { FastifyBaseLogger } from 'fastify'; | ||
import 'fastify'; | ||
|
||
declare global { | ||
// eslint-disable-next-line no-var | ||
var logger: FastifyBaseLogger; | ||
declare module 'fastify' { | ||
interface FastifyRequest { | ||
userId: string; | ||
} | ||
interface FastifyInstance { | ||
start: () => Promise<void>; | ||
shutdown: () => Promise<void>; | ||
} | ||
} | ||
export {}; |
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
Oops, something went wrong.