Skip to content

Commit 327dc9d

Browse files
fix: Add getter for logs to sendTransactionError (#2771)
* Add getter for logs to sendTransactionError - Add a getter for logs with the old types to be a non breaking change * Add check for promise * Do a little tidying: log deduplication, different promise checking, add return type to `logs` getter. --------- Co-authored-by: steveluscher <[email protected]>
1 parent be59290 commit 327dc9d

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

packages/library-legacy/src/errors.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,81 @@ import {TransactionSignature} from './transaction';
44
export class SendTransactionError extends Error {
55
private signature: TransactionSignature;
66
private transactionMessage: string;
7-
private logs: string[] | Promise<string[]> | undefined;
7+
private transactionLogs: string[] | Promise<string[]> | undefined;
88

99
constructor({
1010
action,
1111
signature,
1212
transactionMessage,
13-
logs: logs,
13+
logs,
1414
}: {
1515
action: 'send' | 'simulate';
1616
signature: TransactionSignature;
1717
transactionMessage: string;
1818
logs?: string[];
1919
}) {
20+
const maybeLogsOutput = logs
21+
? `Logs: \n${JSON.stringify(logs.slice(-10), null, 2)}. `
22+
: '';
23+
const guideText =
24+
'\nCatch the `SendTransactionError` and call `getLogs()` on it for full details.';
2025
let message: string;
21-
2226
switch (action) {
2327
case 'send':
2428
message =
2529
`Transaction ${signature} resulted in an error. \n` +
2630
`${transactionMessage}. ` +
27-
(logs
28-
? `Logs: \n${JSON.stringify(logs.slice(-10), null, 2)}. `
29-
: '') +
30-
'\nCatch the SendTransactionError and call `getLogs()` on it for full details.';
31+
maybeLogsOutput +
32+
guideText;
3133
break;
3234
case 'simulate':
3335
message =
3436
`Simulation failed. \nMessage: ${transactionMessage}. \n` +
35-
(logs
36-
? `Logs: \n${JSON.stringify(logs.slice(-10), null, 2)}. `
37-
: '') +
38-
'\nCatch the SendTransactionError and call `getLogs()` on it for full details.';
37+
maybeLogsOutput +
38+
guideText;
3939
break;
40-
default:
41-
message = 'Unknown action';
40+
default: {
41+
message = `Unknown action '${((a: never) => a)(action)}'`;
42+
}
4243
}
4344
super(message);
4445

4546
this.signature = signature;
4647
this.transactionMessage = transactionMessage;
47-
this.logs = logs ? logs : undefined;
48+
this.transactionLogs = logs ? logs : undefined;
4849
}
4950

5051
get transactionError(): {message: string; logs?: string[]} {
5152
return {
5253
message: this.transactionMessage,
53-
logs: Array.isArray(this.logs) ? this.logs : undefined,
54+
logs: Array.isArray(this.transactionLogs)
55+
? this.transactionLogs
56+
: undefined,
5457
};
5558
}
5659

60+
/* @deprecated Use `await getLogs()` instead */
61+
get logs(): string[] | undefined {
62+
const cachedLogs = this.transactionLogs;
63+
if (
64+
cachedLogs != null &&
65+
typeof cachedLogs === 'object' &&
66+
'then' in cachedLogs
67+
) {
68+
return undefined;
69+
}
70+
return cachedLogs;
71+
}
72+
5773
async getLogs(connection: Connection): Promise<string[]> {
58-
if (!Array.isArray(this.logs)) {
59-
this.logs = new Promise((resolve, reject) => {
74+
if (!Array.isArray(this.transactionLogs)) {
75+
this.transactionLogs = new Promise((resolve, reject) => {
6076
connection
6177
.getTransaction(this.signature)
6278
.then(tx => {
6379
if (tx && tx.meta && tx.meta.logMessages) {
6480
const logs = tx.meta.logMessages;
65-
this.logs = logs;
81+
this.transactionLogs = logs;
6682
resolve(logs);
6783
} else {
6884
reject(new Error('Log messages not found'));
@@ -71,7 +87,7 @@ export class SendTransactionError extends Error {
7187
.catch(reject);
7288
});
7389
}
74-
return await this.logs;
90+
return await this.transactionLogs;
7591
}
7692
}
7793

0 commit comments

Comments
 (0)