handleXhrErrorResponse.spec.jsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import handleXhrErrorResponse from 'app/utils/handleXhrErrorResponse';
  2. import * as Sentry from '@sentry/browser';
  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. });