-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add node
AccountId
to timeout/max attempt errors (#2631)
* feat: Added NodeInfoError class and refactored a bit Executable max attemts/timeout errors, unit tests added Signed-off-by: ivaylogarnev-limechain <[email protected]> * refactor: Added console log of the node ip in the NodeChanel for demo purposes Signed-off-by: ivaylogarnev-limechain <[email protected]> * refactor: Added mapper for the node ips in NodeChannel Signed-off-by: ivaylogarnev-limechain <[email protected]> * refactor: Commented out allNetoworksIp array map Signed-off-by: ivaylogarnev-limechain <[email protected]> * tests: Added unit tests for NodeInfoError Signed-off-by: ivaylogarnev-limechain <[email protected]> * fix: Adjsuted units tests for NodeInfoError, export LocalProvider Signed-off-by: ivaylogarnev-limechain <[email protected]> * fix: Changed the file destination of LocalProvider export Signed-off-by: ivaylogarnev-limechain <[email protected]> * fix: Moved NodeInfoError related tests from executable to the NodeInfo file Signed-off-by: ivaylogarnev-limechain <[email protected]> * refactor: Fixed naming comments, refactored NodeChannel grpc error logic Signed-off-by: ivaylogarnev-limechain <[email protected]> * refactor: Added the node id error on WebChannel Signed-off-by: ivaylogarnev-limechain <[email protected]> * fix: Added JSDOC to the ClientConstants and changed the MaxAttemptsOrTimeoutError nodeId to string Signed-off-by: ivaylogarnev-limechain <[email protected]> * fix: Added additional check for the nodeAccoundId error in Executable.js Signed-off-by: ivaylogarnev-limechain <[email protected]> --------- Signed-off-by: ivaylogarnev-limechain <[email protected]>
- Loading branch information
1 parent
7034135
commit 16a2911
Showing
9 changed files
with
301 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/*- | ||
* | ||
* Hedera JavaScript SDK | ||
* | ||
* Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
/** | ||
* @typedef {object} MaxAttemptsOrTimeoutErrorJSON | ||
* @property {string} message | ||
* @property {string} nodeAccountId | ||
* | ||
*/ | ||
|
||
export default class MaxAttemptsOrTimeoutError extends Error { | ||
/** | ||
* @param {string} message | ||
* @param {string} nodeAccountId | ||
*/ | ||
constructor(message, nodeAccountId) { | ||
// Call the Error constructor with the message | ||
super(message); | ||
|
||
// Assign the nodeAccountId as a custom property | ||
this.nodeAccountId = nodeAccountId; | ||
} | ||
|
||
toJSON() { | ||
return { | ||
message: this.message, | ||
nodeAccountId: this.nodeAccountId, | ||
}; | ||
} | ||
|
||
/** | ||
* @returns {string} | ||
*/ | ||
toString() { | ||
return JSON.stringify(this.toJSON()); | ||
} | ||
|
||
/** | ||
* @returns {MaxAttemptsOrTimeoutErrorJSON} | ||
*/ | ||
valueOf() { | ||
return this.toJSON(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ describe("Executable", function () { | |
), | ||
).to.be.true; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { | ||
AccountId, | ||
TransferTransaction, | ||
Hbar, | ||
MaxAttemptsOrTimeoutError, | ||
} from "../../src/index.js"; | ||
import Mocker from "./Mocker.js"; | ||
|
||
describe("MaxAttemptsOrTimeoutError", function () { | ||
let message; | ||
let nodeAccountId; | ||
let error; | ||
|
||
beforeEach(function () { | ||
message = "Test error message"; | ||
nodeAccountId = "0.0.3"; | ||
|
||
error = new MaxAttemptsOrTimeoutError(message, nodeAccountId); | ||
}); | ||
|
||
it("should create an instance with correct properties", () => { | ||
expect(error).to.be.instanceOf(MaxAttemptsOrTimeoutError); | ||
expect(error.message).to.be.equal(message); | ||
expect(error.nodeAccountId).to.be.equal(nodeAccountId); | ||
}); | ||
|
||
it("toJSON should return correct JSON representation", () => { | ||
const expectedJson = { | ||
message, | ||
nodeAccountId, | ||
}; | ||
|
||
expect(error.toJSON()).to.be.deep.equal(expectedJson); | ||
}); | ||
|
||
it("toString should return a JSON string", () => { | ||
const expectedString = JSON.stringify({ | ||
message, | ||
nodeAccountId, | ||
}); | ||
|
||
expect(error.toString()).to.be.equal(expectedString); | ||
}); | ||
|
||
it("valueOf should return the same result as toJSON", () => { | ||
expect(error.valueOf()).to.be.deep.equal(error.toJSON()); | ||
}); | ||
|
||
describe("Transaction execution errors", function () { | ||
let client, transaction; | ||
|
||
beforeEach(async function () { | ||
const setup = await Mocker.withResponses([]); | ||
client = setup.client; | ||
transaction = new TransferTransaction() | ||
.addHbarTransfer("0.0.2", new Hbar(1)) | ||
.setNodeAccountIds([new AccountId(5)]); | ||
}); | ||
|
||
it("should throw a timeout error when the timeout exceeds", async function () { | ||
// Set the client's request timeout to 0 for testing | ||
client.setRequestTimeout(0); | ||
transaction = transaction.freezeWith(client); | ||
|
||
try { | ||
await transaction.execute(client); | ||
throw new Error("Expected request to time out but it didn't."); | ||
} catch (error) { | ||
expect(error.message).to.include("timeout exceeded"); | ||
expect(error.nodeAccountId).to.equal("0.0.5"); | ||
} | ||
}); | ||
|
||
it("should throw a max attempts error when max attempts is reached", async function () { | ||
// Set the transaction's max attempts to 0 for testing | ||
transaction = transaction.setMaxAttempts(0).freezeWith(client); | ||
|
||
try { | ||
await transaction.execute(client); | ||
throw new Error( | ||
"Expected request to fail due to max attempts being reached.", | ||
); | ||
} catch (error) { | ||
expect(error.message).to.include( | ||
"max attempts of 0 was reached for request with last error being:", | ||
); | ||
expect(error.nodeAccountId).to.equal("0.0.5"); | ||
} | ||
}); | ||
}); | ||
}); |