mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-23 09:09:36 +02:00
Merge pull request #33413 from overleaf/revert-33040-em-parse-req-errors
Revert "Replace isZodErrorLike with custom error types in request validation" GitOrigin-RevId: 1f51fe9e14ffabf283f1229552d3887136420f8f
This commit is contained in:
@@ -1,29 +1,5 @@
|
||||
// @ts-check
|
||||
|
||||
const OError = require('@overleaf/o-error')
|
||||
|
||||
/**
|
||||
* @typedef {import('zod').ZodError} ZodError
|
||||
*/
|
||||
class ParamsError extends OError {}
|
||||
|
||||
class InvalidRequestError extends OError {
|
||||
/**
|
||||
* @param {ZodError} zodError
|
||||
*/
|
||||
constructor(zodError) {
|
||||
super('Invalid request', {}, zodError)
|
||||
this.zodError = zodError
|
||||
}
|
||||
}
|
||||
|
||||
class InvalidParamsError extends OError {
|
||||
/**
|
||||
* @param {ZodError} zodError
|
||||
*/
|
||||
constructor(zodError) {
|
||||
super('Invalid request parameters', {}, zodError)
|
||||
this.zodError = zodError
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { InvalidParamsError, InvalidRequestError }
|
||||
module.exports = { ParamsError }
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
const { fromError } = require('zod-validation-error')
|
||||
const { InvalidParamsError, InvalidRequestError } = require('./Errors')
|
||||
const { isZodErrorLike, fromError } = require('zod-validation-error')
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
return [
|
||||
(err, req, res, next) => {
|
||||
if (!isZodErrorLike(err)) {
|
||||
return next(err)
|
||||
}
|
||||
|
||||
res.status(statusCode).json({ ...fromError(err), statusCode })
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
const handleValidationError = createHandleValidationError(400)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const { InvalidParamsError, InvalidRequestError } = require('./Errors')
|
||||
const { ParamsError } = require('./Errors')
|
||||
const { z } = require('zod')
|
||||
const { zz } = require('./zodHelpers')
|
||||
const { parseReq } = require('./parseReq')
|
||||
@@ -15,6 +15,5 @@ module.exports = {
|
||||
parseReq,
|
||||
handleValidationError,
|
||||
createHandleValidationError,
|
||||
InvalidRequestError,
|
||||
InvalidParamsError,
|
||||
ParamsError,
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// @ts-check
|
||||
const { InvalidRequestError, InvalidParamsError } = require('./Errors')
|
||||
const { ParamsError } = require('./Errors')
|
||||
|
||||
/**
|
||||
* @typedef {import('zod').ZodType} ZodType
|
||||
@@ -25,9 +25,9 @@ function parseReq(req, schema) {
|
||||
return parsed.data
|
||||
} else if (parsed.error.issues.some(issue => issue.path[0] === 'params')) {
|
||||
// Parts of the URL path failed to validate; throw a specific error
|
||||
throw new InvalidParamsError(parsed.error)
|
||||
throw new ParamsError('Invalid params').withCause(parsed.error)
|
||||
} else {
|
||||
throw new InvalidRequestError(parsed.error)
|
||||
throw parsed.error
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ describe('parseReq', () => {
|
||||
}),
|
||||
})
|
||||
)
|
||||
).toThrowError(expect.objectContaining({ name: 'InvalidParamsError' }))
|
||||
).toThrowError(expect.objectContaining({ name: 'ParamsError' }))
|
||||
})
|
||||
|
||||
it('should throw an error containing issues if the schema is invalid', () => {
|
||||
@@ -75,11 +75,9 @@ describe('parseReq', () => {
|
||||
)
|
||||
).toThrowError(
|
||||
expect.objectContaining({
|
||||
zodError: expect.objectContaining({
|
||||
issues: expect.arrayContaining([
|
||||
expect.objectContaining({ path: ['body', 'name'] }),
|
||||
]),
|
||||
}),
|
||||
issues: expect.arrayContaining([
|
||||
expect.objectContaining({ path: ['body', 'name'] }),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user