Skip to content

Commit 6b31a6a

Browse files
committed
More error handling goodness
1 parent f7f19de commit 6b31a6a

File tree

9 files changed

+82
-93
lines changed

9 files changed

+82
-93
lines changed

convex-benchmarks/src/main/java/convex/benchmarks/LocalPeerBenchmark.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ public void readWriteTransaction() throws TimeoutException, InterruptedException
107107
* Benchmark to test a single small op in a query. Basically the fastest we can
108108
* get a single query result.
109109
* @throws TimeoutException If client times out
110+
* @throws InterruptedException
110111
*/
111112
@Benchmark
112-
public void constantOpQuery() throws TimeoutException {
113+
public void constantOpQuery() throws InterruptedException {
113114
Result r=CONVEX.querySync(Constant.create(CVMLong.ONE));
114115
if (r.isError()) {
115116
throw new Error("Query Failed: "+r.toString());
@@ -121,9 +122,10 @@ public void constantOpQuery() throws TimeoutException {
121122
* get a k/v store query result.
122123
* @throws TimeoutException If client times out
123124
* @throws IOException In case of IO error
125+
* @throws InterruptedException
124126
*/
125127
@Benchmark
126-
public void readWriteOpQuery() throws TimeoutException, IOException {
128+
public void readWriteOpQuery() throws InterruptedException {
127129
Result r=CONVEX.querySync(readWriteCmd);
128130
if (r.isError()) {
129131
throw new Error("Query Failed: "+r.toString());

convex-cli/src/main/java/convex/cli/account/AccountInformation.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package convex.cli.account;
22

3-
import java.util.concurrent.TimeoutException;
4-
53
import org.slf4j.Logger;
64
import org.slf4j.LoggerFactory;
75

86
import convex.api.Convex;
9-
import convex.cli.CLIError;
107
import convex.cli.Constants;
118
import convex.cli.Main;
129
import convex.core.Result;
@@ -65,8 +62,8 @@ public void run() {
6562
try {
6663
result = convex.querySync(message);
6764
mainParent.printResult(result);
68-
} catch (TimeoutException e) {
69-
throw new CLIError("Timeout",e);
65+
} catch (InterruptedException e) {
66+
Thread.currentThread().interrupt();
7067
}
7168
}
7269
}

convex-cli/src/main/java/convex/cli/client/Query.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package convex.cli.client;
22

3-
import java.util.concurrent.TimeoutException;
4-
53
import convex.api.Convex;
6-
import convex.cli.CLIError;
74
import convex.core.Result;
85
import convex.core.data.ACell;
96
import convex.core.lang.Reader;
@@ -45,8 +42,8 @@ public void run() {
4542
break;
4643
}
4744
}
48-
} catch (TimeoutException e) {
49-
throw new CLIError("Query timed out. Perhaps there is a network problem, or the host is not an operational Convex peer?");
45+
} catch (InterruptedException e) {
46+
Thread.currentThread().interrupt();
5047
}
5148
}
5249
}

convex-cli/src/main/java/convex/cli/client/Transact.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package convex.cli.client;
22

3-
import java.util.concurrent.TimeoutException;
4-
53
import org.slf4j.Logger;
64
import org.slf4j.LoggerFactory;
75

@@ -52,8 +50,6 @@ public void run() {
5250
try {
5351
Result result = convex.transactSync(transaction);
5452
mainParent.printResult(result);
55-
} catch (TimeoutException e) {
56-
throw new CLIError(ExitCodes.TEMPFAIL,"Timeout executing transaction",e);
5753
} catch (InterruptedException e) {
5854
Thread.currentThread().interrupt();
5955
}

convex-core/src/main/java/convex/core/Result.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ public RecordFormat getFormat() {
368368
return RESULT_FORMAT;
369369
}
370370

371+
/**
372+
* Constructs a result from a caught exception
373+
* @param e Exception caught
374+
* @return
375+
*/
371376
public static Result fromException(Throwable e) {
372377
if (e instanceof TimeoutException) {
373378
String msg=e.getMessage();
@@ -384,9 +389,23 @@ public static Result fromException(Throwable e) {
384389
if (e instanceof ResultException) {
385390
return ((ResultException) e).getResult();
386391
}
392+
if (e instanceof InterruptedException) {
393+
return interruptThread();
394+
}
387395
return Result.create(null, ErrorCodes.EXCEPTION,Strings.create(e.getMessage()));
388396
}
389397

398+
private static final Result INTERRUPTED_RESULT=Result.error(ErrorCodes.INTERRUPTED,Strings.create("Interrupted!"));
399+
400+
/**
401+
* Returns a Result representing a thread interrup, AND sets the interrupt status on the current thread
402+
* @return
403+
*/
404+
public static Result interruptThread() {
405+
Thread.currentThread().interrupt();
406+
return INTERRUPTED_RESULT;
407+
}
408+
390409

391410

392411

0 commit comments

Comments
 (0)