Skip to content

Commit

Permalink
Added web exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Alf-Melmac committed Jun 9, 2021
1 parent 8bd4dc6 commit 4cd2989
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.webalf.slotbot.exception;

import de.webalf.slotbot.model.annotations.ResponseView;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

Expand All @@ -10,8 +11,11 @@
* @since 22.06.2020
*/
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Resource not found")
@ResponseView
public class ResourceNotFoundException extends RuntimeException {

private static final long serialVersionUID = 3327000772094345936L;

public ResourceNotFoundException() {
super();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.webalf.slotbot.util.StringUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -11,6 +12,7 @@
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
Expand All @@ -27,7 +29,8 @@
* @author Alf
* @since 09.08.2020
*/
@ControllerAdvice
@ControllerAdvice(annotations = RestController.class)
@Order(1)
class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(value = {ResourceNotFoundException.class, BusinessRuntimeException.class, ForbiddenException.class,
Expand Down
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 "";
}
}
}
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";
}

0 comments on commit 4cd2989

Please sign in to comment.