Closed alexdemers closed 4 years ago
Hi @alexdemers,
My question is, why doesn't my catcher catch the 422 error code? Is there something I don't understand?
Hmm I see some issue with the way you wrote the catcher, but it would not explain why the exception does not get caught at all.
// async seems unnecessary - more details below
const accountPasswordCatcher = async (error: WretcherError) => {
/*
This line looks incorrect:
- by default, wretch assumes that an error body is text and only defines the error.text property.
you can add the .errorType('json') to the method chain change the behaviour
- error.text and error.json are not functions, and contain respectively a String or a parsed Object
- since the text and json properties are already populated, there is no need to use await
*/
const response = await error.json();
if ('account_password' in response.errors) {
console.log('test');
}
};
I tried to reproduce on my end but it seems to work fine, here is the self-contained html file I used to test:
<!DOCTYPE html>
<html>
<head>
<title>Wretch Test</title>
<script type="module">
import wretch from 'https://unpkg.com/wretch/dist/index.js'
const middleware = next => (url, opts) => {
console.log('Middleware…')
return next(url, opts)
}
const catcher = error => {
console.log('Caught!')
return error.json
}
const client = wretch()
.polyfills({ fetch: window.fetch })
.url('https://httpstat.us')
.headers({ Accept: 'application/json' })
.catcher(422, catcher)
.errorType('json')
.middlewares([middleware])
client.url('/422').get().json().then(response => {
console.log(response)
})
</script>
</head>
<body></body>
</html>
Prints in the browser console:
I defined a middleware to insert a XSRF token, which works and and a catcher when the server returns a 422 status. The catcher never receives the response.
Usage:
But I get an unresolved error:
Unhandled Rejection (Error): {"message":"The given data was invalid.","errors":{"account_password":["The account password field is required."]}}
My question is, why doesn't my catcher catch the 422 error code? Is there something I don't understand?