You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In openapi-fetch and by extension openapi-react-query I can't find a satisfying way to handle APIs which respond successfully (from the interface's perspective) with a non-ok status code, such as for example 404 where an entity doesn't exist yet but the request was otherwise valid.
Should this respond 404 isError would be unhelpful. error itself, even if typed, may not offer sufficient information to establish whether it was the happy path I described or an actual issue. Additionally, because the 404 isn't necessarily a failure case, we need to disable retries.
Essentially openapi-fetch firmly treats non-ok responses as errors, and this is particularly problematic in openapi-react-query where we no longer have access to the response or status code. Even if we did have access to the status code, it won't play nicely with the hook's design, where the 404 would still be classed as an error, affecting all sorts of other things such as retries.
I imagine this could be solved if there was some way to specify that for this request a 404 isn't an error, or to otherwise have a hook that allows you to sort of catch the error and either return the response type or re-throw as a fallback. Something like:
constComponent=()=>{const{ data, error, isError, ...etc}=useQuery('get','/foo',{catch: (e)=>{if(e.response.status===404)return[]// this would need to match the shape of `data`throwe},},)}
Or:
constComponent=()=>{const{ data, error, isError, ...etc}=useQuery('get','/foo',{// `data` would remain undefinedfilterError: (e)=>e.response.status!==404,},)}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
In openapi-fetch and by extension openapi-react-query I can't find a satisfying way to handle APIs which respond successfully (from the interface's perspective) with a non-ok status code, such as for example 404 where an entity doesn't exist yet but the request was otherwise valid.
Should this respond 404
isError
would be unhelpful.error
itself, even if typed, may not offer sufficient information to establish whether it was the happy path I described or an actual issue. Additionally, because the 404 isn't necessarily a failure case, we need to disable retries.Essentially openapi-fetch firmly treats non-ok responses as errors, and this is particularly problematic in openapi-react-query where we no longer have access to the response or status code. Even if we did have access to the status code, it won't play nicely with the hook's design, where the 404 would still be classed as an error, affecting all sorts of other things such as retries.
I imagine this could be solved if there was some way to specify that for this request a 404 isn't an error, or to otherwise have a hook that allows you to sort of catch the error and either return the response type or re-throw as a fallback. Something like:
Or:
Related: #1618
Beta Was this translation helpful? Give feedback.
All reactions