fetch-plus icon indicating copy to clipboard operation
fetch-plus copied to clipboard

Support async error handlers

Open JakeCoxon opened this issue 8 years ago • 0 comments

I needed this because I want to be able to do get the error response with errorResponse.text() and put it in the error message. It could also help in other middleware if they need to fire off a second request the first one fails etc. It should be backwards compatible. Usages:

error: (resp) => {
  console.error(resp);
  throw resp;
}
error: (resp) => {
  console.error(resp);
  return Promise.reject(resp);
}
error: (resp) => {
  console.error(resp);
  return resp.text().then(text => { 
    throw { ...resp, errorText: text };
  });
}
error: (resp) => {
  console.error(resp);
  return new Promise(resolve => {
    // Resolve after 1 second
    setTimeout(() => {
      resolve({ something: {} });
    }, 1000);
  });
}

JakeCoxon avatar Jan 09 '17 11:01 JakeCoxon