handleXhrErrorResponse.spec.jsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import * as Sentry from '@sentry/react';
  2. import handleXhrErrorResponse from 'sentry/utils/handleXhrErrorResponse';
  3. describe('handleXhrErrorResponse', function () {
  4. const stringError = {responseJSON: {detail: 'Error'}, status: 400};
  5. const objError = {
  6. status: 400,
  7. responseJSON: {detail: {code: 'api-err-code', message: 'Error message'}},
  8. };
  9. beforeEach(function () {
  10. jest.clearAllMocks();
  11. });
  12. it('does nothing if we have invalid response', function () {
  13. handleXhrErrorResponse('')(null);
  14. expect(Sentry.captureException).not.toHaveBeenCalled();
  15. handleXhrErrorResponse('')({});
  16. expect(Sentry.captureException).not.toHaveBeenCalled();
  17. });
  18. it('captures an exception to sdk when `resp.detail` is a string', function () {
  19. handleXhrErrorResponse('String error')(stringError);
  20. expect(Sentry.captureException).toHaveBeenCalledWith(new Error('String error'));
  21. });
  22. it('captures an exception to sdk when `resp.detail` is an object', function () {
  23. handleXhrErrorResponse('Object error')(objError);
  24. expect(Sentry.captureException).toHaveBeenCalledWith(new Error('Object error'));
  25. });
  26. it('ignores `sudo-required` errors', function () {
  27. handleXhrErrorResponse('Sudo required error')({
  28. status: 401,
  29. responseJSON: {
  30. detail: {
  31. code: 'sudo-required',
  32. detail: 'Sudo required',
  33. },
  34. },
  35. });
  36. expect(Sentry.captureException).not.toHaveBeenCalled();
  37. });
  38. });