Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addMonitor run after addResponseTransform ? #265

Open
zeing opened this issue Jul 1, 2021 · 1 comment
Open

addMonitor run after addResponseTransform ? #265

zeing opened this issue Jul 1, 2021 · 1 comment

Comments

@zeing
Copy link

zeing commented Jul 1, 2021

I have addResponseTransform for transform data form respond and handle error message then throw to catch later


api.addResponseTransform((response) => {
  if (response.status >= 400 || !response.ok) {
    const error = new Error(
      response.data?.message || response.originalError.message || response.problem,
    )
    error.status = response.status
    error.response = response
    // throw error
  }

  if (response.ok && response.data.code === 0) {
    // just mutate the data to what you want.
    response.data = response.data.data
  } else {
    const error = new Error(response.data?.message)
    error.status = response.data?.code
    error.response = response
    throw error
  }

  if (response.ok) {
    if (response.data.code === 0) response.data = response.data.data
    else {
      response.ok = false
      response.problem = response.data?.message
      response.status = response.data?.code
    }
  }
})

And I have addMonitor for handle status like 401 for auto-logout

const naviMonitor = (response) => {
    if (response.status === 401) {
      console.warn('Unhandled session expiration')
      signOut()
    }

  }
  api.addMonitor(naviMonitor)

but don't work when addResponseTransform have throw error function

what do you suggest me? I have a way to fix it that I move throw error into each of functions which use API

ie.

api.addResponseTransform((response) => {
  if (response.ok) {
    if (response.data.code === 0) response.data = response.data.data
    else {
      response.ok = false
      response.problem = response.data?.message
      response.status = response.data?.code
    }
  }
})
export const approve = (payload = {}) => {
  return api
    .post(`/approve`,payload)
    .then((response) => {
      if (!response.ok) {
        const error = new Error(response.data?.message)
        error.status = response.data?.code
        error.response = response
        throw error
      }
    })
}
@chakrihacker
Copy link
Collaborator

If you throw an error it will go to the catch

const naviMonitor = (response) => {
    try{
    if (response.status === 401) {
      console.warn('Unhandled session expiration')
      signOut()
    }
    } catch(e) {
        console.log(e) // the error you throwed at transformation
   }
  }
  api.addMonitor(naviMonitor)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants