@@ -4,65 +4,81 @@ import {TransactionSignature} from './transaction';
4
4
export class SendTransactionError extends Error {
5
5
private signature : TransactionSignature ;
6
6
private transactionMessage : string ;
7
- private logs : string [ ] | Promise < string [ ] > | undefined ;
7
+ private transactionLogs : string [ ] | Promise < string [ ] > | undefined ;
8
8
9
9
constructor ( {
10
10
action,
11
11
signature,
12
12
transactionMessage,
13
- logs : logs ,
13
+ logs,
14
14
} : {
15
15
action : 'send' | 'simulate' ;
16
16
signature : TransactionSignature ;
17
17
transactionMessage : string ;
18
18
logs ?: string [ ] ;
19
19
} ) {
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.' ;
20
25
let message : string ;
21
-
22
26
switch ( action ) {
23
27
case 'send' :
24
28
message =
25
29
`Transaction ${ signature } resulted in an error. \n` +
26
30
`${ 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 ;
31
33
break ;
32
34
case 'simulate' :
33
35
message =
34
36
`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 ;
39
39
break ;
40
- default :
41
- message = 'Unknown action' ;
40
+ default : {
41
+ message = `Unknown action '${ ( ( a : never ) => a ) ( action ) } '` ;
42
+ }
42
43
}
43
44
super ( message ) ;
44
45
45
46
this . signature = signature ;
46
47
this . transactionMessage = transactionMessage ;
47
- this . logs = logs ? logs : undefined ;
48
+ this . transactionLogs = logs ? logs : undefined ;
48
49
}
49
50
50
51
get transactionError ( ) : { message : string ; logs ?: string [ ] } {
51
52
return {
52
53
message : this . transactionMessage ,
53
- logs : Array . isArray ( this . logs ) ? this . logs : undefined ,
54
+ logs : Array . isArray ( this . transactionLogs )
55
+ ? this . transactionLogs
56
+ : undefined ,
54
57
} ;
55
58
}
56
59
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
+
57
73
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 ) => {
60
76
connection
61
77
. getTransaction ( this . signature )
62
78
. then ( tx => {
63
79
if ( tx && tx . meta && tx . meta . logMessages ) {
64
80
const logs = tx . meta . logMessages ;
65
- this . logs = logs ;
81
+ this . transactionLogs = logs ;
66
82
resolve ( logs ) ;
67
83
} else {
68
84
reject ( new Error ( 'Log messages not found' ) ) ;
@@ -71,7 +87,7 @@ export class SendTransactionError extends Error {
71
87
. catch ( reject ) ;
72
88
} ) ;
73
89
}
74
- return await this . logs ;
90
+ return await this . transactionLogs ;
75
91
}
76
92
}
77
93
0 commit comments