createTrustedRelaysResponseError.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import {t} from 'sentry/locale';
  2. import type RequestError from 'sentry/utils/requestError/requestError';
  3. type TrustedRelaysResponseError = {
  4. message: string;
  5. type:
  6. | 'unknown'
  7. | 'bad-structure'
  8. | 'missing-name'
  9. | 'empty-name'
  10. | 'missing-key'
  11. | 'invalid-key'
  12. | 'duplicated-key';
  13. };
  14. interface TrustedRelaysRequestError extends RequestError {
  15. responseJSON?: {
  16. trustedRelays: Array<string>;
  17. };
  18. }
  19. function createTrustedRelaysResponseError(
  20. error: TrustedRelaysRequestError
  21. ): TrustedRelaysResponseError {
  22. const errorMessage = error.responseJSON?.trustedRelays[0];
  23. if (!errorMessage) {
  24. return {
  25. type: 'unknown',
  26. message: t('An unknown error occurred while saving Relay public key.'),
  27. };
  28. }
  29. if (errorMessage === 'Bad structure received for Trusted Relays') {
  30. return {
  31. type: 'bad-structure',
  32. message: t('An invalid structure was sent.'),
  33. };
  34. }
  35. if (errorMessage === 'Relay key info with missing name in Trusted Relays') {
  36. return {
  37. type: 'missing-name',
  38. message: t('Field Required'),
  39. };
  40. }
  41. if (errorMessage === 'Relay key info with empty name in Trusted Relays') {
  42. return {
  43. type: 'empty-name',
  44. message: t('Invalid Field'),
  45. };
  46. }
  47. if (errorMessage.startsWith('Missing public key for Relay key info with name:')) {
  48. return {
  49. type: 'missing-key',
  50. message: t('Field Required'),
  51. };
  52. }
  53. if (errorMessage.startsWith('Invalid public key for relay key info with name:')) {
  54. return {
  55. type: 'invalid-key',
  56. message: t('Invalid Relay key'),
  57. };
  58. }
  59. if (errorMessage.startsWith('Duplicated key in Trusted Relays:')) {
  60. return {
  61. type: 'duplicated-key',
  62. message: t('Relay key already taken'),
  63. };
  64. }
  65. return {
  66. type: 'unknown',
  67. message: t('An unknown error occurred while saving Relay public key.'),
  68. };
  69. }
  70. export default createTrustedRelaysResponseError;