Skip to content

Commit

Permalink
feat: move current node account id logging logic into sdk client (#3297)
Browse files Browse the repository at this point in the history
* chore: move the logic into sdk client

Signed-off-by: nikolay <[email protected]>

* chore: resolve comments

Signed-off-by: nikolay <[email protected]>

---------

Signed-off-by: nikolay <[email protected]>
  • Loading branch information
natanasow authored Nov 27, 2024
1 parent ace2a67 commit b021318
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
17 changes: 2 additions & 15 deletions packages/relay/src/lib/clients/sdkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,14 +728,7 @@ export class SDKClient {
throw e;
}

// Log the target node account ID, right now, it's populated only for MaxAttemptsOrTimeout error
if (e?.nodeAccountId) {
this.logger.info(
`${requestDetails.formattedRequestId} Transaction failed to execute against node with id: ${e.nodeAccountId}`
);
}

const sdkClientError = new SDKClientError(e, e.message, transaction.transactionId?.toString());
const sdkClientError = new SDKClientError(e, e.message, transaction.transactionId?.toString(), e.nodeAccountId);

// WRONG_NONCE is one of the special errors where the SDK still returns a valid transactionResponse.
// Throw the WRONG_NONCE error, as additional handling logic is expected in a higher layer.
Expand Down Expand Up @@ -818,13 +811,7 @@ export class SDKClient {
`${requestDetails.formattedRequestId} Successfully execute all ${transactionResponses.length} ${txConstructorName} transactions: callerName=${callerName}, status=${Status.Success}(${Status.Success._code})`,
);
} catch (e: any) {
// Log the target node account ID, right now, it's populated only for MaxAttemptsOrTimeout error
if (e?.nodeAccountId) {
this.logger.info(
`${requestDetails.formattedRequestId} Transaction failed to execute against node with id: ${e.nodeAccountId}`
);
}
const sdkClientError = new SDKClientError(e, e.message);
const sdkClientError = new SDKClientError(e, e.message, undefined, e.nodeAccountId);

this.logger.warn(
`${requestDetails.formattedRequestId} Fail to executeAll for ${txConstructorName} transaction: transactionId=${transaction.transactionId}, callerName=${callerName}, transactionType=${txConstructorName}, status=${sdkClientError.status}(${sdkClientError.status._code})`,
Expand Down
4 changes: 3 additions & 1 deletion packages/relay/src/lib/errors/SDKClientError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ import { Status } from '@hashgraph/sdk';

export class SDKClientError extends Error {
public status: Status = Status.Unknown;
public nodeAccountId: string | undefined;
private validNetworkError: boolean = false;
private failedTransactionId: string | undefined;

constructor(e: any, message?: string, transactionId?: string) {
constructor(e: any, message?: string, transactionId?: string, nodeId?: string | undefined) {
super(e?.status?._code ? e.message : message);

if (e?.status?._code) {
this.validNetworkError = true;
this.status = e.status;
}
this.failedTransactionId = transactionId || '';
this.nodeAccountId = nodeId;
Object.setPrototypeOf(this, SDKClientError.prototype);
}

Expand Down
7 changes: 7 additions & 0 deletions packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,13 @@ export class EthImpl implements Eth {
}

if (e instanceof SDKClientError) {
if (e.nodeAccountId) {
// Log the target node account ID, right now, it's populated only for MaxAttemptsOrTimeout error
this.logger.info(
`${requestDetails.formattedRequestId} Transaction failed to execute against node with id: ${e.nodeAccountId}`,
);
}

this.hapiService.decrementErrorCounter(e.statusCode);
if (e.status.toString() === constants.TRANSACTION_RESULT_STATUS.WRONG_NONCE) {
const mirrorNodeGetContractResultRetries = this.mirrorNodeClient.getMirrorNodeRequestRetryCount();
Expand Down
9 changes: 8 additions & 1 deletion packages/relay/tests/lib/errors/SDKClientError.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
*
*/

import { expect } from 'chai';
import { Status } from '@hashgraph/sdk';
import { expect } from 'chai';

import { SDKClientError } from '../../../src/lib/errors/SDKClientError'; // Update the path to point to the SDKClientError file

describe('SDKClientError', () => {
Expand Down Expand Up @@ -115,4 +116,10 @@ describe('SDKClientError', () => {
expect(error.statusCode).to.equal(invalidStatus._code);
expect(error.isValidNetworkError()).to.be.true;
});

it('should be able to get nodeAccountId', () => {
const nodeId = '0.0.3';
const error = new SDKClientError({}, undefined, undefined, nodeId);
expect(error.nodeAccountId).to.equal(nodeId);
});
});

0 comments on commit b021318

Please sign in to comment.