Reputation: 2196
I created a custom error page to replace the default whitelabel based on this tutorial. It worked fine but I need to pass other attributes to the page so I changed my code to intercept the error
endpoint based on the geoand's answer here.
Here is my final code:
@Controller
public class ErroHandlerController implements ErrorController {
@Value("${terena.midas.location}")
private String midasLocation;
@RequestMapping("/error")
public String handleError( Model model ) {
model.addAttribute( "midasLocation", midasLocation );
return "error";
}
@Override
public String getErrorPath() {
return "/error";
}
}
Well the code worked sending my variable midasLocation but I lost the error details like path, status,message, etc... How can I bring them back again?
Upvotes: 1
Views: 1892
Reputation: 1980
You need to use the ErrorAttributes which "provides access to error attributes which can be logged or presented to the user".
Take a look:
Basic functionality:
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.context.request.WebRequest;
@Controller
public class ErrorHandler implements ErrorController {
private final ErrorAttributes errorAttributes;
public ErrorHandler(ErrorAttributes errorAttributes) {
this.errorAttributes = errorAttributes;
}
@GetMapping("/error")
public String handleError(Model model, WebRequest webRequest) {
model.addAttribute("midasLocation", "xxx");
final Throwable error = errorAttributes.getError(webRequest);
model.addAttribute("exception", error);
model.addAttribute("message", error == null ? "" : error.getMessage());
return "error";
}
@Override public String getErrorPath() {
return "/error";
}
@GetMapping("/throwErrorForTest")
public String throwError() {
throw new RuntimeException("my exception");
}
}
Upvotes: 1