creating big error util file per francescos comment in PR

This commit is contained in:
Steve Klebanoff
2018-10-17 13:42:28 -07:00
parent d052342df7
commit 155858de6e
5 changed files with 56 additions and 55 deletions

View File

@@ -4,7 +4,7 @@ import { connect } from 'react-redux';
import { SlidingError } from '../components/sliding_error';
import { LatestErrorDisplay, State } from '../redux/reducer';
import { errorDescription } from '../util/error_description';
import { errorUtil } from '../util/error';
export interface LatestErrorComponentProps {
assetData?: string;
@@ -16,7 +16,7 @@ export const LatestErrorComponent: React.StatelessComponent<LatestErrorComponent
if (!props.latestError) {
return <div />;
}
const { icon, message } = errorDescription(props.latestError, props.assetData);
const { icon, message } = errorUtil.errorDescription(props.latestError, props.assetData);
return <SlidingError direction={props.slidingDirection} icon={icon} message={message} />;
};

View File

@@ -12,7 +12,7 @@ import { State } from '../redux/reducer';
import { ColorOption } from '../style/theme';
import { AsyncProcessState } from '../types';
import { assetBuyer } from '../util/asset_buyer';
import { errorFlasher } from '../util/error_flasher';
import { errorUtil } from '../util/error';
import { AssetAmountInput } from '../components/asset_amount_input';
@@ -49,9 +49,9 @@ const updateBuyQuoteAsync = async (
let newBuyQuote: BuyQuote | undefined;
try {
newBuyQuote = await assetBuyer.getBuyQuoteAsync(assetData, baseUnitValue);
errorFlasher.clearError(dispatch);
errorUtil.errorFlasher.clearError(dispatch);
} catch (error) {
errorFlasher.flashNewError(dispatch, error);
errorUtil.errorFlasher.flashNewError(dispatch, error);
return;
}

View File

@@ -0,0 +1,51 @@
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 => {
if (error.message === AssetBuyerError.InsufficientAssetLiquidity) {
const assetName = assetDataUtil.bestNameForAsset(assetData, 'of this asset');
return `Not enough ${assetName} available`;
}
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...',
};
},
};

View File

@@ -1,23 +0,0 @@
import { AssetBuyerError } from '@0xproject/asset-buyer';
import { assetDataUtil } from '../util/asset_data';
const humanReadableMessageForError = (error: Error, assetData?: string): string | undefined => {
if (error.message === AssetBuyerError.InsufficientAssetLiquidity) {
const assetName = assetDataUtil.bestNameForAsset(assetData, 'of this asset');
return `Not enough ${assetName} available`;
}
return undefined;
};
export const 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...',
};
};

View File

@@ -1,27 +0,0 @@
import { Dispatch } from 'redux';
import { Action, actions } from '../redux/actions';
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);
}
}
}
export const errorFlasher = new ErrorFlasher();