createRequestError.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {ResponseMeta} from 'sentry/api';
  2. import RequestError from './requestError';
  3. const ERROR_MAP = {
  4. 0: 'CancelledError',
  5. 400: 'BadRequestError',
  6. 401: 'UnauthorizedError',
  7. 403: 'ForbiddenError',
  8. 404: 'NotFoundError',
  9. 426: 'UpgradeRequiredError',
  10. 429: 'TooManyRequestsError',
  11. 500: 'InternalServerError',
  12. 501: 'NotImplementedError',
  13. 502: 'BadGatewayError',
  14. 503: 'ServiceUnavailableError',
  15. 504: 'GatewayTimeoutError',
  16. };
  17. /**
  18. * Create a RequestError whose name is equal to HTTP status text defined above
  19. *
  20. * @param {Object} resp A XHR response object
  21. * @param {String} stack The stack trace to use. Helpful for async calls and we want to preserve a different stack.
  22. */
  23. export default function createRequestError(
  24. resp: ResponseMeta,
  25. cause: Error,
  26. method: 'POST' | 'GET' | 'DELETE' | 'PUT' | undefined,
  27. path: string
  28. ) {
  29. const err = new RequestError(method, path, {cause});
  30. if (resp) {
  31. const errorName = ERROR_MAP[resp.status];
  32. if (errorName) {
  33. err.setName(errorName);
  34. }
  35. err.setResponse(resp);
  36. }
  37. return err;
  38. }