Skip to content

Commit

Permalink
Updated error handling in examples and excluded example with precompile.
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Lebedev <[email protected]>
  • Loading branch information
thenswan committed Oct 18, 2023
1 parent 9350202 commit 382a322
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 37 deletions.
1 change: 1 addition & 0 deletions examples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ task runAllExamples {
.filter { it.name.endsWith('Example.java') }
.map { it.name.replaceAll('\\.java$', '') }
.filter { !it.equals('ValidateChecksumExample') } // disabled this example, because it needs user input (but it WORKS)
.filter { !it.equals('ZeroTokenOperationsExample') } // doesn't work with hedera-local-node
.filter { !it.equals('SolidityPrecompileExample') } // doesn't work with hedera-local-node
.filter { !it.equals('ConsensusPubSubChunkedExample') } // doesn't work on CI
.toList()
Expand Down
26 changes: 11 additions & 15 deletions examples/src/main/java/ContractHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,18 @@ public ContractHelper executeSteps(
.setValidateStatus(false)
.getRecord(client);

try {
if (record.receipt.status != Status.SUCCESS) {
throw new Exception("transaction receipt yielded unsuccessful response code " + record.receipt.status);
}
ContractFunctionResult functionResult = Objects.requireNonNull(record.contractFunctionResult);
System.out.println("gas used: " + functionResult.gasUsed);
if (getResultValidator(stepIndex).apply(functionResult)) {
System.out.println("step " + stepIndex + " completed, and returned valid result. (TransactionId \"" + record.transactionId + "\")");
} else {
throw new Exception("returned invalid result");
}
} catch (Throwable error) {
System.out.println("Error occurred in step " + stepIndex + ": " + error.getMessage());
System.out.println("Transaction record: " + record);
break;
if (record.receipt.status != Status.SUCCESS) {
throw new Exception("transaction receipt yielded unsuccessful response code " + record.receipt.status);
}
ContractFunctionResult functionResult = Objects.requireNonNull(record.contractFunctionResult);
System.out.println("gas used: " + functionResult.gasUsed);
if (getResultValidator(stepIndex).apply(functionResult)) {
System.out.println("step " + stepIndex + " completed, and returned valid result. (TransactionId \"" + record.transactionId + "\")");
} else {
throw new Exception("returned invalid result");
}

break;
}
return this;
}
Expand Down
5 changes: 2 additions & 3 deletions examples/src/main/java/ContractNoncesExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private ContractNoncesExample() {
}

public static void main(String[] args)
throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException {
throws Exception {
Client client = ClientHelper.forName(HEDERA_NETWORK);

// Defaults the operator account ID and key such that all generated transactions will be paid for
Expand Down Expand Up @@ -90,8 +90,7 @@ public static void main(String[] args)
.getReceipt(client);

if (contractDeleteResult.status != Status.SUCCESS) {
System.out.println("error deleting contract: " + contractDeleteResult.status);
return;
throw new Exception("error deleting contract: " + contractDeleteResult.status);
}
System.out.println("Contract successfully deleted");
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/main/java/CreateAccountWithAliasExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private CreateAccountWithAliasExample() {
- Sign the `AccountCreateTransaction` transaction with the new private key
- Get the `AccountInfo` and show that the account has contractAccountId
*/
public static void main(String[] args) throws PrecheckStatusException, TimeoutException, InterruptedException {
public static void main(String[] args) throws Exception {
Client client = ClientHelper.forName(HEDERA_NETWORK);

// Defaults the operator account ID and key such that all generated transactions will be paid for
Expand Down Expand Up @@ -85,7 +85,7 @@ public static void main(String[] args) throws PrecheckStatusException, TimeoutEx
if (accountInfo.contractAccountId != null) {
System.out.println("The new account has alias " + accountInfo.contractAccountId);
} else {
System.out.println("The new account doesn't have alias");
throw new Exception("The new account doesn't have alias");
}
}
}
5 changes: 2 additions & 3 deletions examples/src/main/java/CreateSimpleContractExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private CreateSimpleContractExample() {
}

public static void main(String[] args)
throws PrecheckStatusException, IOException, TimeoutException, ReceiptStatusException, InterruptedException {
throws Exception {
String byteCodeHex = ContractHelper.getBytecodeHex("hello_world.json");

Client client = ClientHelper.forName(HEDERA_NETWORK);
Expand Down Expand Up @@ -122,8 +122,7 @@ public static void main(String[] args)
.getReceipt(client);

if (contractDeleteResult.status != Status.SUCCESS) {
System.out.println("error deleting contract: " + contractDeleteResult.status);
return;
throw new Exception("error deleting contract: " + contractDeleteResult.status);
}
System.out.println("Contract successfully deleted");
}
Expand Down
8 changes: 3 additions & 5 deletions examples/src/main/java/CreateStatefulContractExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private CreateStatefulContractExample() {
}

public static void main(String[] args)
throws PrecheckStatusException, TimeoutException, IOException, ReceiptStatusException, InterruptedException {
throws Exception {
String byteCodeHex = ContractHelper.getBytecodeHex("stateful.json");

Client client = ClientHelper.forName(HEDERA_NETWORK);
Expand Down Expand Up @@ -105,8 +105,7 @@ public static void main(String[] args)
.execute(client);

if (contractCallResult.errorMessage != null) {
System.out.println("error calling contract: " + contractCallResult.errorMessage);
return;
throw new Exception("error calling contract: " + contractCallResult.errorMessage);
}

String message = contractCallResult.getString(0);
Expand All @@ -132,8 +131,7 @@ public static void main(String[] args)
.execute(client);

if (contractUpdateResult.errorMessage != null) {
System.out.println("error calling contract: " + contractUpdateResult.errorMessage);
return;
throw new Exception("error calling contract: " + contractUpdateResult.errorMessage);
}

String message2 = contractUpdateResult.getString(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private ScheduleIdenticalTransactionExample() {
}

public static void main(String[] args)
throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException {
throws Exception {
Client client = ClientHelper.forName(HEDERA_NETWORK);

// Defaults the operator account ID and key such that all generated transactions will be paid for
Expand Down Expand Up @@ -151,8 +151,7 @@ public static void main(String[] args)
}

if (!scheduleID.equals(Objects.requireNonNull(loopReceipt.scheduleId))) {
System.out.println("invalid generated schedule id, expected " + scheduleID + ", got " + loopReceipt.scheduleId);
return;
throw new Exception("invalid generated schedule id, expected " + scheduleID + ", got " + loopReceipt.scheduleId);
}

// If the status return by the receipt is related to already created, execute a schedule sign transaction
Expand All @@ -167,8 +166,7 @@ public static void main(String[] args)
.setTransactionId(signTransaction.transactionId)
.execute(client);
if (signReceipt.status != Status.SUCCESS && signReceipt.status != Status.SCHEDULE_ALREADY_EXECUTED) {
System.out.println("Bad status while getting receipt of schedule sign with operator " + operatorId + ": " + signReceipt.status);
return;
throw new Exception("Bad status while getting receipt of schedule sign with operator " + operatorId + ": " + signReceipt.status);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions examples/src/main/java/ScheduledTransferExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private ScheduledTransferExample() {
}

public static void main(String[] args)
throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException {
throws Exception {
Client client = ClientHelper.forName(HEDERA_NETWORK);

// Defaults the operator account ID and key such that all generated transactions will be paid for
Expand Down Expand Up @@ -169,9 +169,7 @@ public static void main(String[] args)
System.out.println("The scheduled transfer transaction from Bob's POV:");
System.out.println(scheduledTransfer);
} else {
System.out.println("The scheduled transaction was not a transfer transaction.");
System.out.println("Something has gone horribly wrong. Crashing...");
System.exit(-1);
throw new Exception("The scheduled transaction was not a transfer transaction.");
}

new ScheduleSignTransaction()
Expand Down

0 comments on commit 382a322

Please sign in to comment.