From 32ed7e9d15a052e7fa38f0a95da0c6dc195455fb Mon Sep 17 00:00:00 2001 From: ddangelorb Date: Fri, 13 Aug 2021 16:02:51 -0300 Subject: [PATCH] Adding exception classes --- .../exception/CustomErrorAttributes.java | 35 +++++++++++++++++ .../wpdauth/exception/CustomException.java | 24 ++++++++++++ .../GlobalExceptionHandlerController.java | 39 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 src/main/java/org/waterproofingdata/wpdauth/exception/CustomErrorAttributes.java create mode 100644 src/main/java/org/waterproofingdata/wpdauth/exception/CustomException.java create mode 100644 src/main/java/org/waterproofingdata/wpdauth/exception/GlobalExceptionHandlerController.java diff --git a/src/main/java/org/waterproofingdata/wpdauth/exception/CustomErrorAttributes.java b/src/main/java/org/waterproofingdata/wpdauth/exception/CustomErrorAttributes.java new file mode 100644 index 0000000..23243d8 --- /dev/null +++ b/src/main/java/org/waterproofingdata/wpdauth/exception/CustomErrorAttributes.java @@ -0,0 +1,35 @@ +package org.waterproofingdata.wpdauth.exception; + +import org.springframework.boot.web.error.ErrorAttributeOptions; +import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.WebRequest; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Map; + +@Component +public class CustomErrorAttributes extends DefaultErrorAttributes { + private static final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + + + public Map getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) { + // Let Spring handle the error first, we will modify later :) + Map errorAttributes = super.getErrorAttributes(webRequest, options); + + // format & update timestamp + Object timestamp = errorAttributes.get("timestamp"); + if (timestamp == null) { + errorAttributes.put("timestamp", dateFormat.format(new Date())); + } else { + errorAttributes.put("timestamp", dateFormat.format((Date) timestamp)); + } + + // insert a new key + errorAttributes.put("version", "1.2"); + + return errorAttributes; + } +} \ No newline at end of file diff --git a/src/main/java/org/waterproofingdata/wpdauth/exception/CustomException.java b/src/main/java/org/waterproofingdata/wpdauth/exception/CustomException.java new file mode 100644 index 0000000..f98963c --- /dev/null +++ b/src/main/java/org/waterproofingdata/wpdauth/exception/CustomException.java @@ -0,0 +1,24 @@ +package org.waterproofingdata.wpdauth.exception; + +import org.springframework.http.HttpStatus; + +public class CustomException extends RuntimeException { + private static final long serialVersionUID = 1L; + + private final String message; + private final HttpStatus httpStatus; + + public CustomException(String message, HttpStatus httpStatus) { + this.message = message; + this.httpStatus = httpStatus; + } + + @Override + public String getMessage() { + return message; + } + + public HttpStatus getHttpStatus() { + return httpStatus; + } +} diff --git a/src/main/java/org/waterproofingdata/wpdauth/exception/GlobalExceptionHandlerController.java b/src/main/java/org/waterproofingdata/wpdauth/exception/GlobalExceptionHandlerController.java new file mode 100644 index 0000000..bfe78b5 --- /dev/null +++ b/src/main/java/org/waterproofingdata/wpdauth/exception/GlobalExceptionHandlerController.java @@ -0,0 +1,39 @@ +package org.waterproofingdata.wpdauth.exception; + +import java.io.IOException; + +import javax.servlet.http.HttpServletResponse; + +import org.waterproofingdata.wpdauth.exception.CustomErrorAttributes; +import org.waterproofingdata.wpdauth.exception.CustomException; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpStatus; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +@RestControllerAdvice +public class GlobalExceptionHandlerController extends ResponseEntityExceptionHandler { + @Bean + public ErrorAttributes errorAttributes() { + // Hide exception field in the return object + return new CustomErrorAttributes(); + } + + @ExceptionHandler(CustomException.class) + public void handleCustomException(HttpServletResponse res, CustomException ex) throws IOException { + res.sendError(ex.getHttpStatus().value(), ex.getMessage()); + } + + @ExceptionHandler(AccessDeniedException.class) + public void handleAccessDeniedException(HttpServletResponse res) throws IOException { + res.sendError(HttpStatus.FORBIDDEN.value(), "Access denied"); + } + + @ExceptionHandler(Exception.class) + public void handleException(HttpServletResponse res) throws IOException { + res.sendError(HttpStatus.BAD_REQUEST.value(), "Something went wrong"); + } +}