4

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.