mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-23 09:09:36 +02:00
Reintroduce custom error types in request validation GitOrigin-RevId: 1985ca04c8fe693fb836b042517d94700343bc46
23 lines
716 B
JavaScript
23 lines
716 B
JavaScript
const { fromError } = require('zod-validation-error')
|
|
const { InvalidParamsError, InvalidRequestError } = require('./Errors')
|
|
|
|
function createHandleValidationError(statusCode = 400) {
|
|
return (err, req, res, next) => {
|
|
if (err instanceof InvalidParamsError) {
|
|
res
|
|
.status(404)
|
|
.json({ error: fromError(err.zodError).toString(), statusCode: 404 })
|
|
} else if (err instanceof InvalidRequestError) {
|
|
res
|
|
.status(statusCode)
|
|
.json({ error: fromError(err.zodError).toString(), statusCode })
|
|
} else {
|
|
next(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
const handleValidationError = createHandleValidationError(400)
|
|
|
|
module.exports = { handleValidationError, createHandleValidationError }
|