-
-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
middy/validator : How to format errors in proper json format #720
Comments
Also interested in how we can format the error. |
By default middy won't leak internal information for security reasons. You need to expose the errors yourself. Because there are multiple standards and ways to do this we don't include it by default. You can access them from If you'd like an example, have a look at: https://github.com/willfarrell/middy-jsonapi/blob/main/index.js#L69 If you'd like to share your middleware for handling |
Thanks, I figured out my solution. I have created my own custom middleware for this. |
It took me a while to understand how to do this, so hopefully the following (TypeScript) example helps. To be placed in-between
|
@morcs Would you like to open a PR to have this example added to the docs? |
Here's a version of the above that doesn't require Custom Middleware File// custom-middleware.js
export const validationErrorJSONFormatter = () => ({
onError: (request) => {
const error = request.error;
if (error.expose && error.statusCode === 400) {
request.response = {
statusCode: 400,
body: JSON.stringify({
message: error.message,
validationErrors: error.cause,
}),
headers: { "Content-Type": "application/json" },
};
}
},
}); Use the custom middlewareimport {validationErrorJSONFormatter} from './custom-middleware'
import schema from './schema'
middy()
.use(validationErrorJSONFormatter())
.use(
validatorMiddleware({
eventSchema: transpileSchema(schema, { verbose: true }),
})
) |
// validator Error type export default (handler: Handler, eventSchema: object = {}): middy.MiddyfiedHandler =>
]);`` Following the above @morcs comment i have apply some more formation |
I am using middy/validator for validating input request with httpErrorHandler, but it giving message 'Event object failed validation' response only.
I was expecting that it will show proper error field and its respective error.
BadRequestError: Event object failed validation
at createError (D:\Project\BoxSmart POC\middy-poc\node_modules@middy\util\index.js:259:10)
at validatorMiddlewareBefore (D:\Project\BoxSmart POC\middy-poc\node_modules@middy\validator\index.js:53:21)
at runMiddlewares (D:\Project\BoxSmart POC\middy-poc\node_modules@middy\core\index.js:120:88)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async runRequest (D:\Project\BoxSmart POC\middy-poc\node_modules@middy\core\index.js:80:5)
at async InProcessRunner.run (D:\Project\BoxSmart POC\middy-poc\node_modules\serverless-offline\dist\lambda\handler-runner\in-process-runner\InProcessRunner.js:213:24)
at async LambdaFunction.runHandler (D:\Project\BoxSmart POC\middy-poc\node_modules\serverless-offline\dist\lambda\LambdaFunction.js:355:20)
at async hapiHandler (D:\Project\BoxSmart POC\middy-poc\node_modules\serverless-offline\dist\events\http\HttpServer.js:602:18)
at async exports.Manager.execute (D:\Project\BoxSmart POC\middy-poc\node_modules@hapi\hapi\lib\toolkit.js:60:28)
at async Object.internals.handler (D:\Project\BoxSmart POC\middy-poc\node_modules@hapi\hapi\lib\handler.js:46:20)
at async exports.execute (D:\Project\BoxSmart POC\middy-poc\node_modules@hapi\hapi\lib\handler.js:31:20)
at async Request._lifecycle (D:\Project\BoxSmart POC\middy-poc\node_modules@hapi\hapi\lib\request.js:372:32)
at async Request._execute (D:\Project\BoxSmart POC\middy-poc\node_modules@hapi\hapi\lib\request.js:280:9) {
details: [
{
instancePath: '/body/password',
schemaPath: '#/properties/body/properties/password/minLength',
keyword: 'minLength',
params: [Object],
message: 'must NOT be shorter than 8 characters'
}
]
}
The text was updated successfully, but these errors were encountered: