-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom
WalletError
class to normalise wallet errors
- Loading branch information
Showing
14 changed files
with
126 additions
and
75 deletions.
There are no files selected for viewing
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
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,36 @@ | ||
/** | ||
* Custom error class which wraps around an error thrown by a wallet. | ||
*/ | ||
export class WalletError extends Error { | ||
/** | ||
* Holds the original error and type thrown by the wallet. | ||
*/ | ||
public raw: unknown; | ||
|
||
constructor(message: string, raw: unknown) { | ||
super(message); | ||
this.name = "WalletError"; | ||
this.raw = raw; | ||
} | ||
|
||
/** | ||
* Returns the result of the `promise` if it resolves successfully, normalising | ||
* any errors thrown into a `WalletError` instance. | ||
* | ||
* It is best to wrap all wallet API calls with this function as some wallets | ||
* throw other data types other than actual `Error` instances. | ||
*/ | ||
public static async wrap<T>(promise: Promise<T>): Promise<T> { | ||
try { | ||
return await promise; | ||
} catch (err) { | ||
if (typeof err === "string") { | ||
throw new WalletError(err, err); | ||
} | ||
if (err instanceof Error) { | ||
throw new WalletError(err.message, err); | ||
} | ||
throw new WalletError("unknown error", err); | ||
} | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.