Skip to content

Commit 21f241b

Browse files
authored
chore: Fixed sendTransactionError types (#2765)
* chore: Fixed sendTransactionError types Reverted TransactionError types and added transactionMessage and logs directly into the SendTransactionError * Fix warning unused field transactionLogs * Remove # and use private instead Remove # and use private instead to target older js version
1 parent 6a420c8 commit 21f241b

File tree

4 files changed

+36
-54
lines changed

4 files changed

+36
-54
lines changed

packages/library-legacy/src/connection.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,12 +2927,7 @@ export type SignatureResult = {
29272927
/**
29282928
* Transaction error
29292929
*/
2930-
export type TransactionError = {
2931-
message: string;
2932-
data?: {
2933-
logs?: string[];
2934-
};
2935-
};
2930+
export type TransactionError = {} | string;
29362931

29372932
/**
29382933
* Transaction confirmation status
@@ -5787,17 +5782,12 @@ export class Connection {
57875782
console.error(res.error.message, logTrace);
57885783
}
57895784
}
5790-
const transactionError: TransactionError = {
5791-
message: res.error.message,
5792-
data: {
5793-
logs: logs,
5794-
},
5795-
};
57965785

57975786
throw new SendTransactionError({
57985787
action: 'simulate',
57995788
signature: '',
5800-
transactionError: transactionError,
5789+
transactionMessage: res.error.message,
5790+
transactionLogs: logs,
58015791
});
58025792
}
58035793
return res.result;
@@ -5933,17 +5923,12 @@ export class Connection {
59335923
if ('data' in res.error) {
59345924
logs = res.error.data.logs;
59355925
}
5936-
const transactionError: TransactionError = {
5937-
message: res.error.message,
5938-
data: {
5939-
logs: logs,
5940-
},
5941-
};
59425926

59435927
throw new SendTransactionError({
59445928
action: skipPreflight ? 'send' : 'simulate',
59455929
signature: '',
5946-
transactionError: transactionError,
5930+
transactionMessage: res.error.message,
5931+
transactionLogs: logs,
59475932
});
59485933
}
59495934
return res.result;

packages/library-legacy/src/errors.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
1-
import {Connection, TransactionError} from './connection';
1+
import {Connection} from './connection';
22
import {TransactionSignature} from './transaction';
33

44
export class SendTransactionError extends Error {
5-
#signature: TransactionSignature;
6-
#transactionError: TransactionError;
7-
#resolvedLogs: string[] | Promise<string[]> | undefined;
5+
private signature: TransactionSignature;
6+
private transactionMessage: string;
7+
private transactionLogs?: string[];
8+
private resolvedLogs: string[] | Promise<string[]> | undefined;
89

910
constructor({
1011
action,
1112
signature,
12-
transactionError,
13+
transactionMessage,
14+
transactionLogs,
1315
}: {
1416
action: 'send' | 'simulate';
1517
signature: TransactionSignature;
16-
transactionError: TransactionError;
18+
transactionMessage: string;
19+
transactionLogs?: string[];
1720
}) {
1821
let message: string;
1922

2023
switch (action) {
2124
case 'send':
2225
message =
2326
`Transaction ${signature} resulted in an error. \n` +
24-
`${transactionError.message}. ` +
25-
(transactionError.data?.logs
26-
? `Logs: \n${JSON.stringify(transactionError.data.logs.slice(-10), null, 2)}. `
27+
`${transactionMessage}. ` +
28+
(transactionLogs
29+
? `Logs: \n${JSON.stringify(transactionLogs.slice(-10), null, 2)}. `
2730
: '') +
2831
'\nCatch the SendTransactionError and call `getLogs()` on it for full details.';
2932
break;
3033
case 'simulate':
3134
message =
32-
`Simulation failed. \nMessage: ${transactionError.message}. \n` +
33-
(transactionError.data?.logs
34-
? `Logs: \n${JSON.stringify(transactionError.data.logs.slice(-10), null, 2)}. `
35+
`Simulation failed. \nMessage: ${transactionMessage}. \n` +
36+
(transactionLogs
37+
? `Logs: \n${JSON.stringify(transactionLogs.slice(-10), null, 2)}. `
3538
: '') +
3639
'\nCatch the SendTransactionError and call `getLogs()` on it for full details.';
3740
break;
@@ -40,26 +43,26 @@ export class SendTransactionError extends Error {
4043
}
4144
super(message);
4245

43-
this.#signature = signature;
44-
this.#transactionError = transactionError;
45-
this.#resolvedLogs = transactionError.data?.logs
46-
? transactionError.data.logs
47-
: undefined;
46+
this.signature = signature;
47+
this.transactionMessage = transactionMessage;
48+
this.transactionLogs = transactionLogs;
49+
this.resolvedLogs = transactionLogs ? transactionLogs : undefined;
4850
}
4951

50-
get transactionError(): TransactionError {
51-
return this.#transactionError;
52+
get transactionError(): {message: string; logs?: string[]} {
53+
return {message: this.transactionMessage, logs: this.transactionLogs};
5254
}
5355

5456
async getLogs(connection: Connection): Promise<string[]> {
55-
if (this.#resolvedLogs === undefined) {
56-
this.#resolvedLogs = new Promise((resolve, reject) => {
57+
if (this.resolvedLogs === undefined) {
58+
this.resolvedLogs = new Promise((resolve, reject) => {
5759
connection
58-
.getTransaction(this.#signature)
60+
.getTransaction(this.signature)
5961
.then(tx => {
6062
if (tx && tx.meta && tx.meta.logMessages) {
6163
const logs = tx.meta.logMessages;
62-
this.#resolvedLogs = logs;
64+
this.resolvedLogs = logs;
65+
this.transactionLogs = logs;
6366
resolve(logs);
6467
} else {
6568
reject(new Error('Log messages not found'));
@@ -68,7 +71,7 @@ export class SendTransactionError extends Error {
6871
.catch(reject);
6972
});
7073
}
71-
return await this.#resolvedLogs;
74+
return await this.resolvedLogs;
7275
}
7376
}
7477

packages/library-legacy/src/utils/send-and-confirm-raw-transaction.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
TransactionConfirmationStrategy,
88
} from '../connection';
99
import type {TransactionSignature} from '../transaction';
10-
import type {ConfirmOptions, TransactionError} from '../connection';
10+
import type {ConfirmOptions} from '../connection';
1111
import {SendTransactionError} from '../errors';
1212

1313
/**
@@ -95,13 +95,10 @@ export async function sendAndConfirmRawTransaction(
9595

9696
if (status.err) {
9797
if (signature != null) {
98-
const transactionError: TransactionError = {
99-
message: `Status: (${JSON.stringify(status)})`,
100-
};
10198
throw new SendTransactionError({
10299
action: sendOptions?.skipPreflight ? 'send' : 'simulate',
103100
signature: signature,
104-
transactionError: transactionError,
101+
transactionMessage: `Status: (${JSON.stringify(status)})`,
105102
});
106103
}
107104
throw new Error(

packages/library-legacy/src/utils/send-and-confirm-transaction.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Connection, SignatureResult} from '../connection';
22
import {Transaction} from '../transaction';
3-
import type {ConfirmOptions, TransactionError} from '../connection';
3+
import type {ConfirmOptions} from '../connection';
44
import type {Signer} from '../keypair';
55
import type {TransactionSignature} from '../transaction';
66
import {SendTransactionError} from '../errors';
@@ -91,13 +91,10 @@ export async function sendAndConfirmTransaction(
9191

9292
if (status.err) {
9393
if (signature != null) {
94-
const transactionError: TransactionError = {
95-
message: `Status: (${JSON.stringify(status)})`,
96-
};
9794
throw new SendTransactionError({
9895
action: 'send',
9996
signature: signature,
100-
transactionError: transactionError,
97+
transactionMessage: `Status: (${JSON.stringify(status)})`,
10198
});
10299
}
103100
throw new Error(

0 commit comments

Comments
 (0)