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.

improve handling of non-object errors thrown in endpoint handlers

See original GitHub issue

oh boy, i just spent the last couple hours running in circles trying to figure this one out. my API endpoint was returning {"status":"success"} with no data, which was then causing errors when my generated client was assuming "status":"success" means data should be present

the culprit is this:

  1. the error is only passed to the result handler if it’s instanceof Error - https://github.com/RobinTail/express-zod-api/blob/721a41e2f762a9f0a524fc111549c461d9057462/src/endpoint.ts#L333-L335
  2. if there’s no error passed to the result handler, it returns a 200 and a success status regardless of whether there’s any real data or not - https://github.com/RobinTail/express-zod-api/blob/721a41e2f762a9f0a524fc111549c461d9057462/src/result-handler.ts#L83-L89
  3. turns out that running prettier programmatically throws a string 😅 - this was in my code

I was about to open a PR up but I’m actually not really sure how you want to handle this since this will be a breaking change, albeit a needed one IMO (probably before the v8 release?). the two obvious choices seem to be:

  1. widening the typings on the ResultHandler so you can pass in any kind of error (thus passing the onus to the default handler or the downstream consumer to handle more error cases)
  2. just stringify any non-object error and create an Error object out of it

the latter seems way easier and with less breaking changes, whereas the former will certainly break any custom result handler implementations. but if you’re gonna make a breaking change anyway, I think the former is the way to go because if you just coerce everything into a string then it gives downstream consumers no way to implement custom error handling based on the type of the error

so my suggestion is to change the error type in the ResultHandler to unknown, then check if it’s !== undefined (or != null to handle both undefined & null?) rather than just checking if it’s falsey like you currently do. but not sure if you’ve got some other thoughts on how to better handle this?

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
RobinTailcommented, Sep 8, 2022

🚀 The fix released in version 7.9.2. Thank you so much @rayzr522

1reaction
RobinTailcommented, Sep 6, 2022

I think I’m going to go with the second approach

  1. just stringify any non-object error and create an Error object out of it

There is no need to widen the signature of ResultHandler, we just need to handle more throwing cases in endpoints and middlewares.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best Practices for REST API Error Handling - Baeldung
In this tutorial, we'll learn about some of the best practices for handling REST API errors, including useful approaches for providing users ...
Read more >
Best Practices for Node.js Error-handling - Toptal
Using Node.js built-in Error object is a good practice because it includes intuitive and clear information about errors like StackTrace, which most developers ......
Read more >
Web API Error Handling: How To Make Debugging Easier
Whether you're the consumer or producer of an API, you've no doubt seen that good error handling can make debugging easier.
Read more >
Global Error Handling in ASP.NET Core Web API - Code Maze
The exception handling features help us deal with the unforeseen errors which could appear in our code. To handle exceptions we can use...
Read more >
Full Stack Error Handling with GraphQL and Apollo
networkError : Errors that are thrown outside of your resolvers. If networkError is present in your response, it means your entire query was ......
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