Throw and catch exception on query error
@@ -39,25 +39,24 @@ public class ModelsEvaluationHandler extends ThreadedHttpRequestHandler {
Optional<String> version = path.segment(1);
Optional<String> modelName = path.segment(2);
+ try {
if ( ! apiName.isPresent() || ! apiName.get().equalsIgnoreCase(API_ROOT)) {
- return new ErrorResponse(404, "unknown API");
+ throw new IllegalArgumentException("unknown API");
}
if ( ! version.isPresent() || ! version.get().equalsIgnoreCase(VERSION_V1)) {
- return new ErrorResponse(404, "unknown API version");
+ throw new IllegalArgumentException("unknown API version");
}
if ( ! modelName.isPresent()) {
return listAllModels(request);
}
if ( ! modelsEvaluator.models().containsKey(modelName.get())) {
- // TODO: Replace by catching IllegalArgumentException and passing that error message
- return new ErrorResponse(404, "no model with name '" + modelName.get() + "' found");
+ throw new IllegalArgumentException("no model with name '" + modelName.get() + "' found");
}
Model model = modelsEvaluator.models().get(modelName.get());
// The following logic follows from the spec, in that signature and
// output are optional if the model only has a single function.
- // TODO: Try to avoid recreating that logic here
if (path.segments() == 3) {
if (model.functions().size() > 1) {
@@ -76,8 +75,7 @@ public class ModelsEvaluationHandler extends ThreadedHttpRequestHandler {
if (model.functions().size() <= 1) {
return evaluateModel(request, modelName.get());
}
- // TODO: Replace by catching IllegalArgumentException and passing that error message
- return new ErrorResponse(404, "attempt to evaluate model without specifying function");
+ throw new IllegalArgumentException("attempt to evaluate model without specifying function");
}
if (path.segments() == 5) {
@@ -86,6 +84,10 @@ public class ModelsEvaluationHandler extends ThreadedHttpRequestHandler {
}
}
+ } catch (IllegalArgumentException e) {
+ return new ErrorResponse(404, e.getMessage());
+ }
+
return new ErrorResponse(404, "unrecognized request");
}
