From ee95d5a1b7d8220f8716b11f2d922ea18eee1d61 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 21 Dec 2020 21:26:45 +0100 Subject: [PATCH] Improved handling of errors in several API interactions --- src/short-urls/helpers/EditShortUrlModal.tsx | 8 ++++++-- src/short-urls/helpers/EditTagsModal.tsx | 10 ++++++---- src/short-urls/reducers/shortUrlEdition.ts | 12 +++++++++--- src/short-urls/reducers/shortUrlTags.ts | 12 +++++++++--- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/short-urls/helpers/EditShortUrlModal.tsx b/src/short-urls/helpers/EditShortUrlModal.tsx index 1a24d640..0a09e27e 100644 --- a/src/short-urls/helpers/EditShortUrlModal.tsx +++ b/src/short-urls/helpers/EditShortUrlModal.tsx @@ -5,6 +5,7 @@ import { ShortUrlEdition } from '../reducers/shortUrlEdition'; import { handleEventPreventingDefault, hasValue, OptionalString } from '../../utils/utils'; import { ShortUrlModalProps } from '../data'; import { Result } from '../../utils/Result'; +import { ShlinkApiError } from '../../api/ShlinkApiError'; interface EditShortUrlModalProps extends ShortUrlModalProps { shortUrlEdition: ShortUrlEdition; @@ -12,7 +13,7 @@ interface EditShortUrlModalProps extends ShortUrlModalProps { } const EditShortUrlModal = ({ isOpen, toggle, shortUrl, shortUrlEdition, editShortUrl }: EditShortUrlModalProps) => { - const { saving, error } = shortUrlEdition; + const { saving, error, errorData } = shortUrlEdition; const url = shortUrl?.shortUrl ?? ''; const [ longUrl, setLongUrl ] = useState(shortUrl.longUrl); @@ -36,7 +37,10 @@ const EditShortUrlModal = ({ isOpen, toggle, shortUrl, shortUrlEdition, editShor {error && ( - Something went wrong while saving the long URL :( + )} diff --git a/src/short-urls/helpers/EditTagsModal.tsx b/src/short-urls/helpers/EditTagsModal.tsx index a41a4b98..a390499c 100644 --- a/src/short-urls/helpers/EditTagsModal.tsx +++ b/src/short-urls/helpers/EditTagsModal.tsx @@ -6,6 +6,7 @@ import { ShortUrlModalProps } from '../data'; import { OptionalString } from '../../utils/utils'; import { TagsSelectorProps } from '../../tags/helpers/TagsSelector'; import { Result } from '../../utils/Result'; +import { ShlinkApiError } from '../../api/ShlinkApiError'; interface EditTagsModalProps extends ShortUrlModalProps { shortUrlTags: ShortUrlTags; @@ -20,6 +21,7 @@ const EditTagsModal = (TagsSelector: FC) => ( useEffect(() => resetShortUrlsTags, []); + const { saving, error, errorData } = shortUrlTags; const url = shortUrl?.shortUrl ?? ''; const saveTags = async () => editShortUrlTags(shortUrl.shortCode, shortUrl.domain, selectedTags) .then(toggle) @@ -32,16 +34,16 @@ const EditTagsModal = (TagsSelector: FC) => ( - {shortUrlTags.error && ( + {error && ( - Something went wrong while saving the tags :( + )} - diff --git a/src/short-urls/reducers/shortUrlEdition.ts b/src/short-urls/reducers/shortUrlEdition.ts index 44feac7b..c326c1ab 100644 --- a/src/short-urls/reducers/shortUrlEdition.ts +++ b/src/short-urls/reducers/shortUrlEdition.ts @@ -4,6 +4,7 @@ import { GetState } from '../../container/types'; import { OptionalString } from '../../utils/utils'; import { ShortUrlIdentifier } from '../data'; import { ShlinkApiClientBuilder } from '../../utils/services/ShlinkApiClientBuilder'; +import { ProblemDetailsError } from '../../utils/services/types'; /* eslint-disable padding-line-between-statements */ export const EDIT_SHORT_URL_START = 'shlink/shortUrlEdition/EDIT_SHORT_URL_START'; @@ -16,12 +17,17 @@ export interface ShortUrlEdition { longUrl: string | null; saving: boolean; error: boolean; + errorData?: ProblemDetailsError; } export interface ShortUrlEditedAction extends Action, ShortUrlIdentifier { longUrl: string; } +export interface ShortUrlEditionFailedAction extends Action { + errorData?: ProblemDetailsError; +} + const initialState: ShortUrlEdition = { shortCode: null, longUrl: null, @@ -29,9 +35,9 @@ const initialState: ShortUrlEdition = { error: false, }; -export default buildReducer({ +export default buildReducer({ [EDIT_SHORT_URL_START]: (state) => ({ ...state, saving: true, error: false }), - [EDIT_SHORT_URL_ERROR]: (state) => ({ ...state, saving: false, error: true }), + [EDIT_SHORT_URL_ERROR]: (state, { errorData }) => ({ ...state, saving: false, error: true, errorData }), [SHORT_URL_EDITED]: (_, { shortCode, longUrl }) => ({ shortCode, longUrl, saving: false, error: false }), }, initialState); @@ -47,7 +53,7 @@ export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => ( await updateShortUrlMeta(shortCode, domain, { longUrl }); dispatch({ shortCode, longUrl, domain, type: SHORT_URL_EDITED }); } catch (e) { - dispatch({ type: EDIT_SHORT_URL_ERROR }); + dispatch({ type: EDIT_SHORT_URL_ERROR, errorData: e.response?.data }); throw e; } diff --git a/src/short-urls/reducers/shortUrlTags.ts b/src/short-urls/reducers/shortUrlTags.ts index b81380f1..0c38462c 100644 --- a/src/short-urls/reducers/shortUrlTags.ts +++ b/src/short-urls/reducers/shortUrlTags.ts @@ -4,6 +4,7 @@ import { GetState } from '../../container/types'; import { OptionalString } from '../../utils/utils'; import { ShortUrlIdentifier } from '../data'; import { ShlinkApiClientBuilder } from '../../utils/services/ShlinkApiClientBuilder'; +import { ProblemDetailsError } from '../../utils/services/types'; /* eslint-disable padding-line-between-statements */ export const EDIT_SHORT_URL_TAGS_START = 'shlink/shortUrlTags/EDIT_SHORT_URL_TAGS_START'; @@ -17,12 +18,17 @@ export interface ShortUrlTags { tags: string[]; saving: boolean; error: boolean; + errorData?: ProblemDetailsError; } export interface EditShortUrlTagsAction extends Action, ShortUrlIdentifier { tags: string[]; } +export interface EditShortUrlTagsFailedAction extends Action { + errorData?: ProblemDetailsError; +} + const initialState: ShortUrlTags = { shortCode: null, tags: [], @@ -30,9 +36,9 @@ const initialState: ShortUrlTags = { error: false, }; -export default buildReducer({ +export default buildReducer({ [EDIT_SHORT_URL_TAGS_START]: (state) => ({ ...state, saving: true, error: false }), - [EDIT_SHORT_URL_TAGS_ERROR]: (state) => ({ ...state, saving: false, error: true }), + [EDIT_SHORT_URL_TAGS_ERROR]: (state, { errorData }) => ({ ...state, saving: false, error: true, errorData }), [SHORT_URL_TAGS_EDITED]: (_, { shortCode, tags }) => ({ shortCode, tags, saving: false, error: false }), [RESET_EDIT_SHORT_URL_TAGS]: () => initialState, }, initialState); @@ -50,7 +56,7 @@ export const editShortUrlTags = (buildShlinkApiClient: ShlinkApiClientBuilder) = dispatch({ tags: normalizedTags, shortCode, domain, type: SHORT_URL_TAGS_EDITED }); } catch (e) { - dispatch({ type: EDIT_SHORT_URL_TAGS_ERROR }); + dispatch({ type: EDIT_SHORT_URL_TAGS_ERROR, errorData: e.response?.data }); throw e; }