Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
chore: Fixed sendTransactionError types (#2765)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Woody4618 authored Jun 4, 2024
1 parent 6a420c8 commit 21f241b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 54 deletions.
25 changes: 5 additions & 20 deletions packages/library-legacy/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2927,12 +2927,7 @@ export type SignatureResult = {
/**
* Transaction error
*/
export type TransactionError = {
message: string;
data?: {
logs?: string[];
};
};
export type TransactionError = {} | string;

/**
* Transaction confirmation status
Expand Down Expand Up @@ -5787,17 +5782,12 @@ export class Connection {
console.error(res.error.message, logTrace);
}
}
const transactionError: TransactionError = {
message: res.error.message,
data: {
logs: logs,
},
};

throw new SendTransactionError({
action: 'simulate',
signature: '',
transactionError: transactionError,
transactionMessage: res.error.message,
transactionLogs: logs,
});
}
return res.result;
Expand Down Expand Up @@ -5933,17 +5923,12 @@ export class Connection {
if ('data' in res.error) {
logs = res.error.data.logs;
}
const transactionError: TransactionError = {
message: res.error.message,
data: {
logs: logs,
},
};

throw new SendTransactionError({
action: skipPreflight ? 'send' : 'simulate',
signature: '',
transactionError: transactionError,
transactionMessage: res.error.message,
transactionLogs: logs,
});
}
return res.result;
Expand Down
51 changes: 27 additions & 24 deletions packages/library-legacy/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
import {Connection, TransactionError} from './connection';
import {Connection} from './connection';
import {TransactionSignature} from './transaction';

export class SendTransactionError extends Error {
#signature: TransactionSignature;
#transactionError: TransactionError;
#resolvedLogs: string[] | Promise<string[]> | undefined;
private signature: TransactionSignature;
private transactionMessage: string;
private transactionLogs?: string[];
private resolvedLogs: string[] | Promise<string[]> | undefined;

constructor({
action,
signature,
transactionError,
transactionMessage,
transactionLogs,
}: {
action: 'send' | 'simulate';
signature: TransactionSignature;
transactionError: TransactionError;
transactionMessage: string;
transactionLogs?: string[];
}) {
let message: string;

switch (action) {
case 'send':
message =
`Transaction ${signature} resulted in an error. \n` +
`${transactionError.message}. ` +
(transactionError.data?.logs
? `Logs: \n${JSON.stringify(transactionError.data.logs.slice(-10), null, 2)}. `
`${transactionMessage}. ` +
(transactionLogs
? `Logs: \n${JSON.stringify(transactionLogs.slice(-10), null, 2)}. `
: '') +
'\nCatch the SendTransactionError and call `getLogs()` on it for full details.';
break;
case 'simulate':
message =
`Simulation failed. \nMessage: ${transactionError.message}. \n` +
(transactionError.data?.logs
? `Logs: \n${JSON.stringify(transactionError.data.logs.slice(-10), null, 2)}. `
`Simulation failed. \nMessage: ${transactionMessage}. \n` +
(transactionLogs
? `Logs: \n${JSON.stringify(transactionLogs.slice(-10), null, 2)}. `
: '') +
'\nCatch the SendTransactionError and call `getLogs()` on it for full details.';
break;
Expand All @@ -40,26 +43,26 @@ export class SendTransactionError extends Error {
}
super(message);

this.#signature = signature;
this.#transactionError = transactionError;
this.#resolvedLogs = transactionError.data?.logs
? transactionError.data.logs
: undefined;
this.signature = signature;
this.transactionMessage = transactionMessage;
this.transactionLogs = transactionLogs;
this.resolvedLogs = transactionLogs ? transactionLogs : undefined;
}

get transactionError(): TransactionError {
return this.#transactionError;
get transactionError(): {message: string; logs?: string[]} {
return {message: this.transactionMessage, logs: this.transactionLogs};
}

async getLogs(connection: Connection): Promise<string[]> {
if (this.#resolvedLogs === undefined) {
this.#resolvedLogs = new Promise((resolve, reject) => {
if (this.resolvedLogs === undefined) {
this.resolvedLogs = new Promise((resolve, reject) => {
connection
.getTransaction(this.#signature)
.getTransaction(this.signature)
.then(tx => {
if (tx && tx.meta && tx.meta.logMessages) {
const logs = tx.meta.logMessages;
this.#resolvedLogs = logs;
this.resolvedLogs = logs;
this.transactionLogs = logs;
resolve(logs);
} else {
reject(new Error('Log messages not found'));
Expand All @@ -68,7 +71,7 @@ export class SendTransactionError extends Error {
.catch(reject);
});
}
return await this.#resolvedLogs;
return await this.resolvedLogs;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
TransactionConfirmationStrategy,
} from '../connection';
import type {TransactionSignature} from '../transaction';
import type {ConfirmOptions, TransactionError} from '../connection';
import type {ConfirmOptions} from '../connection';
import {SendTransactionError} from '../errors';

/**
Expand Down Expand Up @@ -95,13 +95,10 @@ export async function sendAndConfirmRawTransaction(

if (status.err) {
if (signature != null) {
const transactionError: TransactionError = {
message: `Status: (${JSON.stringify(status)})`,
};
throw new SendTransactionError({
action: sendOptions?.skipPreflight ? 'send' : 'simulate',
signature: signature,
transactionError: transactionError,
transactionMessage: `Status: (${JSON.stringify(status)})`,
});
}
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Connection, SignatureResult} from '../connection';
import {Transaction} from '../transaction';
import type {ConfirmOptions, TransactionError} from '../connection';
import type {ConfirmOptions} from '../connection';
import type {Signer} from '../keypair';
import type {TransactionSignature} from '../transaction';
import {SendTransactionError} from '../errors';
Expand Down Expand Up @@ -91,13 +91,10 @@ export async function sendAndConfirmTransaction(

if (status.err) {
if (signature != null) {
const transactionError: TransactionError = {
message: `Status: (${JSON.stringify(status)})`,
};
throw new SendTransactionError({
action: 'send',
signature: signature,
transactionError: transactionError,
transactionMessage: `Status: (${JSON.stringify(status)})`,
});
}
throw new Error(
Expand Down

0 comments on commit 21f241b

Please sign in to comment.