-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
errors.ts
54 lines (51 loc) · 1.82 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
import { ZeroBytes32 } from '@fuel-ts/constants';
import type {
CallResult,
TransactionResultLogDataReceipt,
TransactionResultLogReceipt,
TransactionResultRevertReceipt,
} from '@fuel-ts/providers';
import { ReceiptType } from '@fuel-ts/providers';
const bigintReplacer = (key: unknown, value: unknown) =>
typeof value === 'bigint' ? value.toString() : value;
const printLineWithId = (id: string, line: string) =>
`${id === ZeroBytes32 ? 'script' : id}: ${line}`;
export class ScriptResultDecoderError extends Error {
constructor(result: CallResult, message: string) {
const revertReceipts = result.receipts.filter(
(r) => r.type === ReceiptType.Revert
) as TransactionResultRevertReceipt[];
const revertsText = revertReceipts.length
? `Reverts:\n${revertReceipts
.map(({ type, id, ...r }) =>
printLineWithId(id, `${r.val} ${JSON.stringify(r, bigintReplacer)}`)
)
.join('\n')}`
: null;
const logReceipts = result.receipts.filter(
(r) => r.type === ReceiptType.Log || r.type === ReceiptType.LogData
) as Array<TransactionResultLogReceipt | TransactionResultLogDataReceipt>;
const logsText = logReceipts.length
? `Logs:\n${logReceipts
.map(({ type, id, ...r }) =>
printLineWithId(
id,
`${
type === ReceiptType.LogData ? (r as TransactionResultLogDataReceipt).data : r.val0
}`
)
)
.join('\n')}`
: null;
const receiptsText = `Receipts:\n${JSON.stringify(
result.receipts.map(({ type, ...r }) => ({ type: ReceiptType[type], ...r })),
bigintReplacer,
2
)}`;
super(
`${message}\n\n${revertsText ? `${revertsText}\n\n` : ''}${
logsText ? `${logsText}\n\n` : ''
}${receiptsText}`
);
}
}