Skip to content

Commit 0be149d

Browse files
committed
Tidy exception handling
1 parent 1f4fcb8 commit 0be149d

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

convex-peer/src/main/java/convex/api/Convex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public long getSequence(Address addr) throws InterruptedException, ResultExcepti
230230
* Look up the sequence number for an account
231231
* @param origin Account for which to check sequence
232232
* @return Sequence number of account
233-
* @throws ResultException If sequence number could not be obtained
233+
* @throws ResultException If sequence number could not be obtained (invalid account or bad server response)
234234
*/
235235
public long lookupSequence(Address origin) throws InterruptedException, ResultException {
236236
AOp<ACell> code= Special.forSymbol(Symbols.STAR_SEQUENCE);

convex-restapi/src/main/java/convex/restapi/api/McpAPI.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,6 @@ private AMap<AString, ACell> createResponse(AMap<?, ?> request) {
230230
case "tools/call" -> result = toolCall(request.get(FIELD_PARAMS));
231231
default -> result = protocolError(-32601, "Method not found: " + method);
232232
}
233-
} catch (InterruptedException ie) {
234-
Thread.currentThread().interrupt();
235-
result = protocolError(-32603, "Interrupted");
236233
} catch (Exception ex) {
237234
log.warn("Error handling MCP request for method {}", method, ex);
238235
result = protocolError(-32603, "Internal error");
@@ -282,7 +279,7 @@ private AMap<AString, ACell> maybeAttachId(AMap<AString, ACell> response, ACell
282279
return response.assoc(FIELD_ID, idCell);
283280
}
284281

285-
private AMap<AString, ACell> toolCall(ACell paramsCell) throws InterruptedException {
282+
private AMap<AString, ACell> toolCall(ACell paramsCell) {
286283
if (!(paramsCell instanceof AMap<?, ?> params)) {
287284
return protocolError(-32602, "params must be an object");
288285
}
@@ -388,7 +385,7 @@ private class QueryTool extends McpTool {
388385
}
389386

390387
@Override
391-
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws InterruptedException {
388+
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) {
392389
AString sourceCell = RT.ensureString(arguments.get(ARG_SOURCE));
393390
if (sourceCell == null) {
394391
return protocolError(-32602, "Query requires 'source' string");
@@ -406,10 +403,9 @@ public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws Interr
406403
Result result = convex.querySync(form, address);
407404
return toolResult(result);
408405
} catch (InterruptedException e) {
409-
throw e;
410-
} catch (Exception e) {
411-
return toolError("Query execution failed: " + e.getMessage());
412-
}
406+
Thread.currentThread().interrupt();
407+
return toolError("Tool call interrupted");
408+
}
413409
}
414410
}
415411

@@ -419,7 +415,7 @@ private class TransactTool extends McpTool {
419415
}
420416

421417
@Override
422-
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws InterruptedException {
418+
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) {
423419
AString sourceCell = RT.ensureString(arguments.get(ARG_SOURCE));
424420
if (sourceCell == null) {
425421
return protocolError(-32602, "Transact requires 'source' string");
@@ -465,7 +461,7 @@ private class PrepareTool extends McpTool {
465461
}
466462

467463
@Override
468-
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws InterruptedException {
464+
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) {
469465
AString sourceCell = RT.ensureString(arguments.get(ARG_SOURCE));
470466
if (sourceCell == null) {
471467
return protocolError(-32602, "Prepare requires 'source' string");
@@ -499,12 +495,14 @@ public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws Interr
499495
return toolError("sequence must be an integer");
500496
}
501497
sequence = seqLong.longValue();
502-
} else {
503-
try {
504-
sequence = restServer.getConvex().getSequence(address) + 1;
505-
} catch (ResultException e) {
506-
return toolResult(e.getResult());
507-
}
498+
} else try {
499+
500+
sequence = restServer.getConvex().getSequence(address) + 1;
501+
} catch (ResultException e) {
502+
return toolError("Failed to get sequence number "+e.getMessage());
503+
} catch (InterruptedException e) {
504+
Thread.currentThread().interrupt();
505+
return toolError("Tool call interrupted");
508506
}
509507

510508
try {
@@ -605,7 +603,7 @@ private class SubmitTool extends McpTool {
605603
}
606604

607605
@Override
608-
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws InterruptedException {
606+
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) {
609607
AString hashCell = RT.ensureString(arguments.get(Strings.create("hash")));
610608
if (hashCell == null) {
611609
return protocolError(-32602, "Submit requires 'hash' string");
@@ -828,7 +826,7 @@ private class CreateAccountTool extends McpTool {
828826
}
829827

830828
@Override
831-
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws InterruptedException {
829+
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) {
832830
AString accountKeyCell = RT.ensureString(arguments.get(ARG_ACCOUNT_KEY));
833831
if (accountKeyCell == null) {
834832
return protocolError(-32602, "CreateAccount requires 'accountKey' string");
@@ -883,7 +881,7 @@ private class DescribeAccountTool extends McpTool {
883881
}
884882

885883
@Override
886-
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws InterruptedException {
884+
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) {
887885
try {
888886
Address address;
889887
try {
@@ -937,7 +935,7 @@ private class LookupTool extends McpTool {
937935
}
938936

939937
@Override
940-
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws InterruptedException {
938+
public AMap<AString, ACell> handle(AMap<AString, ACell> arguments) {
941939
try {
942940
// Parse address
943941
ACell addressCell = arguments.get(ARG_ADDRESS);

convex-restapi/src/main/java/convex/restapi/mcp/McpTool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public AMap<AString, ACell> getMetadata() {
3939
/**
4040
* Execute the tool using the supplied arguments.
4141
*/
42-
public abstract AMap<AString, ACell> handle(AMap<AString, ACell> arguments) throws InterruptedException;
42+
public abstract AMap<AString, ACell> handle(AMap<AString, ACell> arguments);
4343

4444
/**
4545
* Utility to load tool metadata from a JSON resource.

0 commit comments

Comments
 (0)