-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ece4c42
commit 80eb046
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
packages/checkout/widgets-lib/src/widgets/add-tokens/hooks/useErrorHandler.ts
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { useContext } from 'react'; | ||
import { AddTokensError, AddTokensErrorTypes } from '../types'; | ||
import { EventTargetContext } from '../../../context/event-target-context/EventTargetContext'; | ||
import { sendAddTokensFailedEvent } from '../AddTokensWidgetEvents'; | ||
import { useError } from '../../../lib/squid/hooks/useError'; | ||
import { AddTokensContext } from '../context/AddTokensContext'; | ||
import { useProvidersContext } from '../../../context/providers-context/ProvidersContext'; | ||
|
||
export const useErrorHandler = () => { | ||
const { | ||
addTokensState: { id: contextId }, | ||
} = useContext(AddTokensContext); | ||
|
||
const { | ||
providersState: { | ||
checkout, | ||
}, | ||
} = useProvidersContext(); | ||
|
||
const { showErrorHandover } = useError(checkout.config.environment); | ||
|
||
const { | ||
eventTargetState: { eventTarget }, | ||
} = useContext(EventTargetContext); | ||
|
||
const handleTransactionError = (err: unknown) => { | ||
const reason = `${(err as any)?.reason || (err as any)?.message || '' | ||
}`.toLowerCase(); | ||
|
||
let errorType = AddTokensErrorTypes.WALLET_FAILED; | ||
|
||
if (reason.includes('failed') && reason.includes('open confirmation')) { | ||
errorType = AddTokensErrorTypes.WALLET_POPUP_BLOCKED; | ||
} | ||
|
||
if (reason.includes('rejected') && reason.includes('user')) { | ||
errorType = AddTokensErrorTypes.WALLET_REJECTED; | ||
} | ||
|
||
if ( | ||
reason.includes('failed to submit') | ||
&& reason.includes('highest gas limit') | ||
) { | ||
errorType = AddTokensErrorTypes.WALLET_REJECTED_NO_FUNDS; | ||
} | ||
|
||
if ( | ||
reason.includes('status failed') | ||
|| reason.includes('transaction failed') | ||
) { | ||
errorType = AddTokensErrorTypes.TRANSACTION_FAILED; | ||
sendAddTokensFailedEvent(eventTarget, errorType); | ||
} | ||
|
||
if (reason.includes('unrecognized chain')) { | ||
errorType = AddTokensErrorTypes.UNRECOGNISED_CHAIN; | ||
} | ||
|
||
const error: AddTokensError = { | ||
type: errorType, | ||
data: { error: err }, | ||
}; | ||
|
||
showErrorHandover(errorType, { contextId, error }); | ||
}; | ||
|
||
return { handleTransactionError }; | ||
}; |