-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8bd4dc6
commit 4cd2989
Showing
4 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
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
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
46 changes: 46 additions & 0 deletions
46
src/main/java/de/webalf/slotbot/exception/WebResponseEntityExceptionHandler.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,46 @@ | ||
package de.webalf.slotbot.exception; | ||
|
||
import de.webalf.slotbot.model.annotations.ResponseView; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.core.annotation.AnnotationUtils; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
import org.springframework.web.servlet.view.RedirectView; | ||
|
||
/** | ||
* @author Alf | ||
* @since 09.06.2021 | ||
*/ | ||
@ControllerAdvice(annotations = Controller.class) | ||
@Order(2) | ||
@Slf4j | ||
public class WebResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
@ExceptionHandler(value = {ResourceNotFoundException.class, MethodArgumentTypeMismatchException.class}) | ||
protected RedirectView handleConflict(RuntimeException ex) { | ||
return new RedirectView(determineRedirectView(ex)); | ||
} | ||
|
||
/** | ||
* Uses the Annotation from the Exception if available | ||
* | ||
* @param e exception to check | ||
* @return annotated reason or message of exception | ||
*/ | ||
private String determineRedirectView(Exception e) { | ||
ResponseView responseViewAnnotation = AnnotationUtils.findAnnotation(e.getClass(), ResponseView.class); | ||
if (responseViewAnnotation != null) { | ||
return responseViewAnnotation.value(); | ||
} | ||
try { | ||
return (String) ResponseView.class.getMethod("value").getDefaultValue(); | ||
} catch (NoSuchMethodException noSuchMethodException) { | ||
log.error("Failed to get default value of ResponseView class", noSuchMethodException); | ||
return ""; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/de/webalf/slotbot/model/annotations/ResponseView.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,16 @@ | ||
package de.webalf.slotbot.model.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author Alf | ||
* @since 09.06.2021 | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE}) | ||
public @interface ResponseView { | ||
String value() default "/error/404"; | ||
} |