Skip to content

Commit

Permalink
perf($starter): define the order of controller advice
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Jul 12, 2021
1 parent 8db3874 commit 34706c9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.authentication.BadCredentialsException;
Expand Down Expand Up @@ -36,6 +38,7 @@
* @date 2019-03-02 17:39
**/
@Slf4j
@Order(0)
@RestControllerAdvice
public class CommonExceptionControllerAdvice {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jmsoftware.maf.springcloudstarter.aspect;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.jmsoftware.maf.common.bean.ResponseBodyBean;
Expand All @@ -10,7 +11,10 @@
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand All @@ -28,8 +32,16 @@
**/
@Slf4j
@RestControllerAdvice
@Order(DatabaseExceptionControllerAdvice.ORDER)
@ConditionalOnClass({MyBatisSystemException.class, MybatisPlusException.class, PersistenceException.class})
public class DatabaseExceptionControllerAdvice {
/**
* Before CommonExceptionControllerAdvice
*
* @see CommonExceptionControllerAdvice
*/
public static final int ORDER = -1;

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(MyBatisSystemException.class)
public ResponseBodyBean<?> handleMyBatisSystemException(HttpServletRequest request,
Expand Down Expand Up @@ -61,6 +73,21 @@ public ResponseBodyBean<?> handlePersistenceException(HttpServletRequest request
removeLineSeparator(exception.getMessage())));
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(BadSqlGrammarException.class)
public ResponseBodyBean<?> handleBadSqlGrammarException(HttpServletRequest request,
BadSqlGrammarException exception) {
requestLog(request);
log.error("BadSqlGrammarException message: {}", exception.getMessage());
var message = exception.getMessage();
if (ObjectUtil.isNotNull(exception.getCause()) && StrUtil.isNotBlank(exception.getCause().getMessage())) {
message = exception.getCause().getMessage();
}
return ResponseBodyBean.ofStatus(HttpStatus.INTERNAL_SERVER_ERROR,
String.format("PersistenceException message: %s",
removeLineSeparator(message)));
}

private void requestLog(HttpServletRequest request) {
log.error("Exception occurred when [{}] requested access. Request URL: [{}] {}",
RequestUtil.getRequestIpAndPort(request), request.getMethod(), request.getRequestURL());
Expand Down

0 comments on commit 34706c9

Please sign in to comment.