-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(wallet-mobile): collocated api errors
- Loading branch information
1 parent
541b2ab
commit 89ea130
Showing
3 changed files
with
116 additions
and
108 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,123 @@ | ||
import {Api} from '@yoroi/types' | ||
import * as React from 'react' | ||
import {useIntl} from 'react-intl' | ||
|
||
import {useApiDialogs} from './useApiDialogs' | ||
import {apiErrors} from '../kernel/i18n/global-messages' | ||
|
||
export const useApiErrorResolver = () => { | ||
const dialogs = useApiDialogs() | ||
|
||
const resolver = (error: unknown) => { | ||
if (error instanceof Api.Errors.BadRequest) return dialogs.errorBadRequest | ||
if (error instanceof Api.Errors.Conflict) return dialogs.errorConflict | ||
if (error instanceof Api.Errors.Forbidden) return dialogs.errorForbidden | ||
if (error instanceof Api.Errors.Gone) return dialogs.errorGone | ||
if (error instanceof Api.Errors.InvalidState) return dialogs.errorInvalidState | ||
if (error instanceof Api.Errors.Network) return dialogs.errorNetwork | ||
if (error instanceof Api.Errors.NotFound) return dialogs.errorNotFound | ||
if (error instanceof Api.Errors.ResponseMalformed) return dialogs.errorResponseMalformed | ||
if (error instanceof Api.Errors.ServerSide) return dialogs.errorServerSide | ||
if (error instanceof Api.Errors.TooEarly) return dialogs.errorTooEarly | ||
if (error instanceof Api.Errors.TooManyRequests) return dialogs.errorTooManyRequests | ||
if (error instanceof Api.Errors.Unauthorized) return dialogs.errorUnauthorized | ||
if (error instanceof Api.Errors.Unknown) return dialogs.errorUnknown | ||
|
||
return dialogs.errorUnknown | ||
} | ||
const strings = useApiStringsErrors() | ||
|
||
const dialogs = React.useRef({ | ||
errorBadRequest: { | ||
title: strings.errorTitle, | ||
message: strings.badRequest, | ||
}, | ||
errorUnauthorized: { | ||
title: strings.errorTitle, | ||
message: strings.unauthorized, | ||
}, | ||
errorForbidden: { | ||
title: strings.errorTitle, | ||
message: strings.forbidden, | ||
}, | ||
errorNotFound: { | ||
title: strings.errorTitle, | ||
message: strings.notFound, | ||
}, | ||
errorConflict: { | ||
title: strings.errorTitle, | ||
message: strings.conflict, | ||
}, | ||
errorGone: { | ||
title: strings.errorTitle, | ||
message: strings.gone, | ||
}, | ||
errorTooEarly: { | ||
title: strings.errorTitle, | ||
message: strings.tooEarly, | ||
}, | ||
errorTooManyRequests: { | ||
title: strings.errorTitle, | ||
message: strings.tooManyRequests, | ||
}, | ||
errorServerSide: { | ||
title: strings.errorTitle, | ||
message: strings.serverSide, | ||
}, | ||
errorUnknown: { | ||
title: strings.errorTitle, | ||
message: strings.unknown, | ||
}, | ||
errorNetwork: { | ||
title: strings.errorTitle, | ||
message: strings.network, | ||
}, | ||
errorInvalidState: { | ||
title: strings.errorTitle, | ||
message: strings.invalidState, | ||
}, | ||
errorResponseMalformed: { | ||
title: strings.errorTitle, | ||
message: strings.responseMalformed, | ||
}, | ||
} as const).current | ||
|
||
const resolver = React.useCallback( | ||
(error: unknown) => { | ||
if (error instanceof Api.Errors.BadRequest) return dialogs.errorBadRequest | ||
if (error instanceof Api.Errors.Conflict) return dialogs.errorConflict | ||
if (error instanceof Api.Errors.Forbidden) return dialogs.errorForbidden | ||
if (error instanceof Api.Errors.Gone) return dialogs.errorGone | ||
if (error instanceof Api.Errors.InvalidState) return dialogs.errorInvalidState | ||
if (error instanceof Api.Errors.Network) return dialogs.errorNetwork | ||
if (error instanceof Api.Errors.NotFound) return dialogs.errorNotFound | ||
if (error instanceof Api.Errors.ResponseMalformed) return dialogs.errorResponseMalformed | ||
if (error instanceof Api.Errors.ServerSide) return dialogs.errorServerSide | ||
if (error instanceof Api.Errors.TooEarly) return dialogs.errorTooEarly | ||
if (error instanceof Api.Errors.TooManyRequests) return dialogs.errorTooManyRequests | ||
if (error instanceof Api.Errors.Unauthorized) return dialogs.errorUnauthorized | ||
if (error instanceof Api.Errors.Unknown) return dialogs.errorUnknown | ||
|
||
return dialogs.errorUnknown | ||
}, | ||
[ | ||
dialogs.errorBadRequest, | ||
dialogs.errorConflict, | ||
dialogs.errorForbidden, | ||
dialogs.errorGone, | ||
dialogs.errorInvalidState, | ||
dialogs.errorNetwork, | ||
dialogs.errorNotFound, | ||
dialogs.errorResponseMalformed, | ||
dialogs.errorServerSide, | ||
dialogs.errorTooEarly, | ||
dialogs.errorTooManyRequests, | ||
dialogs.errorUnauthorized, | ||
dialogs.errorUnknown, | ||
], | ||
) | ||
|
||
return resolver | ||
} | ||
|
||
const useApiStringsErrors = () => { | ||
const intl = useIntl() | ||
|
||
return React.useRef({ | ||
errorTitle: intl.formatMessage(apiErrors.title), | ||
|
||
badRequest: intl.formatMessage(apiErrors.badRequest), | ||
conflict: intl.formatMessage(apiErrors.conflict), | ||
forbidden: intl.formatMessage(apiErrors.forbidden), | ||
gone: intl.formatMessage(apiErrors.gone), | ||
invalidState: intl.formatMessage(apiErrors.invalidState), | ||
network: intl.formatMessage(apiErrors.network), | ||
notFound: intl.formatMessage(apiErrors.notFound), | ||
responseMalformed: intl.formatMessage(apiErrors.responseMalformed), | ||
serverSide: intl.formatMessage(apiErrors.serverSide), | ||
tooEarly: intl.formatMessage(apiErrors.tooEarly), | ||
tooManyRequests: intl.formatMessage(apiErrors.tooManyRequests), | ||
unauthorized: intl.formatMessage(apiErrors.unauthorized), | ||
unknown: intl.formatMessage(apiErrors.unknown), | ||
} as const).current | ||
} |
This file was deleted.
Oops, something went wrong.