handleError.tsx 1.4 KB

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