-
-
Notifications
You must be signed in to change notification settings - Fork 374
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
Validation error with async handler results in timeout #95
Comments
Quick update. The code does not result in a timeout anymore but in an internal server error. The validation error is logged in cloud watch but the httpError middleware is not catching it. Code looks like this:
|
I will try to troubleshoot this ASAP, but at a first glance i would say that the error is not being catched because the error is not using the http-errors class |
Your code is missing some important parts for me to be able to debug it. I stick with the original hypothesis that your code is generating a non http exception somewhere and thus it is not catched by the http error middleware. I recreated the following example following most of your steps and it seems to work as expected (also converted to normal js for convenience, to be run in node 8+ for async support). feel free to use it as a starting point to recreate a similar example that we can use to troubleshoot your issue further: const middy = require('middy')
const { doNotWaitForEmptyEventLoop, jsonBodyParser, validator, httpErrorHandler } = require('middy/middlewares')
const inputSchema = {
type: 'object',
properties: {
body: {
properties: {
name: { type: 'string' }
}
}
}
}
const createHandler = async (event, context, callback) => {
return ({
statusCode: 200,
body: "ok"
})
}
module.exports = middy(createHandler)
.use(doNotWaitForEmptyEventLoop())
.use(jsonBodyParser())
.use(validator({ inputSchema }))
.use(httpErrorHandler())
const handler = require('./handler')
const event = {
body: {
name: "Test"
}
}
handler(event, {}, (error, response) => {
console.log({ error, response })
}) |
Can't reproduce it right now. I will reopen if I do |
I'm having the same problem. const test = async event => {
return {
statusCode: 200,
body: JSON.stringify({ event }),
};
};
export const main = middyfy(test, schema); import middy from '@middy/core';
import jsonBodyParser from '@middy/http-json-body-parser';
import httpErrorHandler from '@middy/http-error-handler';
import validator from '@middy/validator';
export const middyfy = (handler, inputSchema) => {
return middy(handler)
.use(jsonBodyParser())
.use(validator({ inputSchema }))
.use(httpErrorHandler());
}; And the schema: export default {
type: 'object',
properties: {
name: { type: 'string' },
},
required: ['name'],
} as const; The client hangs, and on the server side (running locally using serverless-offline) I see:
When deployed to AWS, the client doesn't hang, but I get this immediately: {
"message": "Internal Server Error"
} |
@ffxsam What version are you running? #609 (comment) |
1.5.0 |
AWS Lambda only supports up to node v12 (without esm support). CloudWatch should tell you what the issues it. |
I don't think that's the issue here. I've gotten down to the bottom of it, I think: {
statusCode: 400,
body: JSON.stringify(validationErrors)
} Maybe I'm misunderstanding how it's intended to be used. But throwing an error inside a Lambda behind an API Gateway route causes the server to simply return "Internal server error" which isn't very useful. |
Nvm, got it. Accidentally mixed versions again (http-error-handler was at 2.x). This should also maybe take care of @FranzBusch 's issue too. Adding the |
Yeah, only the message is returned to ensure secure by default and the error response from the middleware is not in any specific error format. Thus needs to be handled custom to meet specific needs, like you just posted. Yeah, mixing version will cause unexpected issues. v2 is a big breaking change. |
Thanks! I wound up using my own middleware instead of http-error-handler, because it didn't return anything more than "Event object failed validation". I'm sending the whole error payload to the client. |
Yes,
|
When using an async handler and during the validation, an error occurs the function times out. The code below results in a timeout for me
Changing it to a none async handler it works fine.
I am using my own middleware for the event loop because of this issue. Is there any reason why async handlers are not working with the middleware?
The text was updated successfully, but these errors were encountered: