question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error handling best practices

See original GitHub issue

Hello 👋 First of all thank you so much for this amazing project, it’s awesome to use and we’re adopting it for some of our public facing services as an alternative to GraphQL at utility-company scale

Question about error handling best practices; the JSON-RPC spec includes the following description:

The error codes from and including -32768 to -32000 are reserved for pre-defined errors. Any code within this range, but not defined explicitly below is reserved for future use. The error codes are nearly the same as those suggested for XML-RPC at the following url: http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php

The remainder of the space is available for application defined errors.

https://www.jsonrpc.org/specification#error_object

However, TRPC errors appear limited to the subset defined in the spec:

image

Does this mean that best practices for error handling is to return them as a standard result of the request instead of as a special error thrown:

// I.e. this
return { error: "CUSTOM_ERROR" }; // This would also now be typed correctly on the client
// Instead of this
throw new TRPCError({ code: "CUSTOM_ERROR" }); // This is a type error

Once again, thank you so much for this amazing library!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
KATTcommented, Jul 11, 2021

Again, check out https://trpc.io/docs/error-formatting

It might be what you are looking for.

1reaction
sam3dcommented, Jul 11, 2021

I considered doing that but yeah exactly I didn’t want to lose implicit type safety. I’ll continue to see if I can wrap mutateAsync to return { data, errors } and also add an options object with something like axios’s validateStatus. Thank you for your help (and again for making this awesome library!) ❤️

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Error Handling Done Right - Medium
A lot of design strategy is to have each component independent and explicit. For error handling, if we can't handle it by ourselves,...
Read more >
Best Practices for exceptions - .NET - Microsoft Learn
Use try/catch/finally blocks to recover from errors or release resources · Handle common conditions without throwing exceptions · Design classes ...
Read more >
Clean Code and the Art of Exception Handling - Toptal
Exception Handling : It's a Good Thing · Always create your own ApplicationError hierarchy · Never rescue Exception · Never rescue more exceptions...
Read more >
Best practices for error catching and handling
Best practices for error catching and handling · More videos on YouTube · Be very thorough with your error checking · Check for...
Read more >
Java Exception handling best practices - TheServerSide.com
Top 10 Java Exception handling best practices · Be careful what you log. · Don't bury thrown exceptions. · Use a global Exception...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found