/
MetaplexError.ts
51 lines (44 loc) · 1.19 KB
/
MetaplexError.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
/** @group Errors */
export class MetaplexError extends Error {
readonly name: string = 'MetaplexError';
readonly source: MetaplexErrorSource;
readonly sourceDetails?: string;
readonly cause?: Error;
constructor(
message: string,
source: MetaplexErrorSource,
sourceDetails?: string,
cause?: Error
) {
super(message);
this.source = source;
this.sourceDetails = sourceDetails;
this.cause = cause;
this.message =
this.message +
`\n\nSource: ${this.getFullSource()}` +
(this.cause ? `\n\nCaused By: ${this.cause}` : '') +
'\n';
}
getCapitalizedSource(): string {
if (this.source === 'sdk' || this.source === 'rpc') {
return this.source.toUpperCase();
}
return this.source[0].toUpperCase() + this.source.slice(1);
}
getFullSource(): string {
const capitalizedSource = this.getCapitalizedSource();
const sourceDetails = this.sourceDetails ? ` > ${this.sourceDetails}` : '';
return capitalizedSource + sourceDetails;
}
toString() {
return `[${this.name}] ${this.message}`;
}
}
/** @group Errors */
export type MetaplexErrorSource =
| 'sdk'
| 'network'
| 'rpc'
| 'plugin'
| 'program';