Files
js/utils/errorParser.tsx
T
752b2c2634 Refactor for react/sdk v3 (#457)
* Refactor claim conditions

* Add util function

* Full NFT pages refactor

* Improve onDashboard

* Token refactor fun

* Fix some logic

* Make token drop use custom contract pages

* Remove all the things

* Fix checks

* Fix delayed reveal

* Fix marketplace embed

* Fix lgtm

* Change detection strategy

* Improve some checks

* Reduce use of actualContract

* Fix lgtm

* Fix build

* Some renamings and some more cleanup

* Default sources to release source if found

* Fix lgtm

* Rename multiwrap

* Use useTokenDecimals hook

* Fix token forms not accepting decimals

* Check decimals for inputs

* Fix logic

* Update packages

* update SDK + add Optimism/Arbitrum goerli testnets

* Add goerli l2s to disabled chains of gnosis

* Add comment

* Fix todo

* Rename detection

* Fix code tab on multiwrap and pack

* Add deprecated text

* Add faucet links

* Prettier comments

* Check isDirty for all forms

* add latest nightly

* upgrade nightly and use stable for storage

Co-authored-by: Joaquim Verges <[email protected]>
Co-authored-by: Jonas Daniels <[email protected]>
2022-09-09 18:16:09 -07:00

131 lines
3.4 KiB
TypeScript

import { Link } from "@chakra-ui/react";
import posthog from "posthog-js";
import { Text } from "tw-components";
const PLEASE_REACH_OUT_MESSAGE = (
<>
<br />
<br />
<Text as="span" color="inherit">
If you believe this is incorrect or the error persists, please reach out
in{" "}
<Link
fontWeight="700"
href="https://discord.gg/thirdweb"
isExternal
textDecor="underline"
>
discord
</Link>
.
</Text>
</>
);
const UNKNOWN_ERROR_MESSAGE = "An unknown error occurred, please try again.";
export function parseErrorToMessage(error: unknown): string | JSX.Element {
const message = parseError(error);
return (
<Text as="span" color="inherit">
{message}
<i>{PLEASE_REACH_OUT_MESSAGE}</i>
</Text>
);
}
function parseError(error: unknown): string | JSX.Element {
// if the error is a straight string just return it
if (typeof error === "string") {
return error;
}
if ((error as any)?.error?.message?.includes("execution reverted:")) {
return (error as any)?.error?.message;
}
// errors with code top level
if (isErrorWithCode(error)) {
const err = parseErrorCode(error);
if (err) {
return err;
}
}
// handle generic error instances
if (error instanceof Error) {
return error.message;
}
// everything that falls through here should be logged and sent to posthog
console.error("unknown error", error);
posthog.capture("unknown_error", { error });
// worst case scenario send a generic error message back
return UNKNOWN_ERROR_MESSAGE;
}
interface ErrorWithCode extends Error {
code: number | string;
data: { code: number; message: string };
stack?: string;
reason?: string;
}
function isErrorWithCode(error: unknown): error is ErrorWithCode {
return (error as ErrorWithCode)?.code !== undefined;
}
function parseErrorCode(error: ErrorWithCode): string | JSX.Element | void {
switch (error.code) {
case "CALL_EXCEPTION": {
if (error.reason) {
return (
<>
The following error happened on the underlying Smart Contract:
<br />
<strong>{error.reason}</strong>
</>
);
}
return "An error occurred on the underlying Smart Contract.";
}
case -32602: {
return "You denied the transaction request.";
}
case -32603: {
if (error.message.includes("LEDGER_LOCKED")) {
return "This app does not currently work with ledger, please connect another way.";
}
if (error.message.includes("MetaMask Message Signature")) {
return `
There was an error with the metamask signature.
If you are using a Ledger wallet, make sure you go to settings
and enable the "Allow Contract Data" setting
`;
}
if (error.data.message.includes("max code size exceeded")) {
return `
This contract is bigger than the size limit (24,567 bytes).
You need to reduce the size of the contract before deploying.
We recommend enabling the optimizer in your compiler.
`;
}
return (
error?.data.message ||
error?.message ||
"An internal error occurred with your transaction."
);
}
case -32002: {
return "There is already a pending request on your wallet to do that. Please check your wallet.";
}
case 4001: {
return "You denied the transaction request.";
}
}
}