-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor errors to use @fastify/error and throw correct validation er…
…ror (#110)
- Loading branch information
Showing
6 changed files
with
74 additions
and
59 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 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
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,41 @@ | ||
import createError from '@fastify/error'; | ||
import type { FastifySchemaValidationError } from 'fastify/types/schema'; | ||
import type { ZodError } from 'zod'; | ||
|
||
export class ResponseSerializationError extends createError<[{ cause: ZodError }]>( | ||
'FST_ERR_RESPONSE_SERIALIZATION', | ||
"Response doesn't match the schema", | ||
500, | ||
) { | ||
constructor( | ||
public cause: ZodError, | ||
public method: string, | ||
public url: string, | ||
) { | ||
super({ cause }); | ||
} | ||
} | ||
|
||
export const InvalidSchemaError = createError<[string]>( | ||
'FST_ERR_INVALID_SCHEMA', | ||
'Invalid schema passed: %s', | ||
500, | ||
); | ||
|
||
export const createValidationError = ( | ||
error: ZodError, | ||
method: string, | ||
url: string, | ||
): FastifySchemaValidationError[] => | ||
error.errors.map((issue) => ({ | ||
keyword: issue.code, | ||
instancePath: `/${issue.path.join('/')}`, | ||
schemaPath: `#/${issue.path.join('/')}/${issue.code}`, | ||
params: { | ||
issue, | ||
zodError: error, | ||
method, | ||
url, | ||
}, | ||
message: error.message, | ||
})); |
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