Skip to content

Commit

Permalink
feat($Starter): intercept SQL exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Jun 7, 2021
1 parent 624e5ab commit 9f5a16f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 5/28/2021 4:27 PM
*/
@Documented
@Target(value = {ElementType.FIELD})
@Retention(value = RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeElement {
TreeElementType type() default TreeElementType.ID;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.jmsoftware.maf.springcloudstarter.aspect;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import com.jmsoftware.maf.common.exception.BaseException;
import com.jmsoftware.maf.springcloudstarter.util.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.ibatis.exceptions.PersistenceException;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
Expand Down Expand Up @@ -70,8 +73,7 @@ public ResponseBodyBean<?> handleException(HttpServletRequest request,
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseBodyBean<?> handleException(HttpServletRequest request, MethodArgumentNotValidException exception) {
requestLog(request);
log.error("Exception occurred when validation on an argument annotated with fails. Exception message: {}",
exception.getMessage());
log.error("MethodArgumentNotValidException message: {}", exception.getMessage());
return ResponseBodyBean.ofStatus(HttpStatus.BAD_REQUEST.value(), getFieldErrorMessageFromException(exception),
null);
}
Expand Down Expand Up @@ -167,6 +169,37 @@ public ResponseBodyBean<?> handleException(HttpServletRequest request, HttpServl
null);
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(MyBatisSystemException.class)
public ResponseBodyBean<?> handleMyBatisSystemException(HttpServletRequest request,
MyBatisSystemException exception) {
requestLog(request);
log.error("MyBatisSystemException message: {}", exception.getMessage());
return ResponseBodyBean.ofStatus(HttpStatus.INTERNAL_SERVER_ERROR,
String.format("MyBatisSystemException message: %s",
removeLineSeparator(exception.getMessage())));
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(MybatisPlusException.class)
public ResponseBodyBean<?> handleMybatisPlusException(HttpServletRequest request, MybatisPlusException exception) {
requestLog(request);
log.error("MybatisPlusException message: {}", exception.getMessage());
return ResponseBodyBean.ofStatus(HttpStatus.INTERNAL_SERVER_ERROR,
String.format("MybatisPlusException message: %s",
removeLineSeparator(exception.getMessage())));
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(PersistenceException.class)
public ResponseBodyBean<?> handlePersistenceException(HttpServletRequest request, PersistenceException exception) {
requestLog(request);
log.error("PersistenceException message: {}", exception.getMessage());
return ResponseBodyBean.ofStatus(HttpStatus.INTERNAL_SERVER_ERROR,
String.format("PersistenceException message: %s",
removeLineSeparator(exception.getMessage())));
}

@ExceptionHandler(Throwable.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseBodyBean<?> handleError(Throwable ex) {
Expand Down

0 comments on commit 9f5a16f

Please sign in to comment.