-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show error page when runtime exception happens
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
app/src/main/java/it/chalmers/gamma/adapter/primary/web/GammaErrorController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package it.chalmers.gamma.adapter.primary.web; | ||
|
||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.boot.web.servlet.error.ErrorController; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
@Controller | ||
public class GammaErrorController implements ErrorController { | ||
|
||
@GetMapping("/error") | ||
public ModelAndView handleRuntimeException(@RequestHeader(value = "HX-Request", required = false) boolean htmxRequest, HttpServletResponse response) { | ||
response.addHeader("HX-Retarget", "body"); | ||
response.addHeader("HX-Reswap", "innerHTML"); | ||
|
||
ModelAndView mv = new ModelAndView(); | ||
if (htmxRequest) { | ||
mv.setViewName("pages/error"); | ||
} else { | ||
mv.setViewName("index"); | ||
mv.addObject("page", "pages/error"); | ||
} | ||
|
||
return mv; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<header th:replace="~{common/header}"></header> | ||
<main> | ||
<article> | ||
<header> | ||
500 - Internal Server Error | ||
</header> | ||
<p>Something went wrong...</p> | ||
<footer> | ||
<a href="/"> | ||
<button type="button">Go home</button> | ||
</a> | ||
</footer> | ||
</article> | ||
</main> |