handleError.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import {t} from 'sentry/locale';
  2. export enum ErrorType {
  3. UNKNOWN = 'unknown',
  4. INVALID_SELECTOR = 'invalid-selector',
  5. REGEX_PARSE = 'regex-parse',
  6. }
  7. type Error = {
  8. message: string;
  9. type: ErrorType;
  10. };
  11. type ResponseFields = 'relayPiiConfig';
  12. type ResponseError = {
  13. responseJSON?: Record<ResponseFields, Array<string>>;
  14. };
  15. function handleError(error: ResponseError): Error {
  16. const errorMessage = error.responseJSON?.relayPiiConfig[0];
  17. if (!errorMessage) {
  18. return {
  19. type: ErrorType.UNKNOWN,
  20. message: t('Unknown error occurred while saving data scrubbing rule'),
  21. };
  22. }
  23. if (errorMessage.startsWith('invalid selector: ')) {
  24. for (const line of errorMessage.split('\n')) {
  25. if (line.startsWith('1 | ')) {
  26. const selector = line.slice(3);
  27. return {
  28. type: ErrorType.INVALID_SELECTOR,
  29. message: t('Invalid source value: %s', selector),
  30. };
  31. }
  32. }
  33. }
  34. if (errorMessage.startsWith('regex parse error:')) {
  35. for (const line of errorMessage.split('\n')) {
  36. if (line.startsWith('error:')) {
  37. const regex = line.slice(6).replace(/at line \d+ column \d+/, '');
  38. return {
  39. type: ErrorType.REGEX_PARSE,
  40. message: t('Invalid regex: %s', regex),
  41. };
  42. }
  43. }
  44. }
  45. if (errorMessage.startsWith('Compiled regex exceeds size limit')) {
  46. return {
  47. type: ErrorType.REGEX_PARSE,
  48. message: t('Compiled regex is too large, simplify your regex'),
  49. };
  50. }
  51. return {
  52. type: ErrorType.UNKNOWN,
  53. message: t('An unknown error occurred while saving data scrubbing rule'),
  54. };
  55. }
  56. export default handleError;