Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/vault error class #116

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ts-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PROGRAM_ID, AFFILIATE_PROGRAM_ID, KEEPER_URL } from './src/vault/consta
import { getVaultPdas, getOnchainTime, getLpSupply } from './src/vault/utils';
import { getAmountByShare, getUnmintAmount, calculateWithdrawableAmount } from './src/vault/helper';
import { IDL } from './src/vault/idl';
import VaultError from './src/vault/error';

export default VaultImpl;
export {
Expand All @@ -19,6 +20,8 @@ export {
getAmountByShare,
getUnmintAmount,
calculateWithdrawableAmount,
// Error
VaultError,
};

export type { VaultImplementation, VaultState, AffiliateInfo, ParsedClockState } from './src/vault/types';
Expand Down
2 changes: 1 addition & 1 deletion ts-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mercurial-finance/vault-sdk",
"version": "0.5.2",
"version": "0.5.3",
"description": "Mercurial Vault SDK is a typescript library that allows you to interact with Mercurial v2's vault.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
43 changes: 43 additions & 0 deletions ts-client/src/vault/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { IDL } from './idl';
import { AnchorError } from '@project-serum/anchor';
import { PROGRAM_ID } from './constants';

type Codes = (typeof IDL.errors)[number]['code'];

class VaultError extends Error {
public errorCode: number;
public errorName: string;
public errorMessage: string;

constructor(error: object | Codes) {
let _errorCode = 0;
let _errorName = 'Something went wrong';
let _errorMessage = 'Something went wrong';

if (error instanceof Error) {
const anchorError = AnchorError.parse(JSON.parse(JSON.stringify(error)).logs as string[]);

if (anchorError?.program.toBase58() === PROGRAM_ID) {
_errorCode = anchorError.error.errorCode.number;
_errorName = anchorError.error.errorCode.code;
_errorMessage = anchorError.error.errorMessage;
}
} else {
const idlError = IDL.errors.find((err) => err.code === error);

if (idlError) {
_errorCode = idlError.code;
_errorName = idlError.name;
_errorMessage = idlError.msg;
}
}

super(_errorMessage);

this.errorCode = _errorCode;
this.errorName = _errorName;
this.errorMessage = _errorMessage;
}
}

export default VaultError;
Loading