aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/error.ts
diff options
context:
space:
mode:
authorfragosti <francesco.agosti93@gmail.com>2018-10-19 06:42:33 +0800
committerfragosti <francesco.agosti93@gmail.com>2018-10-19 06:42:33 +0800
commitc87e68f833a2d8a87846d70a9d4a727b46323eea (patch)
treeeb40dc12694baf607a373f427f32dbd9393ac72f /packages/instant/src/util/error.ts
parenta764dfa789ba44e519371b4a1e4569db7f551fb7 (diff)
parent65d85ca5008fe0c307506b388d6ace858122f8ad (diff)
downloaddexon-0x-contracts-c87e68f833a2d8a87846d70a9d4a727b46323eea.tar.gz
dexon-0x-contracts-c87e68f833a2d8a87846d70a9d4a727b46323eea.tar.zst
dexon-0x-contracts-c87e68f833a2d8a87846d70a9d4a727b46323eea.zip
Merge branch 'development' of https://github.com/0xProject/0x-monorepo into feature/instant/beta-render-et-al
Diffstat (limited to 'packages/instant/src/util/error.ts')
-rw-r--r--packages/instant/src/util/error.ts64
1 files changed, 64 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..40fd24c7e
--- /dev/null
+++ b/packages/instant/src/util/error.ts
@@ -0,0 +1,64 @@
+import { AssetBuyerError } from '@0x/asset-buyer';
+import { Dispatch } from 'redux';
+
+import { Action, actions } from '../redux/actions';
+import { Asset } from '../types';
+
+import { assetUtils } from './asset';
+
+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, asset?: Asset): string | undefined => {
+ const hasInsufficientLiquidity =
+ error.message === AssetBuyerError.InsufficientAssetLiquidity ||
+ error.message === AssetBuyerError.InsufficientZrxLiquidity;
+ if (hasInsufficientLiquidity) {
+ const assetName = assetUtils.bestNameForAsset(asset, 'of this asset');
+ return `Not enough ${assetName} available`;
+ }
+
+ if (
+ error.message === AssetBuyerError.StandardRelayerApiError ||
+ error.message.startsWith(AssetBuyerError.AssetUnavailable)
+ ) {
+ const assetName = assetUtils.bestNameForAsset(asset, 'This asset');
+ return `${assetName} is currently unavailable`;
+ }
+
+ return undefined;
+};
+
+export const errorUtil = {
+ errorFlasher: new ErrorFlasher(),
+ errorDescription: (error?: any, asset?: Asset): { icon: string; message: string } => {
+ let bestMessage: string | undefined;
+ if (error instanceof Error) {
+ bestMessage = humanReadableMessageForError(error, asset);
+ }
+ return {
+ icon: '😢',
+ message: bestMessage || 'Something went wrong...',
+ };
+ },
+};