requestError.tsx 1.5 KB

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