}
}
// invoke the method
JsonNode result = null;
ObjectNode error = null;
Throwable thrown = null;
try {
result = invoke(method, paramNodes);
} catch (Throwable e) {
thrown = e;
if (InvocationTargetException.class.isInstance(e)) {
e = InvocationTargetException.class.cast(e).getTargetException();
}
error = mapper.createObjectNode();
// use error
boolean errorMapped = false;
for (JsonRpcError em : errorMappings) {
if (em.exception().isInstance(e)) {
error.put("code", em.code());
error.put("message", em.message());
// get data from annotation
String data = em.data();
// default to exception message
if("".equals(data)) {
data = e.getMessage();
}
// only add the data if we have a value
if (data != null && !"".equals(data)) {
error.put("data", data);
}
// we used an annotation for the exception
errorMapped = true;
break;
}
}
// generate generic error response
if (!errorMapped) {
error.put("code", 0);
error.put("message", e.getMessage());
error.put("data", e.getClass().getName());
}
}
// bail if notification request
if (id==null) {
return;
}
// create response
ObjectNode response = mapper.createObjectNode();
response.put("jsonrpc", jsonRpc);
response.put("id", id);
if (error==null) {
response.put("result", result);
} else if (error!=null) {
response.put("error", error);
}
// write it
mapper.writeValue(ops, response);