Unreadable error message #3363
-
Hello! I want to use powertools-lambda-ts in my project but I am facing an issue with capturing the errors when the parsing fails. I would expect an Object that I can use to return to the front-end however I can only access the stack trace. I am sure I am missing something but I don't know what. logger.error('Error parsing event', { event: event.error }); {
"level": "ERROR",
"message": "Error parsing event",
"sampling_rate": 0,
"service": "service_undefined",
"timestamp": "2024-11-25T14:08:34.378Z",
"xray_trace_id": "1-674484e2-2a74319a3df4a2ac7fef4758",
"event": {
"name": "ParseError",
"location": "/node_modules/.pnpm/@aws-lambda-powertools+parser@2.10.0_@middy+core@6.0.0_zod@3.23.8/node_modules/@aws-lambda-powertools/parser/lib/esm/envelopes/apigw.js:26",
"message": "Failed to parse ApiGatewayEnvelope body. This error was caused by: Failed to parse envelope. This error was caused by: [\n {\n \"code\": \"too_big\",\n \"maximum\": 2,\n \"type\": \"string\",\n \"inclusive\": true,\n \"exact\": false,\n \"message\": \"String must contain at most 2 character(s)\",\n \"path\": [\n \"firstname\"\n ]\n }\n]..",
"stack": "ParseError: Failed to parse ApiGatewayEnvelope body. This error was caused by: Failed to parse envelope. This error was caused by: [\n {\n \"code\": \"too_big\",\n \"maximum\": 2,\n \"type\": \"string\",\n \"inclusive\": true,\n \"exact\": false,\n \"message\": \"String must contain at most 2 character(s)\",\n \"path\": [\n \"firstname\"\n ]\n }\n]..\n at Object.safeParse (/node_modules/.pnpm/@aws-lambda-powertools+parser@2.10.0_@middy+core@6.0.0_zod@3.23.8/node_modules/@aws-lambda-powertools/parser/lib/esm/envelopes/apigw.js:26:24)\n at parse (/node_modules/.pnpm/@aws-lambda-powertools+parser@2.10.0_@middy+core@6.0.0_zod@3.23.8/node_modules/@aws-lambda-powertools/parser/lib/esm/parser.js:29:25)\n at request (/node_modules/.pnpm/@aws-lambda-powertools+parser@2.10.0_@middy+core@6.0.0_zod@3.23.8/node_modules/@aws-lambda-powertools/parser/lib/esm/middleware/parser.js:33:25)\n at runMiddlewares (/node_modules/.pnpm/@middy+core@6.0.0/node_modules/@middy/core/index.js:229:23)\n at runRequest (/node_modules/.pnpm/@middy+core@6.0.0/node_modules/@middy/core/index.js:153:11)\n at Runtime.i (/node_modules/.pnpm/@middy+core@6.0.0/node_modules/@middy/core/index.js:43:12)\n at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)",
"cause": {
"name": "ParseError",
"location": "/node_modules/.pnpm/@aws-lambda-powertools+parser@2.10.0_@middy+core@6.0.0_zod@3.23.8/node_modules/@aws-lambda-powertools/parser/lib/esm/envelopes/envelope.js:50",
"message": "Failed to parse envelope. This error was caused by: [\n {\n \"code\": \"too_big\",\n \"maximum\": 2,\n \"type\": \"string\",\n \"inclusive\": true,\n \"exact\": false,\n \"message\": \"String must contain at most 2 character(s)\",\n \"path\": [\n \"firstname\"\n ]\n }\n].",
"stack": "ParseError: Failed to parse envelope. This error was caused by: [\n {\n \"code\": \"too_big\",\n \"maximum\": 2,\n \"type\": \"string\",\n \"inclusive\": true,\n \"exact\": false,\n \"message\": \"String must contain at most 2 character(s)\",\n \"path\": [\n \"firstname\"\n ]\n }\n].\n at Object.safeParse (/node_modules/.pnpm/@aws-lambda-powertools+parser@2.10.0_@middy+core@6.0.0_zod@3.23.8/node_modules/@aws-lambda-powertools/parser/lib/esm/envelopes/envelope.js:50:28)\n at Object.safeParse (/node_modules/.pnpm/@aws-lambda-powertools+parser@2.10.0_@middy+core@6.0.0_zod@3.23.8/node_modules/@aws-lambda-powertools/parser/lib/esm/envelopes/apigw.js:22:37)\n at parse (/node_modules/.pnpm/@aws-lambda-powertools+parser@2.10.0_@middy+core@6.0.0_zod@3.23.8/node_modules/@aws-lambda-powertools/parser/lib/esm/parser.js:29:25)\n at request (/node_modules/.pnpm/@aws-lambda-powertools+parser@2.10.0_@middy+core@6.0.0_zod@3.23.8/node_modules/@aws-lambda-powertools/parser/lib/esm/middleware/parser.js:33:25)\n at runMiddlewares (/node_modules/.pnpm/@middy+core@6.0.0/node_modules/@middy/core/index.js:229:23)\n at runRequest (/node_modules/.pnpm/@middy+core@6.0.0/node_modules/@middy/core/index.js:153:11)\n at Runtime.i (/node_modules/.pnpm/@middy+core@6.0.0/node_modules/@middy/core/index.js:43:12)\n at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)",
"cause": {
"name": "ZodError",
"location": "/node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/index.mjs:587",
"message": "[\n {\n \"code\": \"too_big\",\n \"maximum\": 2,\n \"type\": \"string\",\n \"inclusive\": true,\n \"exact\": false,\n \"message\": \"String must contain at most 2 character(s)\",\n \"path\": [\n \"firstname\"\n ]\n }\n]",
"stack": "ZodError: [\n {\n \"code\": \"too_big\",\n \"maximum\": 2,\n \"type\": \"string\",\n \"inclusive\": true,\n \"exact\": false,\n \"message\": \"String must contain at most 2 character(s)\",\n \"path\": [\n \"firstname\"\n ]\n }\n]\n at Object.get error (/node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/index.mjs:587:31)\n at Object.safeParse (/node_modules/.pnpm/@aws-lambda-powertools+parser@2.10.0_@middy+core@6.0.0_zod@3.23.8/node_modules/@aws-lambda-powertools/parser/lib/esm/envelopes/envelope.js:51:39)\n at Object.safeParse (/node_modules/.pnpm/@aws-lambda-powertools+parser@2.10.0_@middy+core@6.0.0_zod@3.23.8/node_modules/@aws-lambda-powertools/parser/lib/esm/envelopes/apigw.js:22:37)\n at parse (/node_modules/.pnpm/@aws-lambda-powertools+parser@2.10.0_@middy+core@6.0.0_zod@3.23.8/node_modules/@aws-lambda-powertools/parser/lib/esm/parser.js:29:25)\n at request (/node_modules/.pnpm/@aws-lambda-powertools+parser@2.10.0_@middy+core@6.0.0_zod@3.23.8/node_modules/@aws-lambda-powertools/parser/lib/esm/middleware/parser.js:33:25)\n at runMiddlewares (/node_modules/.pnpm/@middy+core@6.0.0/node_modules/@middy/core/index.js:229:23)\n at runRequest (/node_modules/.pnpm/@middy+core@6.0.0/node_modules/@middy/core/index.js:153:11)\n at Runtime.i (/node_modules/.pnpm/@middy+core@6.0.0/node_modules/@middy/core/index.js:43:12)\n at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)"
}
}
}
} Thanks for the support! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, thank you for opening this discussion. The log you're seeing is the formatted version of the error. When you log an error using Logger, its key get formatted into a JSON-serializable object, which includes the stack trace. At runtime, both You can verify that this is the case with code similar to this: import { z, ZodError } from 'zod';
import { Logger } from '@aws-lambda-powertools/logger';
import { parser } from '@aws-lambda-powertools/parser/middleware';
import { ParseError } from '@aws-lambda-powertools/parser';
import middy from '@middy/core';
import type { Context } from 'aws-lambda';
const logger = new Logger({ logLevel: 'debug' });
export const handler = middy()
.use(
parser({
schema: z.object({
foo: z.string(),
}),
safeParse: true,
})
)
.handler(async (event) => {
if (event.success === false) {
logger.debug('event.error instanceof ParseError', {
instanceOf: event.error instanceof ParseError,
});
logger.debug('event.error.cause instanceof ZodError', {
instanceOf: event.error.cause instanceof ZodError,
});
}
return {
statusCode: 200,
body: JSON.stringify('Hello, World!'),
};
}); which will log these two debug logs (notice the
|
Beta Was this translation helpful? Give feedback.
Hi, thank you for opening this discussion.
The log you're seeing is the formatted version of the error. When you log an error using Logger, its key get formatted into a JSON-serializable object, which includes the stack trace.
At runtime, both
event.error
andevent.error.cause
are indeed errors and instances of respectivelyParseError
&ZodError
which you can work with.You can verify that this is the case with code similar to this: