volks
volks17mo ago

Cannot read properties of undefined (reading 'data') of res.error.data, when trpc errors out

Hello everyone, I am using @trpc/react-query alongside trpc for express, and I am experiencing a crash when I have an error
debug: Server listening on port: 3000 at development mode
error: ERROR OCCURED: '[\n' +
' {\n' +
' "code": "invalid_type",\n' +
' "expected": "object",\n' +
' "received": "undefined",\n' +
' "path": [],\n' +
' "message": "Required"\n' +
' }\n' +
']'
error: ERROR OCCURED: "Cannot read properties of undefined (reading 'data')"
/work/company/monorepo/node_modules/@trpc/server/dist/config-200d5226.js:63
const data = res.error.data;
^

TypeError: Cannot read properties of undefined (reading 'data')
at /work/company/monorepo/node_modules/@trpc/server/dist/config-200d5226.js:63:36
at Array.map (<anonymous>)
at Object.getHTTPStatusCode (/work/company/monorepo/node_modules/@trpc/server/dist/config-200d5226.js:61:38)
at endResponse (/work/company/monorepo/node_modules/@trpc/server/dist/resolveHTTPResponse-e94bc3b5.js:46:29)
at Object.resolveHTTPResponse (/work/company/monorepo/node_modules/@trpc/server/dist/resolveHTTPResponse-e94bc3b5.js:194:16)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Object.nodeHTTPRequestHandler (/work/company/monorepo/node_modules/@trpc/server/dist/nodeHTTPRequestHandler-b0d356ac.js:52:20)
at async /work/company/monorepo/node_modules/@trpc/server/dist/adapters/express.js:16:9
[nodemon] app crashed - waiting for file changes before starting...
debug: Server listening on port: 3000 at development mode
error: ERROR OCCURED: '[\n' +
' {\n' +
' "code": "invalid_type",\n' +
' "expected": "object",\n' +
' "received": "undefined",\n' +
' "path": [],\n' +
' "message": "Required"\n' +
' }\n' +
']'
error: ERROR OCCURED: "Cannot read properties of undefined (reading 'data')"
/work/company/monorepo/node_modules/@trpc/server/dist/config-200d5226.js:63
const data = res.error.data;
^

TypeError: Cannot read properties of undefined (reading 'data')
at /work/company/monorepo/node_modules/@trpc/server/dist/config-200d5226.js:63:36
at Array.map (<anonymous>)
at Object.getHTTPStatusCode (/work/company/monorepo/node_modules/@trpc/server/dist/config-200d5226.js:61:38)
at endResponse (/work/company/monorepo/node_modules/@trpc/server/dist/resolveHTTPResponse-e94bc3b5.js:46:29)
at Object.resolveHTTPResponse (/work/company/monorepo/node_modules/@trpc/server/dist/resolveHTTPResponse-e94bc3b5.js:194:16)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Object.nodeHTTPRequestHandler (/work/company/monorepo/node_modules/@trpc/server/dist/nodeHTTPRequestHandler-b0d356ac.js:52:20)
at async /work/company/monorepo/node_modules/@trpc/server/dist/adapters/express.js:16:9
[nodemon] app crashed - waiting for file changes before starting...
2 Replies
volks
volks17mo ago
I am running the latest version 10.10.0 This is the part from trpc that is erroring out
function getHTTPStatusCode(json) {
const arr = Array.isArray(json) ? json : [
json
];
const httpStatuses = new Set(arr.map((res)=>{
if ('error' in res) {
const data = res.error.data;
if (typeof data.httpStatus === 'number') {
return data.httpStatus;
}
const code = TRPC_ERROR_CODES_BY_NUMBER[res.error.code];
return getStatusCodeFromKey(code);
}
return 200;
}));
if (httpStatuses.size !== 1) {
return 207;
}
const httpStatus = httpStatuses.values().next().value;
return httpStatus;
}
function getHTTPStatusCode(json) {
const arr = Array.isArray(json) ? json : [
json
];
const httpStatuses = new Set(arr.map((res)=>{
if ('error' in res) {
const data = res.error.data;
if (typeof data.httpStatus === 'number') {
return data.httpStatus;
}
const code = TRPC_ERROR_CODES_BY_NUMBER[res.error.code];
return getStatusCodeFromKey(code);
}
return 200;
}));
if (httpStatuses.size !== 1) {
return 207;
}
const httpStatus = httpStatuses.values().next().value;
return httpStatus;
}
Here is my express trpc setup
export const trpc = initTRPC.context<Context>().create({
transformer: superjson,
errorFormatter: ({ error, type, path, input, ctx }) => {
const logger: Logger = Container.get("logger");
//TODO sentry or similar product for errors
logger.error("ERROR OCCURED: %o", error.message);
logger.error("ERROR DATA: %o", (error as any).data);
},
});

app.use(
"/trpc",
trpcExpress.createExpressMiddleware({
router: appRouter,
createContext: sessionContext,
})
);


const accountCreate = trpc.procedure
.input(
z.object({
email: z.string().min(1).email(),
})
)
.mutation(async ({ input }) => {

export const userRouter = trpc.router({
accountCreate,
})
export const trpc = initTRPC.context<Context>().create({
transformer: superjson,
errorFormatter: ({ error, type, path, input, ctx }) => {
const logger: Logger = Container.get("logger");
//TODO sentry or similar product for errors
logger.error("ERROR OCCURED: %o", error.message);
logger.error("ERROR DATA: %o", (error as any).data);
},
});

app.use(
"/trpc",
trpcExpress.createExpressMiddleware({
router: appRouter,
createContext: sessionContext,
})
);


const accountCreate = trpc.procedure
.input(
z.object({
email: z.string().min(1).email(),
})
)
.mutation(async ({ input }) => {

export const userRouter = trpc.router({
accountCreate,
})
Also not even sure about the zod error at the moment (not sure what is undefined, but thats for later debugging)
Alex / KATT 🐱
Alex / KATT 🐱17mo ago
Could you create an issue? If it crashes there it looks like something we should fix or provide a better error for