Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bug: onError not reporting original errorย #6977

@luisrudge

Description

@luisrudge

Provide environment information

  System:
    OS: macOS 26.0.1
    CPU: (16) arm64 Apple M3 Max
    Memory: 7.70 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.13.1 - /Users/xxx/.volta/tools/image/node/22.13.1/bin/node
    Yarn: 1.22.22 - /Users/xxx/.volta/tools/image/yarn/1.22.22/bin/yarn
    npm: 11.3.0 - /Users/xxx/.volta/tools/image/npm/11.3.0/bin/npm
    bun: 1.3.0 - /Users/xxx/.bun/bin/bun
  Browsers:
    Chrome: 141.0.7390.108
    Edge: 141.0.3537.71
    Firefox: 142.0.1
    Safari: 26.0.1
  npmPackages:
    @trpc/client: ^11.5.1 => 11.5.1 
    @trpc/server: ^11.5.1 => 11.5.1 
    react: ^19.1.1 => 19.1.1 
    typescript: ^5.9.2 => 5.9.2 

Describe the bug

I tried multiple things to make sure that Datadog sees the actual error being thrown, but no matter what I did, the error is always the same (screenshot attached).

This is my current logger (it was much simpler before, but I tried to recursively find the cause error with no luck).

import { TRPCError } from '@trpc/server';
import * as trpcExpress from '@trpc/server/adapters/express';
import express from 'express';
import logger from 'server/logger';
import { createContext } from 'server/trpc/context';
import { trpcRouter } from 'server/trpc/mainRouter';

function getLoggableError(error: unknown): unknown {
  if (error instanceof Error && error.cause instanceof Error) {
    return getLoggableError(error.cause);
  }
  return error;
}

export function initTRPC(app: express.Application) {
  app.use(
    '/api/trpc',
    trpcExpress.createExpressMiddleware({
      router: trpcRouter,
      createContext: createContext,
      onError: ({ error, path, type, input }) => {
        if (error instanceof TRPCError && error.code === 'INTERNAL_SERVER_ERROR') {
          const loggable = getLoggableError(error);
          logger.error(loggable, {
            path,
            type,
            input: type === 'query' ? input : undefined,
            trpcCode: error.code,
            trpcMessage: error.message,
            trpcStack: error.stack,
          });
        }
      },
    })
  );
}

and this is how the error is being reported:

Image

so basically I have no way of knowing what the actual error is unless I try/catch the procedure itself

Link to reproduction

n/a

To reproduce

n/a

Additional information

No response

๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Contributing

  • ๐Ÿ™‹โ€โ™‚๏ธ Yes, I'd be down to file a PR fixing this bug!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions