willfarrell on main
chore: dep updates (compare)
willfarrell on 3.1.1
willfarrell on main
feat: add in module param to npm chore: version bump (compare)
{"email":"abc@gmail.com"}
. and my Schema looks like this .const inputRequestSchema = {
type: 'object',
required: [
'email',
],
properties: {
email: {
type: 'string',
},
},
additionalProperties: true,
};
when i fire up the request , i get error message: 'must have required property email'les/@middy/core/index.js?:80:5) {ex.js?:51:21) Event object failed validation. any help here ? i am using following dependency "@middy/core": "^2.2.0",
"@middy/http-error-handler": "^2.2.0",
"@middy/http-header-normalizer": "^2.2.0",
"@middy/http-json-body-parser": "^2.2.0",
"@middy/validator": "^2.2.0"
. . use of middleware code \ export const handler = middy(handlerLambda)
.use(httpHeaderNormalizer())
.use(httpJsonBodyParser())
.use(validator({ inputSchema: inputRequestSchema }))
.use(httpErrorHandler());
{ "queryStringParameters":{...}, "body":{...} }
. https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html
additionalProperties: true,
just to make sure other added by APIGateway do not create issue .but still it says message: 'must have required property email'les/@middy/core/index.js?:80:5) {ex.js?:51:21) Event object failed validation
httpErrorHandler
is a catch all if an error hasn't already been handled. It returns a generic message (which can be overridden), because you don't want stack traces returned to the consumer, leading to leaking too much information (security best practice).
module.exports.handler = async ({ Records }) => {
const failedMessageIds = []
let batch = {}
for (const record of Records) {
try {
console.log(`Processing ${record.messageId}`)
const bodyData = JSON.parse(record.body)
await processEvent(bodyData, batch)
} catch (e) {
console.log(e)
failedMessageIds.push(record.messageId)
}
}
await table.batchWrite(batch)
await flushQueue()
return {
batchItemFailures: failedMessageIds.map(id => ({ itemIdentifier: id }))
}
}
That's what I'm using now and goes fast.
const middy = require("@middy/core")
const sqsBatch = require("@middy/sqs-partial-batch-failure")
const sqsJsonBodyParser = require("@middy/sqs-json-body-parser")
const originalHandler = ({ Records }) => {
const recordPromises = Records.map(async ({ body }) => processEvent(body))
return Promise.allSettled(recordPromises)
}
module.exports = {
handler: middy(originalHandler).use([sqsJsonBodyParser(), sqsBatch()])
}
This is the slow code.
Any ideas?
hey folks, quick question.
I want to create a logger in a middleware that always logs the context information and then pass that logger to the middleware. Is my only option to attach the logger to the event or context?
Something like this:
function attachLoggerMiddleware(){
const attachLoggerMiddlewareBefore = (
request
) => {
request.event.logger = new Logger(request.context);
};
return {
before: attachLoggerMiddlewareBefore,
};
}
And then in the handler I can do:
function (event) {
event.logger.info('yo');
}
A new redesigned website has just been publishet at https://middy.js.org .
We'd really love your feedback on the new design, documentation and all over the new DX.
HI all,
I'm having an issue using middyjs
, I'll glad if anyone here can help.
Is there a particular format one must follow to make use of the onError function? I've caught the error and passed it into the onError, but I've not been able to return this error back to the handler. It just keeps throwing a 502. The success case works perfectly.
const onError = async (request: any): Promise<any> => {
console.log(request.error)
if (request.error.statusCode == 403) return forbiddenError('Unauthorized Request');
// throw createHttpError(401, 'Unauthorized Request')
return;
};
const before = async (request: any): Promise<any> => {
const integratorPk = '';
if (integratorPk) {
console.log('Success');
return;
}
return forbiddenError('Unauthorized Request');
};
export function domainValidationMiddleWare(): MiddlewareObject<
APIGatewayEvent,
APIGatewayProxyResult
> {
return {before, after, onError
};
}
onError
function successfully logs request.error
, but throws a 502
instead of returning 403
Created an issue here also. Thanking you in advance.