diff options
author | Fabio Berger <me@fabioberger.com> | 2018-10-18 18:36:41 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-10-18 18:36:41 +0800 |
commit | 28863f9a6f8a28ddbfe588b9e4d03b1e31da6961 (patch) | |
tree | db9bd874838a30ba82eacab0d4255fe3067eff94 /packages/instant/src/util/error.ts | |
parent | 6ab6a9aa2b3511df60c078398168a84faf3e2385 (diff) | |
parent | cdd650d0eb83153a992922c6ffff35b494f299d3 (diff) | |
download | dexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.tar dexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.tar.gz dexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.tar.bz2 dexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.tar.lz dexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.tar.xz dexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.tar.zst dexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.zip |
merge dev-section-redesign
Diffstat (limited to 'packages/instant/src/util/error.ts')
-rw-r--r-- | packages/instant/src/util/error.ts | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/packages/instant/src/util/error.ts b/packages/instant/src/util/error.ts new file mode 100644 index 000000000..48cb131a9 --- /dev/null +++ b/packages/instant/src/util/error.ts @@ -0,0 +1,62 @@ +import { AssetBuyerError } from '@0xproject/asset-buyer'; +import { Dispatch } from 'redux'; + +import { Action, actions } from '../redux/actions'; +import { assetDataUtil } from '../util/asset_data'; + +class ErrorFlasher { + private _timeoutId?: number; + public flashNewError(dispatch: Dispatch<Action>, error: any, delayMs: number = 7000): void { + this._clearTimeout(); + + // dispatch new message + dispatch(actions.setError(error)); + + this._timeoutId = window.setTimeout(() => { + dispatch(actions.hideError()); + }, delayMs); + } + public clearError(dispatch: Dispatch<Action>): void { + this._clearTimeout(); + dispatch(actions.hideError()); + } + private _clearTimeout(): void { + if (this._timeoutId) { + window.clearTimeout(this._timeoutId); + } + } +} + +const humanReadableMessageForError = (error: Error, assetData?: string): string | undefined => { + const hasInsufficientLiquidity = + error.message === AssetBuyerError.InsufficientAssetLiquidity || + error.message === AssetBuyerError.InsufficientZrxLiquidity; + if (hasInsufficientLiquidity) { + const assetName = assetDataUtil.bestNameForAsset(assetData, 'of this asset'); + return `Not enough ${assetName} available`; + } + + if ( + error.message === AssetBuyerError.StandardRelayerApiError || + error.message.startsWith(AssetBuyerError.AssetUnavailable) + ) { + const assetName = assetDataUtil.bestNameForAsset(assetData, 'This asset'); + return `${assetName} is currently unavailable`; + } + + return undefined; +}; + +export const errorUtil = { + errorFlasher: new ErrorFlasher(), + errorDescription: (error?: any, assetData?: string): { icon: string; message: string } => { + let bestMessage: string | undefined; + if (error instanceof Error) { + bestMessage = humanReadableMessageForError(error, assetData); + } + return { + icon: '😢', + message: bestMessage || 'Something went wrong...', + }; + }, +}; |