prepare
- rewrite fetch to catch httpcode
const httpCodeFetch = async ({ url, config = initialConfig }) => {
const res = await fetch(url, config)
if (res.status === 204) return {}
if (res.ok) {
return res.json()
} else {
const obj = await res.json()
// new Error(res.statusText)
throw { status: res.status, ...obj }
}
}
- common code
httpCodeFetch(url,...).then(res => {
do(res)
}).catch(err =>{
if(err.status === 401) do401(err)
error(err)
})
2.try catch
try{
const res = httpCodeFetch()
do(res)
}catch(err){
if(err.status === 401) do401(err)
error(err)
}