Class GeneralAdvice
java.lang.Object
com.beassolution.rule.advice.GeneralAdvice
- All Implemented Interfaces:
org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice<Collection<?>>
@ControllerAdvice
public class GeneralAdvice
extends Object
implements org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice<Collection<?>>
Global exception handler and response body advice for the Beas Rule Engine.
This class provides centralized exception handling and response processing for the entire application. It converts various exceptions into standardized response formats and adds metadata to collection responses.
Key features include:
- Global exception handling
- Standardized error responses
- Collection response metadata
- Exception type conversion
- Since:
- 1.0
- Version:
- 1.0
- Author:
- Beas Solution Team
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCollection
<?> beforeBodyWrite
(Collection<?> body, org.springframework.core.MethodParameter returnType, org.springframework.http.MediaType selectedContentType, Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> selectedConverterType, org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response) Adds metadata to collection responses.static OperationException
convertToOperationException
(Throwable throwable, org.springframework.http.HttpStatus status) Converts a Throwable to an OperationException with appropriate status.static BaseResponseModel
<OperationException> generateOperationMessage
(OperationException operationException) Generates a standardized error response model.handleError
(Error error) Handles Error instances.handleException
(Exception exception) Handles general exceptions.handleHttpMessageConversionException
(org.springframework.http.converter.HttpMessageConversionException exception) Handles HTTP message conversion exceptions.handleHttpRequestMethodNotSupportedException
(org.springframework.web.HttpRequestMethodNotSupportedException exception) Handles HTTP request method not supported exceptions.handleMethodArgumentNotValidException
(org.springframework.web.bind.MethodArgumentNotValidException exception) Handles method argument not valid exceptions.handleMongoDataIntegrityViolation
(org.springframework.data.mongodb.core.MongoDataIntegrityViolationException exception) Handles MongoDB data integrity violation exceptions.handleNoHandlerFoundException
(org.springframework.web.servlet.NoHandlerFoundException exception) Handles no handler found exceptions.handleOperationException
(OperationException exception) Handles OperationException instances.handleThrowable
(Throwable throwable) Handles general throwable exceptions.boolean
supports
(org.springframework.core.MethodParameter returnType, Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> converterType) Determines if the advice should be applied to the response.
-
Constructor Details
-
GeneralAdvice
public GeneralAdvice()
-
-
Method Details
-
convertToOperationException
public static OperationException convertToOperationException(Throwable throwable, org.springframework.http.HttpStatus status) Converts a Throwable to an OperationException with appropriate status.- Parameters:
throwable
- The throwable to convertstatus
- The HTTP status code- Returns:
- The converted OperationException
-
generateOperationMessage
public static BaseResponseModel<OperationException> generateOperationMessage(OperationException operationException) Generates a standardized error response model.- Parameters:
operationException
- The OperationException to convert- Returns:
- The standardized response model
-
supports
public boolean supports(org.springframework.core.MethodParameter returnType, Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> converterType) Determines if the advice should be applied to the response.- Specified by:
supports
in interfaceorg.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice<Collection<?>>
- Parameters:
returnType
- The method return typeconverterType
- The message converter type- Returns:
- true if the response is a Collection
-
beforeBodyWrite
public Collection<?> beforeBodyWrite(Collection<?> body, org.springframework.core.MethodParameter returnType, org.springframework.http.MediaType selectedContentType, Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> selectedConverterType, org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response) Adds metadata to collection responses.This method adds an X-Total-Count header to collection responses to provide information about the total number of items.
- Specified by:
beforeBodyWrite
in interfaceorg.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice<Collection<?>>
- Parameters:
body
- The response bodyreturnType
- The method return typeselectedContentType
- The selected content typeselectedConverterType
- The selected converter typerequest
- The HTTP requestresponse
- The HTTP response- Returns:
- The modified response body
-
handleMongoDataIntegrityViolation
@ResponseBody @ResponseStatus(PRECONDITION_FAILED) @ExceptionHandler(org.springframework.data.mongodb.core.MongoDataIntegrityViolationException.class) public BaseResponseModel<OperationException> handleMongoDataIntegrityViolation(org.springframework.data.mongodb.core.MongoDataIntegrityViolationException exception) Handles MongoDB data integrity violation exceptions.- Parameters:
exception
- The MongoDB data integrity violation exception- Returns:
- Standardized error response
-
handleOperationException
@ResponseBody @ResponseStatus(BAD_REQUEST) @ExceptionHandler(OperationException.class) public BaseResponseModel<OperationException> handleOperationException(OperationException exception) Handles OperationException instances.- Parameters:
exception
- The OperationException- Returns:
- Standardized error response
-
handleHttpMessageConversionException
@ResponseBody @ResponseStatus(BAD_REQUEST) @ExceptionHandler(org.springframework.http.converter.HttpMessageConversionException.class) public BaseResponseModel<OperationException> handleHttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException exception) Handles HTTP message conversion exceptions.- Parameters:
exception
- The HTTP message conversion exception- Returns:
- Standardized error response
-
handleHttpRequestMethodNotSupportedException
@ResponseBody @ResponseStatus(METHOD_NOT_ALLOWED) @ExceptionHandler(org.springframework.web.HttpRequestMethodNotSupportedException.class) public BaseResponseModel<OperationException> handleHttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException exception) Handles HTTP request method not supported exceptions.- Parameters:
exception
- The HTTP request method not supported exception- Returns:
- Standardized error response
-
handleMethodArgumentNotValidException
@ResponseBody @ResponseStatus(BAD_REQUEST) @ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class) public BaseResponseModel<OperationException> handleMethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException exception) Handles method argument not valid exceptions.- Parameters:
exception
- The method argument not valid exception- Returns:
- Standardized error response
-
handleNoHandlerFoundException
@ResponseBody @ResponseStatus(NOT_FOUND) @ExceptionHandler(org.springframework.web.servlet.NoHandlerFoundException.class) public BaseResponseModel<OperationException> handleNoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException exception) Handles no handler found exceptions.- Parameters:
exception
- The no handler found exception- Returns:
- Standardized error response
-
handleThrowable
@ResponseBody @ResponseStatus(INTERNAL_SERVER_ERROR) @ExceptionHandler(Throwable.class) public BaseResponseModel<OperationException> handleThrowable(Throwable throwable) Handles general throwable exceptions.- Parameters:
throwable
- The throwable exception- Returns:
- Standardized error response
-
handleException
@ResponseBody @ResponseStatus(INTERNAL_SERVER_ERROR) @ExceptionHandler(Exception.class) public BaseResponseModel<OperationException> handleException(Exception exception) Handles general exceptions.- Parameters:
exception
- The exception- Returns:
- Standardized error response
-
handleError
@ResponseBody @ResponseStatus(INTERNAL_SERVER_ERROR) @ExceptionHandler(Error.class) public BaseResponseModel<OperationException> handleError(Error error) Handles Error instances.- Parameters:
error
- The error- Returns:
- Standardized error response
-