ignite icon indicating copy to clipboard operation
ignite copied to clipboard

How we can interceptor Apisauce for catching error instead of doing it in every request ?

Open phanvungoc96 opened this issue 4 years ago • 3 comments

What's going on? I want to you reference API interceptor of Axios to catch error from Server. But I don't know how to do?

Steps to reproduce

  1. I see this guild https://github.com/axios/axios#interceptors
  2. I tried using it on setup Api but It be not working. I was stuck every request. It was look like ascending loop. Please help me! private api: Api constructor(api: Api) { this.api = api api.apisauce.axiosInstance.interceptors.request.use((c) => { console.log(' sta ',c) return c }) }

ignite doctor results:

phanvungoc96 avatar Jun 03 '21 09:06 phanvungoc96

You can use ignites general api problem file "api-problem.ts"'s function in api.ts in setup() functions. If you want to show alert ot toast you can edit api-problem.ts file as per your requirement.

this.apisauce.addAsyncResponseTransform(response => async () => {
      if (!response.ok) {
        const problem = getGeneralApiProblem(response)
      }
    })

MadhavNasit avatar Jun 03 '21 16:06 MadhavNasit

Depending on server setup the server could send a 500 when you have a "Internal Server Error" then you can handle it in the interceptor:

 this.apisauce.addMonitor(monitor => {
      if (monitor.status === 500) {
        __DEV__ && console.tron.log('Server error!')
      }
})

nirre7 avatar Jun 07 '21 05:06 nirre7

DEV && console.tron.log('Server error!')

it is not working

HAFDIAHMED avatar Feb 16 '22 07:02 HAFDIAHMED

There has been a lot of changes since this issue last had activity. This is going to be closed for now, but please re-open if this still needs attention.

joshuayoes avatar Oct 04 '22 17:10 joshuayoes