Rejecting responses with HTTP failure codes has unfortunate consequences
See original GitHub issueThanks for making restful-react
! It’s a great library and tool (I’m using it to generate code from an OpenAPI spec).
I really like how useGet
works:
- The promise never rejects, so you can
await
on it safely without it throwing an error - It’s easy to check if
data
orerror
is defined, and then act on it. (If I was to change anything, there are some cool things you can do with conditional types, so that the compiler can automatically infer thatdata
orerror
is defined, but never both)
However, useMutate
has some drawbacks:
- The promise can throw, meaning that
await
is not safe without appending an empty.then
clause:
Furthermore, even though theawait useMutate("path", props).catch(() => { /* pass */})
error
field fromuseMutate
is of typeGetDataError<TError>
, the compiler cannot infer that in the promise rejection clause, because failed promises can contain anything. - There’s no
data
field, to accompanyerror
. That means that I’ll have to use two mechanisms:await
onmutate
to read the result of my operation, and checkerror
for any failures.
I’d think it would make a lot of sense for useMutate
to be more similar to useGet
:
- The promise returned by
mutate
should never reject, and also resolve tovoid
- A new field
data: TData | null
should be added, to mirror the existingerror
.
Do you have any thoughts on this? I’d be happy to contribute some code to implement this functionality.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Ignore HTTP Client Errors At Your Peril
All of this means that somebody is seriously misinterpreting what your server expects to receive. That usually means a major bug in either...
Read more >HTTP response status codes - MDN Web Docs - Mozilla
HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
Read more >HTTP Status Codes and SEO: what you need to know
HTTP status codes are three-digit responses a server returns at the request of a client (a browser or search engine).
Read more >RFC 7231: Hypertext Transfer Protocol (HTTP/1.1)
If the request method is GET or HEAD and the response status code is 200 (OK), ... Failure to do so will result...
Read more >Best Practices for API Error Handling - Nordic APIs
Error codes in the response stage of an API is the fundamental way in ... There are certain implications for each of the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
What do you think of this?
It very closely mimics
useGet
, which I think is good for two reasons:useGet
has a very pleasant APIThank you so much, @torkelrogstad!