T
tRPC

❓-help

Missing keys in TRPCError when returning as JSON

Mmoloch10/4/2023
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" });
}

Looking for more? Join the community!