handleXhrErrorResponse.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import * as Sentry from '@sentry/react';
  2. import {ResponseMeta} from 'sentry/api';
  3. export default function handleXhrErrorResponse(message: string) {
  4. return (resp: ResponseMeta) => {
  5. if (!resp) {
  6. return;
  7. }
  8. if (!resp.responseJSON) {
  9. return;
  10. }
  11. const {responseJSON} = resp;
  12. // If this is a string then just capture it as error
  13. if (typeof responseJSON.detail === 'string') {
  14. Sentry.withScope(scope => {
  15. scope.setExtra('status', resp.status);
  16. scope.setExtra('detail', responseJSON.detail);
  17. Sentry.captureException(new Error(message));
  18. });
  19. return;
  20. }
  21. // Ignore sudo-required errors
  22. if (responseJSON.detail && responseJSON.detail.code === 'sudo-required') {
  23. return;
  24. }
  25. if (responseJSON.detail && typeof responseJSON.detail.message === 'string') {
  26. Sentry.withScope(scope => {
  27. scope.setExtra('status', resp.status);
  28. scope.setExtra('detail', responseJSON.detail);
  29. scope.setExtra('code', responseJSON.detail.code);
  30. Sentry.captureException(new Error(message));
  31. });
  32. return;
  33. }
  34. };
  35. }