I am currently working on an application in which I use Titan DB as a backend and connect to it using Gremlin-Server.
My goal is to be able to throw exceptions in my Groovy scripts and have these exception messages passed back to the gremlin client so that my application can give a proper response to what when wrong during execution. Currently, I do not believe that this is supported in Gremlin Server.
I am currently using the HttpChannelizer defined at gremlin/server/handler/HttpGremlinEndpointHandler.java
Line 255:
evalFuture.exceptionally(t -> {
sendError(ctx, INTERNAL_SERVER_ERROR,
String.format("Error encountered evaluating script: %s", requestArguments.getValue0()), Optional.of(t));
promise.setFailure(t);
return null;
});
Line 447:
private static void sendError(final ChannelHandlerContext ctx, final HttpResponseStatus status,
final String message, final Optional<Throwable> t) {
if (t.isPresent())
logger.warn(String.format("Invalid request - responding with %s and %s", status, message), t.get());
else
logger.warn(String.format("Invalid request - responding with %s and %s", status, message));
errorMeter.mark();
final ObjectNode node = mapper.createObjectNode();
node.put("message", message);
final FullHttpResponse response = new DefaultFullHttpResponse(
HTTP_1_1, status, Unpooled.copiedBuffer(node.toString(), CharsetUtil.UTF_8));
response.headers().set(CONTENT_TYPE, "application/json");
// Close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
This is the part of the handler that deals with exceptions thrown during execution of the gremlin query. As you can see, the sendError is being called such that the HttpResponseStatus code is INTERNAL_SERVER_ERROR(500) and the error message is set to be "Error encountered evaluating script: <prog_name>"
Even though the exception t is passed into the sendError function, the error message is only logger server side and not included in the http response.
I could return error messages in the Http response data by catching exceptions in Groovy and returning strings with the error messages, but this seems gimmicky and prone to some bugs in the future. I want to know if it would be worthwhile to edit the HttpChannelizer to send the exception message to the client if the exception t is present.
What would be the best way to notify the client about specific errors?