for the last 3-4 hours i've been trying to correctly return error from custom @middy middleware unsuccessfully
export const middyfy = (handler: any) => {
return middy(handler).use(middyJsonBodyParser());
};
export const authorizedMiddify = (handler: any) => {
return middyfy(handler)
.use(httpHeaderNormalizer())
.use(httpErrorHandler())
.use({
before: (request, next) => {
console.log(3)
throw createError(401, 'Unauthorized');
}
});
};
this logs:
{
body: 'Unauthorized',
headers: { 'Content-Type': 'text/plain' },
statusCode: 401
}
and in postman i receive empty body and 502 error.
what is wrong here? P.S: createError is from @middy/util
I think you're looking for
createHttpErrorfrom@middy/http-error-handler. Alternatively, you can create a wrapper for this—maybe call itcreateError()—and throw. documentation