I built a restful api using sparkjava. I run the server from terminal, and wish to print out Exceptions there for debugging. But it prints nothing even if the API 500s.
Is there a way to print out all exceptions handle by spark?
You can add an ExceptionHandler to spark to handle all Exceptions:
Spark.exception(Exception.class, (e, request, response) -> {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
e.printStackTrace(pw);
System.err.println(sw.getBuffer().toString());
});
Since you are also given the request and response, you can even manage how the server responds to the client when an Exception is caught.