From acefeff5f08f123c7c5d372088f2af016e31c599 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Thu, 25 Oct 2018 13:18:17 -0700 Subject: new try/catch pattern and new prop names per code review feedback --- packages/instant/src/components/buy_button.tsx | 33 ++++++++++++++++------ .../src/containers/selected_asset_buy_button.ts | 8 +++--- 2 files changed, 28 insertions(+), 13 deletions(-) (limited to 'packages') diff --git a/packages/instant/src/components/buy_button.tsx b/packages/instant/src/components/buy_button.tsx index f70f2f5be..7004b6a18 100644 --- a/packages/instant/src/components/buy_button.tsx +++ b/packages/instant/src/components/buy_button.tsx @@ -12,10 +12,10 @@ export interface BuyButtonProps { buyQuote?: BuyQuote; assetBuyer?: AssetBuyer; onAwaitingSignature: (buyQuote: BuyQuote) => void; - onProcessingTransaction: (buyQuote: BuyQuote, txnHash: string) => void; + onSignatureDenied: (buyQuote: BuyQuote, preventedError: Error) => void; + onBuyProcessing: (buyQuote: BuyQuote, txnHash: string) => void; onBuySuccess: (buyQuote: BuyQuote, txnHash: string) => void; - onBuyFailure: (buyQuote: BuyQuote, tnxHash?: string) => void; - onBuyPrevented: (buyQuote: BuyQuote, preventedError: Error) => void; + onBuyFailure: (buyQuote: BuyQuote, txnHash?: string) => void; // TODO: make this non-optional } export class BuyButton extends React.Component { @@ -40,19 +40,34 @@ export class BuyButton extends React.Component { if (_.isUndefined(buyQuote) || _.isUndefined(assetBuyer)) { return; } + + let txnHash: string | undefined; this.props.onAwaitingSignature(buyQuote); - let txnHash; try { txnHash = await assetBuyer.executeBuyQuoteAsync(buyQuote); - this.props.onProcessingTransaction(buyQuote, txnHash); - await web3Wrapper.awaitTransactionSuccessAsync(txnHash); - this.props.onBuySuccess(buyQuote, txnHash); } catch (e) { if (e instanceof Error && e.message === AssetBuyerError.SignatureRequestDenied) { - this.props.onBuyPrevented(buyQuote, e); + this.props.onSignatureDenied(buyQuote, e); + } else { + throw e; + } + } + + // Have to let TS know that txHash is definitely defined now + if (!txnHash) { + throw new Error('No txHash available'); + } + + this.props.onBuyProcessing(buyQuote, txnHash); + try { + await web3Wrapper.awaitTransactionSuccessAsync(txnHash); + } catch (e) { + if (e instanceof Error && e.message.startsWith('Transaction failed')) { + this.props.onBuyFailure(buyQuote, txnHash); return; } - this.props.onBuyFailure(buyQuote, txnHash); + throw e; } + this.props.onBuySuccess(buyQuote, txnHash); }; } diff --git a/packages/instant/src/containers/selected_asset_buy_button.ts b/packages/instant/src/containers/selected_asset_buy_button.ts index f55254daf..17cc1a770 100644 --- a/packages/instant/src/containers/selected_asset_buy_button.ts +++ b/packages/instant/src/containers/selected_asset_buy_button.ts @@ -19,10 +19,10 @@ interface ConnectedState { interface ConnectedDispatch { onAwaitingSignature: (buyQuote: BuyQuote) => void; - onProcessingTransaction: (buyQuote: BuyQuote, txnHash: string) => void; + onSignatureDenied: (buyQuote: BuyQuote, error: Error) => void; + onBuyProcessing: (buyQuote: BuyQuote, txnHash: string) => void; onBuySuccess: (buyQuote: BuyQuote, txnHash: string) => void; onBuyFailure: (buyQuote: BuyQuote) => void; - onBuyPrevented: (buyQuote: BuyQuote, error: Error) => void; } const mapStateToProps = (state: State, _ownProps: SelectedAssetBuyButtonProps): ConnectedState => ({ @@ -35,14 +35,14 @@ const mapDispatchToProps = (dispatch: Dispatch, ownProps: SelectedAssetB const newOrderState: OrderState = { processState: OrderProcessState.AWAITING_SIGNATURE }; dispatch(actions.updateBuyOrderState(newOrderState)); }, - onProcessingTransaction: (buyQuote: BuyQuote, txnHash: string) => { + onBuyProcessing: (buyQuote: BuyQuote, txnHash: string) => { const newOrderState: OrderState = { processState: OrderProcessState.PROCESSING, txnHash }; dispatch(actions.updateBuyOrderState(newOrderState)); }, onBuySuccess: (buyQuote: BuyQuote, txnHash: string) => dispatch(actions.updateBuyOrderState({ processState: OrderProcessState.SUCCESS, txnHash })), onBuyFailure: buyQuote => dispatch(actions.updateBuyOrderState({ processState: OrderProcessState.FAILURE })), - onBuyPrevented: (buyQuote, error) => { + onSignatureDenied: (buyQuote, error) => { dispatch(actions.resetAmount()); dispatch(actions.setError(error)); }, -- cgit v1.2.3