日志处理
This commit is contained in:
@@ -4,6 +4,7 @@ package com.cool.store.handler;
|
||||
import com.cool.store.exception.ApiException;
|
||||
import com.cool.store.exception.ServiceException;
|
||||
import com.cool.store.response.error.ErrorResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
@@ -15,23 +16,27 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
* @Description: 自定义异常处理
|
||||
* @date 2023-06-13 19:42
|
||||
*/
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class CustomExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = ServiceException.class)
|
||||
public ResponseEntity<ErrorResponse> handleCustomException(ServiceException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
ErrorResponse errorResponse = new ErrorResponse(e.getErrorCode(), e.getMessage());
|
||||
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = ApiException.class)
|
||||
public ResponseEntity<ErrorResponse> handleCustomException(ApiException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
ErrorResponse errorResponse = new ErrorResponse(e.getErrorCode(), e.getMessage());
|
||||
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
public ResponseEntity<ErrorResponse> handleException(Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
|
||||
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user