tRPCttRPC
Powered by
molochM
tRPC•3y ago
moloch

Missing keys in TRPCError when returning as JSON

Hey all! I am returning a TRPCError from a custom TRPC server but only the code and name keys seems to be serialized and returned to the client. It's quite strange. If I directly return a key such as error.message I get the string no problem, but if I return the entire error object I only recieve error.code and error.name . Any idea why this is?

Here's a little code snippet:
catch (error) {
  if (error instanceof TRPCError) {
    console.log(Object.keys(error)) // [ 'code', 'name' ]
    console.log(error.cause) // The actual cause of the error
    console.log(error.message) // The error message!!!
    // An error from tRPC occured
    const httpCode = getHTTPStatusCodeFromError(error);
    return res.status(httpCode).json({ ...error });
  }
  // Another error occured
  console.error(error);
  res.status(500).json({ message: "Internal server error" });
}
catch (error) {
  if (error instanceof TRPCError) {
    console.log(Object.keys(error)) // [ 'code', 'name' ]
    console.log(error.cause) // The actual cause of the error
    console.log(error.message) // The error message!!!
    // An error from tRPC occured
    const httpCode = getHTTPStatusCodeFromError(error);
    return res.status(httpCode).json({ ...error });
  }
  // Another error occured
  console.error(error);
  res.status(500).json({ message: "Internal server error" });
}
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

TRPCError that has TRPCError as cause
TkDodo 🔮TTkDodo 🔮 / ❓-help
3y ago
TRPCError: Missing content-type header
BenfaBBenfa / ❓-help
8mo ago
Client receives 200 status when throwing TRPCError
BumblebeeBBumblebee / ❓-help
14mo ago
Customizing the TRPCError type
Emmie PäivärintaEEmmie Päivärinta / ❓-help
6mo ago