|
|
@ -8,6 +8,8 @@ import com.jiagutech.ams.exception.BizCode; |
|
|
|
import com.jiagutech.ams.exception.BusinessException; |
|
|
|
import com.jiagutech.ams.model.common.R; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.http.HttpStatus; |
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
import org.springframework.validation.BindException; |
|
|
|
import org.springframework.validation.FieldError; |
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException; |
|
|
@ -24,51 +26,51 @@ import java.util.Map; |
|
|
|
public class GlobalExceptionHandler { |
|
|
|
|
|
|
|
@ExceptionHandler(BindException.class) |
|
|
|
public R handleBindExceptions(BindException ex) { |
|
|
|
public ResponseEntity<R> handleBindExceptions(BindException ex) { |
|
|
|
log.error("参数绑定失败", ex); |
|
|
|
Map<String, String> errorMap = new HashMap<>(); |
|
|
|
for (FieldError fieldError : ex.getFieldErrors()) { |
|
|
|
errorMap.put(fieldError.getField(), fieldError.getDefaultMessage()); |
|
|
|
} |
|
|
|
return R.fail(BizCode.General_ParameterInvalid.getCode(), JSON.toJSONString(errorMap)); |
|
|
|
return new ResponseEntity<>(R.fail(BizCode.General_ParameterInvalid.getCode(), JSON.toJSONString(errorMap)), HttpStatus.BAD_REQUEST); |
|
|
|
} |
|
|
|
|
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class) |
|
|
|
public R handleValidationExceptions(MethodArgumentNotValidException ex) { |
|
|
|
public ResponseEntity<R> handleValidationExceptions(MethodArgumentNotValidException ex) { |
|
|
|
log.error("参数校验失败", ex); |
|
|
|
Map<String, String> errorMap = new HashMap<>(); |
|
|
|
for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) { |
|
|
|
errorMap.put(fieldError.getField(), fieldError.getDefaultMessage()); |
|
|
|
} |
|
|
|
return R.fail(BizCode.General_ParameterInvalid.getCode(), JSON.toJSONString(errorMap)); |
|
|
|
return new ResponseEntity<>(R.fail(BizCode.General_ParameterInvalid.getCode(), JSON.toJSONString(errorMap)), HttpStatus.BAD_REQUEST); |
|
|
|
} |
|
|
|
|
|
|
|
@ExceptionHandler(BusinessException.class) |
|
|
|
public R handleBusinessException(BusinessException ex) { |
|
|
|
return R.fail(ex.getCode(), ex.getMessage()); |
|
|
|
public ResponseEntity<R> handleBusinessException(BusinessException ex) { |
|
|
|
return new ResponseEntity<>(R.fail(ex.getCode(), ex.getMessage()), HttpStatus.BAD_REQUEST); |
|
|
|
} |
|
|
|
|
|
|
|
@ExceptionHandler(MethodArgumentTypeMismatchException.class) |
|
|
|
public R handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) { |
|
|
|
public ResponseEntity<R> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) { |
|
|
|
log.error("参数类型错误", ex); |
|
|
|
return R.fail(new BusinessException("参数类型错误")); |
|
|
|
return new ResponseEntity<>(R.fail(new BusinessException("参数类型错误")), HttpStatus.BAD_REQUEST); |
|
|
|
} |
|
|
|
|
|
|
|
@ExceptionHandler(NotLoginException.class) |
|
|
|
public R handleLoginException(Exception ex) { |
|
|
|
public ResponseEntity<R> handleLoginException(Exception ex) { |
|
|
|
log.error("系统异常", ex); |
|
|
|
return R.fail(BizCode.USER_NOT_LOGIN.getCode(), BizCode.USER_NOT_LOGIN.getMsg()); |
|
|
|
return new ResponseEntity<>(R.fail(BizCode.USER_NOT_LOGIN.getCode(), BizCode.USER_NOT_LOGIN.getMsg()), HttpStatus.UNAUTHORIZED); |
|
|
|
} |
|
|
|
|
|
|
|
@ExceptionHandler(NotRoleException.class) |
|
|
|
public R handlePermissionException(Exception ex) { |
|
|
|
public ResponseEntity<R> handlePermissionException(Exception ex) { |
|
|
|
log.error("鉴权失败", ex); |
|
|
|
return R.fail(BizCode.PERMISSION_NOT_FOUND.getCode(), BizCode.PERMISSION_NOT_FOUND.getMsg()); |
|
|
|
return new ResponseEntity<>(R.fail(BizCode.PERMISSION_NOT_FOUND.getCode(), BizCode.PERMISSION_NOT_FOUND.getMsg()), HttpStatus.FORBIDDEN); |
|
|
|
} |
|
|
|
|
|
|
|
@ExceptionHandler(SQLIntegrityConstraintViolationException.class) |
|
|
|
public R handleDuplicateException(Exception ex){ |
|
|
|
public ResponseEntity<R> handleDuplicateException(Exception ex) { |
|
|
|
log.error("数据重复", ex); |
|
|
|
return R.fail(BizCode.USER_PHONE_EXIST.getCode(), BizCode.USER_PHONE_EXIST.getMsg()); |
|
|
|
return new ResponseEntity<>(R.fail(BizCode.USER_PHONE_EXIST.getCode(), BizCode.USER_PHONE_EXIST.getMsg()), HttpStatus.BAD_REQUEST); |
|
|
|
} |
|
|
|
} |
|
|
|