diff --git a/src/api/ShlinkApiError.tsx b/src/api/ShlinkApiError.tsx index ed3c5708..6b78da38 100644 --- a/src/api/ShlinkApiError.tsx +++ b/src/api/ShlinkApiError.tsx @@ -1,5 +1,5 @@ -import { ProblemDetailsError } from './types'; import { isInvalidArgumentError } from './utils'; +import { ProblemDetailsError } from './types/errors'; export interface ShlinkApiErrorProps { errorData?: ProblemDetailsError; diff --git a/src/api/services/ShlinkApiClient.ts b/src/api/services/ShlinkApiClient.ts index 8850ff18..4b837c33 100644 --- a/src/api/services/ShlinkApiClient.ts +++ b/src/api/services/ShlinkApiClient.ts @@ -17,10 +17,10 @@ import { ShlinkDomainRedirects, ShlinkShortUrlsListParams, ShlinkShortUrlsListNormalizedParams, - ProblemDetailsError, } from '../types'; import { orderToString } from '../../utils/helpers/ordering'; -import { isRegularNotFound } from '../utils'; +import { isRegularNotFound, parseApiError } from '../utils'; +import { ProblemDetailsError } from '../types/errors'; const buildShlinkBaseUrl = (url: string, version: 2 | 3) => `${url}/rest/v${version}`; const rejectNilProps = reject(isNil); @@ -129,7 +129,7 @@ export class ShlinkApiClient { data: body, paramsSerializer: { indexes: false }, }).catch((e: AxiosError) => { - if (!isRegularNotFound(e.response?.data)) { + if (!isRegularNotFound(parseApiError(e))) { throw e; } diff --git a/src/api/types/actions.ts b/src/api/types/actions.ts index 5359b6b3..b60b4c9c 100644 --- a/src/api/types/actions.ts +++ b/src/api/types/actions.ts @@ -1,5 +1,5 @@ import { Action } from 'redux'; -import { ProblemDetailsError } from './index'; +import { ProblemDetailsError } from './errors'; export interface ApiErrorAction extends Action { errorData?: ProblemDetailsError; diff --git a/src/api/types/errors.ts b/src/api/types/errors.ts new file mode 100644 index 00000000..97b04cec --- /dev/null +++ b/src/api/types/errors.ts @@ -0,0 +1,54 @@ +export enum ErrorTypeV2 { + INVALID_ARGUMENT = 'INVALID_ARGUMENT', + INVALID_SHORT_URL_DELETION = 'INVALID_SHORT_URL_DELETION', + DOMAIN_NOT_FOUND = 'DOMAIN_NOT_FOUND', + FORBIDDEN_OPERATION = 'FORBIDDEN_OPERATION', + INVALID_URL = 'INVALID_URL', + INVALID_SLUG = 'INVALID_SLUG', + INVALID_SHORTCODE = 'INVALID_SHORTCODE', + TAG_CONFLICT = 'TAG_CONFLICT', + TAG_NOT_FOUND = 'TAG_NOT_FOUND', + MERCURE_NOT_CONFIGURED = 'MERCURE_NOT_CONFIGURED', + INVALID_AUTHORIZATION = 'INVALID_AUTHORIZATION', + INVALID_API_KEY = 'INVALID_API_KEY', + NOT_FOUND = 'NOT_FOUND', +} + +export enum ErrorTypeV3 { + INVALID_ARGUMENT = 'https://shlink.io/api/error/invalid-data', + INVALID_SHORT_URL_DELETION = 'https://shlink.io/api/error/invalid-short-url-deletion', + DOMAIN_NOT_FOUND = 'https://shlink.io/api/error/domain-not-found', + FORBIDDEN_OPERATION = 'https://shlink.io/api/error/forbidden-tag-operation', + INVALID_URL = 'https://shlink.io/api/error/invalid-url', + INVALID_SLUG = 'https://shlink.io/api/error/non-unique-slug', + INVALID_SHORTCODE = 'https://shlink.io/api/error/short-url-not-found', + TAG_CONFLICT = 'https://shlink.io/api/error/tag-conflict', + TAG_NOT_FOUND = 'https://shlink.io/api/error/tag-not-found', + MERCURE_NOT_CONFIGURED = 'https://shlink.io/api/error/mercure-not-configured', + INVALID_AUTHORIZATION = 'https://shlink.io/api/error/missing-authentication', + INVALID_API_KEY = 'https://shlink.io/api/error/invalid-api-key', + NOT_FOUND = 'https://shlink.io/api/error/not-found', +} + +export interface ProblemDetailsError { + type: string; + detail: string; + title: string; + status: number; + [extraProps: string]: any; +} + +export interface InvalidArgumentError extends ProblemDetailsError { + type: ErrorTypeV2.INVALID_ARGUMENT | ErrorTypeV3.INVALID_ARGUMENT; + invalidElements: string[]; +} + +export interface InvalidShortUrlDeletion extends ProblemDetailsError { + type: 'INVALID_SHORTCODE_DELETION' | ErrorTypeV2.INVALID_SHORT_URL_DELETION | ErrorTypeV3.INVALID_SHORT_URL_DELETION; + threshold: number; +} + +export interface RegularNotFound extends ProblemDetailsError { + type: ErrorTypeV2.NOT_FOUND | ErrorTypeV3.NOT_FOUND; + status: 404; +} diff --git a/src/api/types/index.ts b/src/api/types/index.ts index d8d521cf..fc3fe6de 100644 --- a/src/api/types/index.ts +++ b/src/api/types/index.ts @@ -102,26 +102,3 @@ export interface ShlinkShortUrlsListParams { export interface ShlinkShortUrlsListNormalizedParams extends Omit { orderBy?: string; } - -export interface ProblemDetailsError { - type: string; - detail: string; - title: string; - status: number; - [extraProps: string]: any; -} - -export interface InvalidArgumentError extends ProblemDetailsError { - type: 'INVALID_ARGUMENT'; - invalidElements: string[]; -} - -export interface InvalidShortUrlDeletion extends ProblemDetailsError { - type: 'INVALID_SHORTCODE_DELETION' | 'INVALID_SHORT_URL_DELETION'; - threshold: number; -} - -export interface RegularNotFound extends ProblemDetailsError { - type: 'NOT_FOUND'; - status: 404; -} diff --git a/src/api/utils/index.ts b/src/api/utils/index.ts index 0430bfd3..2156a410 100644 --- a/src/api/utils/index.ts +++ b/src/api/utils/index.ts @@ -1,13 +1,22 @@ import { AxiosError } from 'axios'; -import { InvalidArgumentError, InvalidShortUrlDeletion, ProblemDetailsError, RegularNotFound } from '../types'; +import { + ErrorTypeV2, + ErrorTypeV3, + InvalidArgumentError, + InvalidShortUrlDeletion, + ProblemDetailsError, + RegularNotFound, +} from '../types/errors'; export const parseApiError = (e: AxiosError) => e.response?.data; export const isInvalidArgumentError = (error?: ProblemDetailsError): error is InvalidArgumentError => - error?.type === 'INVALID_ARGUMENT'; + error?.type === ErrorTypeV2.INVALID_ARGUMENT || error?.type === ErrorTypeV3.INVALID_ARGUMENT; export const isInvalidDeletionError = (error?: ProblemDetailsError): error is InvalidShortUrlDeletion => - error?.type === 'INVALID_SHORTCODE_DELETION' || error?.type === 'INVALID_SHORT_URL_DELETION'; + error?.type === 'INVALID_SHORTCODE_DELETION' + || error?.type === ErrorTypeV2.INVALID_SHORT_URL_DELETION + || error?.type === ErrorTypeV3.INVALID_SHORT_URL_DELETION; export const isRegularNotFound = (error?: ProblemDetailsError): error is RegularNotFound => - error?.type === 'NOT_FOUND' && error?.status === 404; + (error?.type === ErrorTypeV2.NOT_FOUND || error?.type === ErrorTypeV3.NOT_FOUND) && error?.status === 404; diff --git a/src/domains/reducers/domainsList.ts b/src/domains/reducers/domainsList.ts index b2349810..b1f5e061 100644 --- a/src/domains/reducers/domainsList.ts +++ b/src/domains/reducers/domainsList.ts @@ -1,5 +1,5 @@ import { Action, Dispatch } from 'redux'; -import { ProblemDetailsError, ShlinkDomainRedirects } from '../../api/types'; +import { ShlinkDomainRedirects } from '../../api/types'; import { buildReducer } from '../../utils/helpers/redux'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; import { GetState } from '../../container/types'; @@ -9,6 +9,7 @@ import { Domain, DomainStatus } from '../data'; import { hasServerData } from '../../servers/data'; import { replaceAuthorityFromUri } from '../../utils/helpers/uri'; import { EDIT_DOMAIN_REDIRECTS, EditDomainRedirectsAction } from './domainRedirects'; +import { ProblemDetailsError } from '../../api/types/errors'; export const LIST_DOMAINS_START = 'shlink/domainsList/LIST_DOMAINS_START'; export const LIST_DOMAINS_ERROR = 'shlink/domainsList/LIST_DOMAINS_ERROR'; diff --git a/src/short-urls/reducers/shortUrlCreation.ts b/src/short-urls/reducers/shortUrlCreation.ts index 2be9ff7e..af7c1146 100644 --- a/src/short-urls/reducers/shortUrlCreation.ts +++ b/src/short-urls/reducers/shortUrlCreation.ts @@ -3,9 +3,9 @@ import { GetState } from '../../container/types'; import { ShortUrl, ShortUrlData } from '../data'; import { buildReducer, buildActionCreator } from '../../utils/helpers/redux'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; -import { ProblemDetailsError } from '../../api/types'; import { parseApiError } from '../../api/utils'; import { ApiErrorAction } from '../../api/types/actions'; +import { ProblemDetailsError } from '../../api/types/errors'; export const CREATE_SHORT_URL_START = 'shlink/createShortUrl/CREATE_SHORT_URL_START'; export const CREATE_SHORT_URL_ERROR = 'shlink/createShortUrl/CREATE_SHORT_URL_ERROR'; diff --git a/src/short-urls/reducers/shortUrlDeletion.ts b/src/short-urls/reducers/shortUrlDeletion.ts index 24ec999d..52edeed3 100644 --- a/src/short-urls/reducers/shortUrlDeletion.ts +++ b/src/short-urls/reducers/shortUrlDeletion.ts @@ -1,10 +1,10 @@ import { Action, Dispatch } from 'redux'; import { buildActionCreator, buildReducer } from '../../utils/helpers/redux'; -import { ProblemDetailsError } from '../../api/types'; import { GetState } from '../../container/types'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; import { parseApiError } from '../../api/utils'; import { ApiErrorAction } from '../../api/types/actions'; +import { ProblemDetailsError } from '../../api/types/errors'; export const DELETE_SHORT_URL_START = 'shlink/deleteShortUrl/DELETE_SHORT_URL_START'; export const DELETE_SHORT_URL_ERROR = 'shlink/deleteShortUrl/DELETE_SHORT_URL_ERROR'; diff --git a/src/short-urls/reducers/shortUrlDetail.ts b/src/short-urls/reducers/shortUrlDetail.ts index dd1bf005..81ab192c 100644 --- a/src/short-urls/reducers/shortUrlDetail.ts +++ b/src/short-urls/reducers/shortUrlDetail.ts @@ -5,9 +5,9 @@ import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilde import { OptionalString } from '../../utils/utils'; import { GetState } from '../../container/types'; import { shortUrlMatches } from '../helpers'; -import { ProblemDetailsError } from '../../api/types'; import { parseApiError } from '../../api/utils'; import { ApiErrorAction } from '../../api/types/actions'; +import { ProblemDetailsError } from '../../api/types/errors'; export const GET_SHORT_URL_DETAIL_START = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_START'; export const GET_SHORT_URL_DETAIL_ERROR = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_ERROR'; diff --git a/src/short-urls/reducers/shortUrlEdition.ts b/src/short-urls/reducers/shortUrlEdition.ts index 6c530695..72bf437d 100644 --- a/src/short-urls/reducers/shortUrlEdition.ts +++ b/src/short-urls/reducers/shortUrlEdition.ts @@ -4,9 +4,9 @@ import { GetState } from '../../container/types'; import { OptionalString } from '../../utils/utils'; import { EditShortUrlData, ShortUrl } from '../data'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; -import { ProblemDetailsError } from '../../api/types'; import { parseApiError } from '../../api/utils'; import { ApiErrorAction } from '../../api/types/actions'; +import { ProblemDetailsError } from '../../api/types/errors'; export const EDIT_SHORT_URL_START = 'shlink/shortUrlEdition/EDIT_SHORT_URL_START'; export const EDIT_SHORT_URL_ERROR = 'shlink/shortUrlEdition/EDIT_SHORT_URL_ERROR'; diff --git a/src/tags/reducers/tagDelete.ts b/src/tags/reducers/tagDelete.ts index 68002479..f0111660 100644 --- a/src/tags/reducers/tagDelete.ts +++ b/src/tags/reducers/tagDelete.ts @@ -2,9 +2,9 @@ import { Action, Dispatch } from 'redux'; import { buildReducer } from '../../utils/helpers/redux'; import { GetState } from '../../container/types'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; -import { ProblemDetailsError } from '../../api/types'; import { parseApiError } from '../../api/utils'; import { ApiErrorAction } from '../../api/types/actions'; +import { ProblemDetailsError } from '../../api/types/errors'; export const DELETE_TAG_START = 'shlink/deleteTag/DELETE_TAG_START'; export const DELETE_TAG_ERROR = 'shlink/deleteTag/DELETE_TAG_ERROR'; diff --git a/src/tags/reducers/tagEdit.ts b/src/tags/reducers/tagEdit.ts index ea58ad8c..b3582f62 100644 --- a/src/tags/reducers/tagEdit.ts +++ b/src/tags/reducers/tagEdit.ts @@ -4,9 +4,9 @@ import { buildReducer } from '../../utils/helpers/redux'; import { GetState } from '../../container/types'; import { ColorGenerator } from '../../utils/services/ColorGenerator'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; -import { ProblemDetailsError } from '../../api/types'; import { parseApiError } from '../../api/utils'; import { ApiErrorAction } from '../../api/types/actions'; +import { ProblemDetailsError } from '../../api/types/errors'; export const EDIT_TAG_START = 'shlink/editTag/EDIT_TAG_START'; export const EDIT_TAG_ERROR = 'shlink/editTag/EDIT_TAG_ERROR'; diff --git a/src/tags/reducers/tagsList.ts b/src/tags/reducers/tagsList.ts index de84c709..cf7a7ac6 100644 --- a/src/tags/reducers/tagsList.ts +++ b/src/tags/reducers/tagsList.ts @@ -2,7 +2,7 @@ import { isEmpty, reject } from 'ramda'; import { Action, Dispatch } from 'redux'; import { CREATE_VISITS, CreateVisitsAction } from '../../visits/reducers/visitCreation'; import { buildReducer } from '../../utils/helpers/redux'; -import { ProblemDetailsError, ShlinkTags } from '../../api/types'; +import { ShlinkTags } from '../../api/types'; import { GetState } from '../../container/types'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; import { CreateVisit, Stats } from '../../visits/types'; @@ -12,6 +12,7 @@ import { ApiErrorAction } from '../../api/types/actions'; import { CREATE_SHORT_URL, CreateShortUrlAction } from '../../short-urls/reducers/shortUrlCreation'; import { DeleteTagAction, TAG_DELETED } from './tagDelete'; import { EditTagAction, TAG_EDITED } from './tagEdit'; +import { ProblemDetailsError } from '../../api/types/errors'; export const LIST_TAGS_START = 'shlink/tagsList/LIST_TAGS_START'; export const LIST_TAGS_ERROR = 'shlink/tagsList/LIST_TAGS_ERROR'; diff --git a/src/visits/types/index.ts b/src/visits/types/index.ts index 03789a28..a110ded0 100644 --- a/src/visits/types/index.ts +++ b/src/visits/types/index.ts @@ -1,7 +1,8 @@ import { Action } from 'redux'; import { ShortUrl } from '../../short-urls/data'; -import { ProblemDetailsError, ShlinkVisitsParams } from '../../api/types'; +import { ShlinkVisitsParams } from '../../api/types'; import { DateInterval, DateRange } from '../../utils/dates/types'; +import { ProblemDetailsError } from '../../api/types/errors'; export interface VisitsInfo { visits: Visit[]; diff --git a/test/api/ShlinkApiError.test.tsx b/test/api/ShlinkApiError.test.tsx index fe04fe47..a7aaebc1 100644 --- a/test/api/ShlinkApiError.test.tsx +++ b/test/api/ShlinkApiError.test.tsx @@ -1,7 +1,7 @@ import { render, screen } from '@testing-library/react'; import { Mock } from 'ts-mockery'; import { ShlinkApiError, ShlinkApiErrorProps } from '../../src/api/ShlinkApiError'; -import { InvalidArgumentError, ProblemDetailsError } from '../../src/api/types'; +import { ErrorTypeV2, ErrorTypeV3, InvalidArgumentError, ProblemDetailsError } from '../../src/api/types/errors'; describe('', () => { const setUp = (props: ShlinkApiErrorProps) => render(); @@ -20,7 +20,8 @@ describe('', () => { it.each([ [undefined, 0], [Mock.all(), 0], - [Mock.of({ type: 'INVALID_ARGUMENT', invalidElements: [] }), 1], + [Mock.of({ type: ErrorTypeV2.INVALID_ARGUMENT, invalidElements: [] }), 1], + [Mock.of({ type: ErrorTypeV3.INVALID_ARGUMENT, invalidElements: [] }), 1], ])('renders list of invalid elements when provided error is an InvalidError', (errorData, expectedElementsCount) => { setUp({ errorData }); expect(screen.queryAllByText(/^Invalid elements/)).toHaveLength(expectedElementsCount); diff --git a/test/domains/ManageDomains.test.tsx b/test/domains/ManageDomains.test.tsx index 509ee2c7..4726ed4e 100644 --- a/test/domains/ManageDomains.test.tsx +++ b/test/domains/ManageDomains.test.tsx @@ -2,9 +2,10 @@ import { screen, waitFor } from '@testing-library/react'; import { Mock } from 'ts-mockery'; import { DomainsList } from '../../src/domains/reducers/domainsList'; import { ManageDomains } from '../../src/domains/ManageDomains'; -import { ProblemDetailsError, ShlinkDomain } from '../../src/api/types'; +import { ShlinkDomain } from '../../src/api/types'; import { SelectedServer } from '../../src/servers/data'; import { renderWithEvents } from '../__helpers__/setUpTest'; +import { ProblemDetailsError } from '../../src/api/types/errors'; describe('', () => { const listDomains = jest.fn(); diff --git a/test/short-urls/helpers/DeleteShortUrlModal.test.tsx b/test/short-urls/helpers/DeleteShortUrlModal.test.tsx index c3a3bd51..ffdfb74e 100644 --- a/test/short-urls/helpers/DeleteShortUrlModal.test.tsx +++ b/test/short-urls/helpers/DeleteShortUrlModal.test.tsx @@ -3,8 +3,8 @@ import { Mock } from 'ts-mockery'; import { DeleteShortUrlModal } from '../../../src/short-urls/helpers/DeleteShortUrlModal'; import { ShortUrl } from '../../../src/short-urls/data'; import { ShortUrlDeletion } from '../../../src/short-urls/reducers/shortUrlDeletion'; -import { ProblemDetailsError } from '../../../src/api/types'; import { renderWithEvents } from '../../__helpers__/setUpTest'; +import { ProblemDetailsError } from '../../../src/api/types/errors'; describe('', () => { const shortUrl = Mock.of({ diff --git a/test/short-urls/reducers/shortUrlDeletion.test.ts b/test/short-urls/reducers/shortUrlDeletion.test.ts index 0eb61b25..95500ee5 100644 --- a/test/short-urls/reducers/shortUrlDeletion.test.ts +++ b/test/short-urls/reducers/shortUrlDeletion.test.ts @@ -7,8 +7,8 @@ import reducer, { resetDeleteShortUrl, deleteShortUrl, } from '../../../src/short-urls/reducers/shortUrlDeletion'; -import { ProblemDetailsError } from '../../../src/api/types'; import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient'; +import { ProblemDetailsError } from '../../../src/api/types/errors'; describe('shortUrlDeletionReducer', () => { describe('reducer', () => { diff --git a/test/tags/helpers/EditTagModal.test.tsx b/test/tags/helpers/EditTagModal.test.tsx index d3f11979..46820437 100644 --- a/test/tags/helpers/EditTagModal.test.tsx +++ b/test/tags/helpers/EditTagModal.test.tsx @@ -3,8 +3,8 @@ import { Mock } from 'ts-mockery'; import { TagEdition } from '../../../src/tags/reducers/tagEdit'; import { EditTagModal as createEditTagModal } from '../../../src/tags/helpers/EditTagModal'; import { ColorGenerator } from '../../../src/utils/services/ColorGenerator'; -import { ProblemDetailsError } from '../../../src/api/types'; import { renderWithEvents } from '../../__helpers__/setUpTest'; +import { ProblemDetailsError } from '../../../src/api/types/errors'; describe('', () => { const EditTagModal = createEditTagModal(Mock.of({ getColorForKey: jest.fn(() => 'green') }));