requestError.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. export default class RequestError extends Error {
  2. responseText?: string;
  3. responseJSON?: any;
  4. status?: number;
  5. statusText?: string;
  6. constructor(method: string | undefined, path: string) {
  7. super(`${method || 'GET'} ${path}`);
  8. this.name = 'RequestError';
  9. Object.setPrototypeOf(this, new.target.prototype);
  10. }
  11. /**
  12. * Updates Error with XHR response
  13. */
  14. setResponse(resp: JQueryXHR) {
  15. if (resp) {
  16. this.setMessage(
  17. `${this.message} ${typeof resp.status === 'number' ? resp.status : 'n/a'}`
  18. );
  19. // Some callback handlers expect these properties on the error object
  20. if (resp.responseText) {
  21. this.responseText = resp.responseText;
  22. }
  23. if (resp.responseJSON) {
  24. this.responseJSON = resp.responseJSON;
  25. }
  26. this.status = resp.status;
  27. this.statusText = resp.statusText;
  28. }
  29. }
  30. setMessage(message: string) {
  31. this.message = message;
  32. }
  33. setStack(newStack: string) {
  34. this.stack = newStack;
  35. }
  36. setName(name: string) {
  37. this.name = name;
  38. }
  39. removeFrames(numLinesToRemove) {
  40. // Drop some frames so stack trace starts at callsite
  41. //
  42. // Note that babel will add a call to support extending Error object
  43. // Old browsers may not have stack trace
  44. if (!this.stack) {
  45. return;
  46. }
  47. const lines = this.stack.split('\n');
  48. this.stack = [lines[0], ...lines.slice(numLinesToRemove)].join('\n');
  49. }
  50. }