package com.szwl.exception; import com.alibaba.fastjson.JSON; import com.szwl.constant.ResponseCodesEnum; import com.szwl.model.bo.R; import com.szwl.model.bo.ResponseModel; import lombok.extern.slf4j.Slf4j; import org.springframework.validation.BindException; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import javax.servlet.http.HttpServletRequest; @Slf4j @RestControllerAdvice public class RestExceptionHandler { @ExceptionHandler(Exception.class) public ResponseModel otherExceptionHandler(Exception exception) { log.error("un caught exception: ", exception); return R.fail("内部异常[%s],请联系管理员。", exception.getMessage()); } @ExceptionHandler(MyException.class) public ResponseModel myExceptionHandler(Exception exception) { log.error("my exception: ", exception); return R.fail("业务异常[%s],请联系管理员。", exception.getMessage()); } @ExceptionHandler(ParamNotFoundException.class) public ResponseModel paramNotFoundExceptionHandler(Exception exception) { log.error("param not found exception: ", exception); return R.fail(ResponseCodesEnum.A0001,"参数异常[%s],请联系管理员。", exception.getMessage()); } /** * 处理业务异常 * * @param e * @return */ @ExceptionHandler(BizException.class) public ResponseModel handlerBizException(BizException e) { log.error("biz exception: {}", e.getMessage()); return R.fail(e.getResponseCodesEnum(), "业务异常[%s],请联系管理员。", e.getMessage()); } /** * 校验错误拦截处理 * * @param ex 错误信息集合 * @return 错误信息 * @Title: methodArgumentNotValidHandler */ @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class}) public ResponseModel methodArgumentNotValidHandler(HttpServletRequest request, Exception ex) { BindingResult result = null; if (ex instanceof BindException) { BindException bindException = (BindException) ex; result = bindException.getBindingResult(); } else if (ex instanceof MethodArgumentNotValidException) { MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) ex; result = methodArgumentNotValidException.getBindingResult(); } StringBuilder msg = new StringBuilder(); if (result != null) { if (result.getFieldErrorCount() > 0) { for (FieldError fieldErr : result.getFieldErrors()) { msg.append(fieldErr.getField() + ":" + fieldErr.getDefaultMessage()).append("|"); } } log.error("error by url:{}", request.getRequestURI().toString()); log.error("request param:{}", JSON.toJSONString(result.getTarget())); log.error("response msg:{}", msg.toString()); } return R.fail(ResponseCodesEnum.A0001, "接口参数错误:%s", msg); } /** * 认证异常 * * @param exception * @return */ @ExceptionHandler(AuthenticationException.class) public ResponseModel authenticationExceptionHandler(AuthenticationException exception) { log.error("common authentication exception: ", exception); return R.fail(exception.getResponseCodesEnum(), exception.getMessage()); } }