requestError.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import {ResponseMeta} from 'sentry/api';
  2. import {sanitizePath} from './sanitizePath';
  3. interface ErrorOptionsObject {
  4. cause: Error;
  5. }
  6. export default class RequestError extends Error {
  7. responseText?: string;
  8. responseJSON?: any;
  9. status?: number;
  10. statusText?: string;
  11. constructor(method: string | undefined, path: string, options: ErrorOptionsObject) {
  12. super(`${method || 'GET'} "${sanitizePath(path)}"`, options);
  13. this.name = 'RequestError';
  14. Object.setPrototypeOf(this, new.target.prototype);
  15. }
  16. /**
  17. * Updates Error with XHR response
  18. */
  19. setResponse(resp: ResponseMeta) {
  20. if (resp) {
  21. this.setMessage(
  22. `${this.message} ${typeof resp.status === 'number' ? resp.status : 'n/a'}`
  23. );
  24. // Some callback handlers expect these properties on the error object
  25. if (resp.responseText) {
  26. this.responseText = resp.responseText;
  27. }
  28. if (resp.responseJSON) {
  29. this.responseJSON = resp.responseJSON;
  30. }
  31. this.status = resp.status;
  32. this.statusText = resp.statusText;
  33. }
  34. }
  35. setMessage(message: string) {
  36. this.message = message;
  37. }
  38. setName(name: string) {
  39. this.name = name;
  40. }
  41. }