Add generic error page with stack trace

Add a @ControllerAdvice that catches all exceptions and renders
an error page showing status, error type, message, and full stack
trace in a <pre> block. Uses table-based layout consistent with
the rest of the UI.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-02-27 08:04:48 +01:00
parent 2d74b000c4
commit 8b5bdc0043
3 changed files with 89 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
package be.seeseepuff.webgit.controller;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import java.io.PrintWriter;
import java.io.StringWriter;
@ControllerAdvice
public class ErrorController
{
@ExceptionHandler(Exception.class)
public ModelAndView handleException(HttpServletRequest request, Exception ex)
{
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
ModelAndView mav = new ModelAndView("error");
mav.addObject("status", HttpStatus.INTERNAL_SERVER_ERROR.value());
mav.addObject("error", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
mav.addObject("message", ex.getMessage());
mav.addObject("trace", sw.toString());
mav.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
return mav;
}
}

View File

@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Error</title>
</head>
<body>
<h1>Error</h1>
<table border="1" cellpadding="4" cellspacing="0">
<tr>
<td><b>Status</b></td>
<td th:text="${status}">500</td>
</tr>
<tr>
<td><b>Error</b></td>
<td th:text="${error}">Internal Server Error</td>
</tr>
<tr>
<td><b>Message</b></td>
<td th:text="${message}">Something went wrong</td>
</tr>
</table>
<h2>Stack Trace</h2>
<pre th:text="${trace}">No stack trace available.</pre>
<br/>
<a href="/" target="_top">Back to home</a>
</body>
</html>

View File

@@ -0,0 +1,33 @@
package be.seeseepuff.webgit.controller;
import be.seeseepuff.webgit.service.GitService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.Mockito.doThrow;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest
class ErrorPageTest
{
@Autowired
private MockMvc mockMvc;
@MockitoBean
private GitService gitService;
@Test
void errorPageShowsStackTrace() throws Exception
{
doThrow(new RuntimeException("Something broke")).when(gitService).push("badrepo");
mockMvc.perform(post("/repo/badrepo/push"))
.andExpect(status().is5xxServerError())
.andExpect(content().string(org.hamcrest.Matchers.containsString("Error")))
.andExpect(content().string(org.hamcrest.Matchers.containsString("Stack Trace")));
}
}