12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package net.lab1024.smartadmin.handler;
- import java.util.List;
- import java.util.stream.Collectors;
- import org.springframework.beans.TypeMismatchException;
- import org.springframework.http.converter.HttpMessageNotReadableException;
- import org.springframework.validation.FieldError;
- import org.springframework.web.HttpRequestMethodNotSupportedException;
- import org.springframework.web.bind.MethodArgumentNotValidException;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseBody;
- import lombok.extern.slf4j.Slf4j;
- import net.lab1024.smartadmin.common.constant.ResponseCodeConst;
- import net.lab1024.smartadmin.common.domain.ResponseDTO;
- import net.lab1024.smartadmin.common.exception.SmartBusinessException;
- import net.lab1024.smartadmin.common.exception.SmartResponseCodeException;
- /**
- * [ 全局异常拦截 ]
- *
- * @author yandanyang
- * @version 1.0
- * @company 1024lab.net
- * @copyright (c) 2019 1024lab.netInc. All rights reserved.
- * @date
- * @since JDK1.8
- */
- @Slf4j
- @ControllerAdvice
- public class SmartGlobalExceptionHandler {
- /**
- * 添加全局异常处理流程
- *
- * @param e
- * @return
- * @throws Exception
- */
- @ResponseBody
- @ExceptionHandler(Exception.class)
- public ResponseDTO exceptionHandler(Exception e) {
- log.error("error:", e);
- // http 请求方式错误
- if (e instanceof HttpRequestMethodNotSupportedException) {
- return ResponseDTO.wrap(ResponseCodeConst.REQUEST_METHOD_ERROR);
- }
- // 参数类型错误
- if (e instanceof TypeMismatchException) {
- return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM);
- }
- // json 格式错误
- if (e instanceof HttpMessageNotReadableException) {
- return ResponseDTO.wrap(ResponseCodeConst.JSON_FORMAT_ERROR);
- }
- // 参数校验未通过
- if (e instanceof MethodArgumentNotValidException) {
- List<FieldError> fieldErrors = ((MethodArgumentNotValidException) e).getBindingResult().getFieldErrors();
- List<String> msgList = fieldErrors.stream().map(FieldError :: getDefaultMessage).collect(Collectors.toList());
- return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM, String.join(",", msgList));
- }
- if (e instanceof SmartBusinessException) {
- return ResponseDTO.wrap(ResponseCodeConst.SYSTEM_ERROR);
- }
- if (e instanceof SmartResponseCodeException) {
- return ResponseDTO.wrap(new ResponseCodeConst(((SmartResponseCodeException) e).getCode(), e.getMessage(),"0"));
- }
- return ResponseDTO.wrap(ResponseCodeConst.SYSTEM_ERROR);
- }
- }
|