0

I am trying to design a common error page for Spring MVC using Mustache. I am able to print out the exception type (example: Internal Server Error) using variable "error" and the exception message using the variable called "message".

My Sample Mustache Template:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Common Error</title>
    <h2>Unexpected Error: </h2>
    <h3>Error: {{error}}</h3>
    <h3>Message: {{message}}</h3>
</head>
<body>

</body>
</html>

This shows up as:

Unexpected Error:

Error: Internal Server Error

Message: Unexpected Runtime Error Message

Question 1: How do I print out the Exception Stacktrace?

Question 2: Is there a way to print all the model variable available to the mustache template?

owaism
  • 1,118
  • 1
  • 8
  • 21
  • Check http://stackoverflow.com/questions/1149703/how-can-i-convert-a-stack-trace-to-a-string for how to get stacktrace as string. Note: Printing stacktrace in the UI may expose information like folder path, database table names which can be used to compromise the system. – seenukarthi Jul 30 '15 at 16:45
  • @KarthikeyanVaithilingam Wanted to know how to do it with Mustache. Also this is an internal application. Showing up the Stacktrace is desirable. – owaism Jul 30 '15 at 17:12
  • Did you get to know how to do the same for generic error/code.jsp fashion? my model keeps getting overwritten by default mustach properties (error, status, message) – Daniel Hajduk Dec 06 '16 at 12:34

1 Answers1

0

Rather than resolving this at the Mustache Template engine layer. I stepped back to the have Spring MVC setup the Model properly for the Mustache Error View.

I did this by using @ControllerAdvice and @ExceptionHandler.

My Controller Advice with catch-all exception handler looks like below:

@ControllerAdvice
public class ExceptionControllerAdvice {

    @ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
    @ExceptionHandler(Throwable.class)
    public ModelAndView genericExceptionHandler(Exception ex){

        String errorMessage = getMessage(ex);
        String errorRootCauseMessage = getRootCauseMessage(ex);
        String errorStacktrace = getStackTrace(ex);
        ModelAndView mv = new ModelAndView("errors/error");
        Map<String, Object> model = mv.getModelMap();
        model.put("errorMessage", errorMessage);
        model.put("rootCauseErrorMessage", errorRootCauseMessage);
        model.put("stackTrace", errorStacktrace);
        return mv;
    }
}

And then my Mustache Template is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Generic Error</title>
    <h3>Wahooo!!! You broke us! Well done. We will take a look at it and try to fix the issue.</h3>
    <h4>What Happend?</h4><h5> {{errorMessage}}<h5>
    <h4>What was the root cause? </h4><h5>{{rootCauseErrorMessage}}</h5>
    <h4>Stacktrace:</h4>
    <h6>{{stackTrace}}</h6>
</head>
<body>

</body>
</html>

This solved my issue.

owaism
  • 1,118
  • 1
  • 8
  • 21