-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/exception handler #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4e6cd1a
Create GlobalExceptionHandler and error code
FionaSprinkles 3d8597e
fix: status code handling in GlobalExceptionHandler
FionaSprinkles a2b178b
Create error page
FionaSprinkles 248e250
Disable whitelabel
FionaSprinkles c0c718e
Create ErrorController and initial code
FionaSprinkles f3f97f9
Allow custom error page for 403
FionaSprinkles ad5a0d4
Add endpoint matchers
FionaSprinkles 2f5f8f7
Add base path "error"
FionaSprinkles 3c05df1
add: 401 unauthorized
FionaSprinkles 95346b3
use custom error page for 401
FionaSprinkles b9d4765
cleanup unused param
FionaSprinkles 6bb037a
fix: proper HTTP status handling
FionaSprinkles 82123df
fix: correct HTTP status codes
FionaSprinkles ce0e5a3
Merge branch 'main' into feature/exception-handler
FionaSprinkles 708f9fb
fix: resolve JTE layout parameter mismatch in error template
FionaSprinkles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
28 changes: 28 additions & 0 deletions
28
src/main/java/org/example/alfs/controllers/ErrorController.java
This file contains hidden or 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,28 @@ | ||
| package org.example.alfs.controllers; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.ui.Model; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
|
||
| @Controller | ||
| @RequestMapping("/error") | ||
| public class ErrorController { | ||
|
|
||
| @RequestMapping("/403") | ||
| @ResponseStatus(HttpStatus.FORBIDDEN) | ||
| public String forbidden(Model model) { | ||
| model.addAttribute("status", 403); | ||
| model.addAttribute("error", "Access denied"); | ||
| return "error"; | ||
| } | ||
|
|
||
| @RequestMapping("/401") | ||
| @ResponseStatus(HttpStatus.UNAUTHORIZED) | ||
| public String unauthorized(Model model) { | ||
| model.addAttribute("status", 401); | ||
| model.addAttribute("error", "You need to log in to access this page"); | ||
| return "error"; | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
src/main/java/org/example/alfs/exceptions/GlobalExceptionHandler.java
This file contains hidden or 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,42 @@ | ||
| package org.example.alfs.exceptions; | ||
|
|
||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.ui.Model; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.server.ResponseStatusException; | ||
|
|
||
| @ControllerAdvice | ||
| public class GlobalExceptionHandler { | ||
|
|
||
| @ExceptionHandler(ResponseStatusException.class) | ||
| public String handleResponseStatusException( | ||
| ResponseStatusException ex, | ||
| Model model, | ||
| HttpServletResponse response | ||
| ) { | ||
|
|
||
| int status = ex.getStatusCode().value(); | ||
| response.setStatus(status); | ||
|
|
||
| model.addAttribute("status", status); | ||
| model.addAttribute("error", ex.getReason()); | ||
|
|
||
| return "error"; | ||
| } | ||
|
|
||
| @ExceptionHandler(Exception.class) | ||
| public String handleException( | ||
| Exception ex, | ||
| Model model, | ||
| HttpServletResponse response | ||
| ) { | ||
|
|
||
| response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); | ||
|
|
||
| model.addAttribute("status", 500); | ||
| model.addAttribute("error", "Something went wrong"); | ||
|
|
||
| return "error"; | ||
| } | ||
| } | ||
This file contains hidden or 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,26 @@ | ||
| @param String error | ||
| @param Integer status | ||
| @param String success | ||
|
|
||
|
|
||
| @template.layout(title = "Error", content = @` | ||
| <div class="max-w-xl mx-auto mt-16 text-center"> | ||
|
|
||
| <h1 class="text-3xl font-serif text-[#1a2b49] mb-6"> | ||
| Something went wrong | ||
| </h1> | ||
|
|
||
| <p class="text-lg mb-4"> | ||
| Status: ${status} | ||
| </p> | ||
|
|
||
| <p class="text-gray-700 mb-6"> | ||
| ${error} | ||
| </p> | ||
|
|
||
| <a href="/startPage" class="btn-primary px-6 py-3 rounded"> | ||
| Back to start page | ||
| </a> | ||
|
|
||
| </div> | ||
| ` , success = null) |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.