handleXhrErrorResponse.tsx 1.1 KB

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