|
14 | 14 |
|
15 | 15 | import * as Sentry from '@sentry/react' |
16 | 16 | import { isPlainObject, isObject } from 'lodash' |
| 17 | +import { defineMessages } from 'react-intl' |
17 | 18 |
|
18 | 19 | import { error as errorLog, warn } from '@ttn-lw/lib/log' |
19 | 20 | import interpolate from '@ttn-lw/lib/interpolate' |
@@ -588,3 +589,40 @@ export const ingestError = (error, extras = {}, tags = {}) => { |
588 | 589 | }) |
589 | 590 | } |
590 | 591 | } |
| 592 | + |
| 593 | +/** |
| 594 | + * Maps claim-related backend errors to appropriate user messages. |
| 595 | + * |
| 596 | + * @param {object} error - The error object. |
| 597 | + * @returns {object|undefined} - The corresponding error message, or undefined if no match. |
| 598 | + */ |
| 599 | +export const getClaimGatewayErrorMessage = error => { |
| 600 | + const m = defineMessages({ |
| 601 | + notFound: "Gateway doesn't exist. Please confirm that the gateway EUI is correct.", |
| 602 | + subscriptionNotActive: |
| 603 | + 'There is no gateway subscription attached or active. Please get a <link>Gateway Subscription</link> or activate your subscription following the steps in the documentation and try again.', |
| 604 | + activationCodeExpired: |
| 605 | + 'The activation code has expired. To reactivate it, extend your <link>Gateway Subscription</link>.', |
| 606 | + permissionDenied: 'The owner token is invalid.', |
| 607 | + }) |
| 608 | + |
| 609 | + const rootCause = getBackendErrorRootCause(error) |
| 610 | + const errorCode = rootCause?.code |
| 611 | + const backendErrorMessage = rootCause?.message_format |
| 612 | + switch (errorCode) { |
| 613 | + case 5: // NOT_FOUND |
| 614 | + return m.notFound |
| 615 | + case 9: // FAILED_PRECONDITION |
| 616 | + if (backendErrorMessage.includes('gateway subscription not attached and active')) { |
| 617 | + return m.subscriptionNotActive |
| 618 | + } |
| 619 | + if (backendErrorMessage.includes('activation code expired')) { |
| 620 | + return m.activationCodeExpired |
| 621 | + } |
| 622 | + return undefined |
| 623 | + case 7: // PERMISSION_DENIED |
| 624 | + return m.permissionDenied |
| 625 | + default: |
| 626 | + return undefined |
| 627 | + } |
| 628 | +} |
0 commit comments