ddangelorb
4 years ago
3 changed files with 98 additions and 0 deletions
-
35src/main/java/org/waterproofingdata/wpdauth/exception/CustomErrorAttributes.java
-
24src/main/java/org/waterproofingdata/wpdauth/exception/CustomException.java
-
39src/main/java/org/waterproofingdata/wpdauth/exception/GlobalExceptionHandlerController.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<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) { |
|||
// Let Spring handle the error first, we will modify later :) |
|||
Map<String, Object> 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; |
|||
} |
|||
} |
@ -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; |
|||
} |
|||
} |
@ -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"); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue