umijs / umi-request

A request tool based on fetch.
2.2k stars 336 forks source link

how do you skip error handlers for special cases #227

Open aquibbaig opened 3 years ago

aquibbaig commented 3 years ago

We have a default error handler as follows.

const { response = {} } = error;
  const errortext = codeMessage[response.status] || response.statusText;
  const { status } = response;

  notification.error({
    message: `Request Error ${status}`,
    description: errortext,
  });

  if (status === 403) {
    router.push('/exception/403');
    return;
  }
  if (status === 404) {
    router.push('/exception/404');
    return;
  }
  if (status <= 504 && status >= 500) {
    router.push('/exception/500');
    return;
  }
  if (status >= 405 && status < 422) {
    router.push('/exception/404');
  }

We need to skip the error handler for special cases and handle exceptions ourselves. It is possible with "plugin-request" provided by umi, is it supported in "umi-request" by any means?

aquibbaig commented 3 years ago

Also, a good way to achieve this is by conditionally checking for response.ok, but when I return from a request.post(), it should return a response object but it returns only the body of the response. For example:

(1)  callee
const response = await request.post(ep, options);
console.log(response)
// returns only the body as {"message": "Ilorem ipsum"}, cannot find the status, statusText and other parameters.

(2) interceptor
request.interceptors.response.use(response => {
  console.log(response)
  // prints out the actual response object with all the parameters
  return response;
  // returns to the callee in (1) but logs only the body, not parameters
})