-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patherrors.ts
165 lines (154 loc) · 5.32 KB
/
errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { ApiPromise } from "@polkadot/api";
import { SubmittableExtrinsic } from "@polkadot/api/types";
import type { DispatchError } from "@polkadot/types/interfaces";
import { ISubmittableResult } from "@polkadot/types/types";
import { AnyTuple } from "@polkadot/types-codec/types";
import { InsufficientBalanceError } from "@threefold/types";
interface ITFChainError {
message: string;
keyError?: string;
section?: string;
method?: string;
args?: AnyTuple | string[];
docs?: string[];
}
interface TFChainError {
message: string;
keyError?: string;
section?: string;
method?: string;
args?: AnyTuple | string[];
docs?: string[];
}
class TFChainError extends Error {
constructor(options: ITFChainError) {
super(options.message);
this.name = "TFChainError";
if (!options.keyError) {
options.keyError = "GenericError";
}
Object.keys(options).forEach(key => {
if (options[key as keyof ITFChainError]) {
Object.defineProperty(this, key, {
value: options[key as keyof ITFChainError],
enumerable: true,
});
}
});
}
}
class TFChainErrorWrapper {
error: DispatchError | Error;
extrinsic: SubmittableExtrinsic<"promise", ISubmittableResult>;
api: ApiPromise;
constructor(
error: DispatchError | Error,
extrinsic: SubmittableExtrinsic<"promise", ISubmittableResult>,
api: ApiPromise,
) {
this.error = error;
this.extrinsic = extrinsic;
this.api = api;
}
/**
* Throws a TFChainError based on the type of error encountered:
* - If the error is a dispatch error:
* - If it is a module error, throws a TFChainError with module details.
* - If it is a token error, throws a TFChainError with token details.
* - If it is an arithmetic error, throws a TFChainError with arithmetic details.
* - If it is a bad origin error, throws a TFChainError for bad origin.
* - If it is an unknown dispatch error, throws a TFChainError for unknown dispatch error.
* - If the error is an instance of Error:
* - If the message includes "Inability to pay some fees", throws an InsufficientBalanceError.
* - Otherwise, throws a TFChainError for a generic error.
* - If the error is not a dispatch error or an instance of Error, throws a TFChainError for an unknown error.
* @throw {TFChainError} The specific TFChainError thrown based on the encountered error type.
*/
throw(): TFChainError {
const extrinsicArgs = this.extrinsic.method.args;
const extrinsicMethod = this.extrinsic.method.method;
const extrinsicSection = this.extrinsic.method.section;
const errorMessage = `Failed to apply '${extrinsicMethod}' on section '${extrinsicSection}' with args '${extrinsicArgs}.`;
if (this.isDispatchError(this.error)) {
if (this.error.isModule) {
const decoded = this.api.registry.findMetaError(this.error.asModule);
const { section, name, docs } = decoded;
throw new TFChainError({
message: `Module Error: ${errorMessage}'`,
keyError: name,
section,
method: extrinsicMethod,
args: extrinsicArgs,
docs,
});
} else if (this.error.isToken) {
const tokenError = this.error.asToken.toHuman();
throw new TFChainError({
message: `Token Error: ${errorMessage}`,
keyError: JSON.stringify(tokenError),
args: extrinsicArgs,
method: extrinsicMethod,
section: extrinsicSection,
docs: [],
});
} else if (this.error.isArithmetic) {
const tokenError = this.error.asArithmetic.toHuman();
throw new TFChainError({
message: `Arithmetic Error: ${errorMessage}`,
keyError: JSON.stringify(tokenError),
args: extrinsicArgs,
method: extrinsicMethod,
section: extrinsicSection,
docs: [],
});
} else if (this.error.isBadOrigin) {
throw new TFChainError({
message: `Bad Origin Error: ${errorMessage}`,
keyError: "BadOriginError",
args: extrinsicArgs,
method: extrinsicMethod,
section: extrinsicSection,
docs: [],
});
} else {
throw new TFChainError({
message: `Unknown Dispatch Error: ${errorMessage}`,
keyError: "UnknownDispatchError",
args: extrinsicArgs,
method: extrinsicMethod,
section: extrinsicSection,
docs: [],
});
}
} else if (this.error instanceof Error) {
if (this.error.message.includes("Inability to pay some fees")) {
throw new InsufficientBalanceError(`Insufficient Balance Error: ${errorMessage}`);
} else {
throw new TFChainError({
message: `Generic Error: ${errorMessage}`,
keyError: "GenericError",
});
}
} else {
throw new TFChainError({
message: `Unknown Error: ${errorMessage}`,
keyError: "UnknownError",
});
}
}
private isDispatchError(e: any): e is DispatchError {
return (
e &&
typeof e === "object" &&
"isModule" in e &&
"isToken" in e &&
"isArithmetic" in e &&
"isBadOrigin" in e &&
"asModule" in e &&
"asToken" in e &&
"asArithmetic" in e &&
"asBadOrigin" in e
);
}
}
export { TFChainErrorWrapper, TFChainError, ITFChainError };