0

i am fetching data from a sever and sometime the request take so long and the sever responds with a 408 status so my code looks like this

export class ResponseError extends Error {
  public response: Response;

  constructor(response: Response) {
    super(response.statusText);
    this.response = response;
  }
function parseJSON(response: Response) {
  if (response.status === 204 || response.status === 205) {
    return null;
  }
  return response.json();
}
function checkStatus(response: Response) {
  if (response.status >= 200 && response.status < 300) {
    return response;
  }
  const error = new ResponseError(response);
  error.response = response;
  throw error;
}
export async function request(
  url: string,
  options?: RequestInit,
): Promise<{} | { err: ResponseError }> {
  const fetchResponse = await fetch(url, options);
  const response = checkStatus(fetchResponse);
  return parseJSON(response);
}

how can i retry the request if the response status code 408 is received

  • See if this works: https://stackoverflow.com/questions/46175660/fetch-retry-request-on-failure – Meow Jun 30 '22 at 00:15

1 Answers1

0

If you use axios in your code for fetching api you can try below code

axios.create().interceptors.response.use(
        response => response,
        error => {
            if (error.response.status === 503){
                axios.create().get("YOUR_URL").then((response)=>{
                })
            }
        }
    )
sajadab
  • 383
  • 2
  • 11