|
@@ -1,7 +1,8 @@
|
|
|
package com.hrsk.cloud.eg.infrastructure.exception;
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.hrsk.cloud.eg.domain.common.constant.ResultCode;
|
|
|
-import com.hrsk.cloud.eg.vo.response.DefaultResponseVo;
|
|
|
+import com.hrsk.pangu.dto.SingleResponse;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.boot.context.properties.bind.BindException;
|
|
|
import org.springframework.http.HttpStatus;
|
|
@@ -28,57 +29,61 @@ public class ExceptionAdvice {
|
|
|
* @return
|
|
|
*/
|
|
|
@ExceptionHandler(Exception.class)
|
|
|
- public ResponseEntity<DefaultResponseVo> exception(Exception exception) {
|
|
|
- DefaultResponseVo result = new DefaultResponseVo<>();
|
|
|
- ResponseEntity<DefaultResponseVo> springResponse = handleSpringException(exception);
|
|
|
+ public ResponseEntity<SingleResponse> exception(Exception exception) {
|
|
|
+ SingleResponse result = new SingleResponse<>();
|
|
|
+ ResponseEntity<SingleResponse> springResponse = handleSpringException(exception);
|
|
|
if (springResponse != null) {
|
|
|
return springResponse;
|
|
|
}
|
|
|
|
|
|
if (exception instanceof IllegalArgumentException) {
|
|
|
- result.setCode(ResultCode.GLOBAL_REQUEST_FAIL.getCode());
|
|
|
- result.setMsg(exception.getMessage());
|
|
|
- return ResponseEntity.ok(result);
|
|
|
+ return ResponseEntity.ok(fail(ResultCode.GLOBAL_REQUEST_FAIL.getCode(), exception.getMessage()));
|
|
|
}
|
|
|
if (exception instanceof BindException) {
|
|
|
- result.setCode(ResultCode.GLOBAL_REQUEST_FAIL.getCode());
|
|
|
- result.setMsg(exception.getMessage());
|
|
|
- return ResponseEntity.ok(result);
|
|
|
+ return ResponseEntity.ok(fail(ResultCode.GLOBAL_REQUEST_FAIL.getCode(), exception.getMessage()));
|
|
|
}
|
|
|
if (exception instanceof ConstraintViolationException) {
|
|
|
- result.setCode(ResultCode.GLOBAL_REQUEST_FAIL.getCode());
|
|
|
- result.setMsg(exception.getMessage());
|
|
|
- return ResponseEntity.ok(result);
|
|
|
+ return ResponseEntity.ok(fail(ResultCode.GLOBAL_REQUEST_FAIL.getCode(), exception.getMessage()));
|
|
|
}
|
|
|
if (exception instanceof BindException) {
|
|
|
- BindException bizException = (BindException) exception;
|
|
|
- result.setCode(ResultCode.GLOBAL_REQUEST_FAIL.getCode());
|
|
|
- result.setMsg(ResultCode.GLOBAL_REQUEST_FAIL.getMsg());
|
|
|
- result.setData(null);
|
|
|
- return ResponseEntity.ok(result);
|
|
|
+// BindException bizException = (BindException) exception;
|
|
|
+ return ResponseEntity.ok(fail(ResultCode.GLOBAL_REQUEST_FAIL.getCode(), ResultCode.GLOBAL_REQUEST_FAIL.getMsg()));
|
|
|
}
|
|
|
if (exception instanceof MethodArgumentNotValidException) {
|
|
|
MethodArgumentNotValidException bindException = (MethodArgumentNotValidException) exception;
|
|
|
- result.setCode(400);
|
|
|
- result.setMsg(bindException.getBindingResult().getFieldError().getDefaultMessage());
|
|
|
- result.setData(null);
|
|
|
- return ResponseEntity.ok(result);
|
|
|
+ return ResponseEntity.ok(fail(400, bindException.getBindingResult().getFieldError().getDefaultMessage()));
|
|
|
}
|
|
|
log.error("全局未知异常,异常信息:{}", exception.getMessage(), exception);
|
|
|
- result = DefaultResponseVo.fail(ResultCode.GLOBAL_REQUEST_FAIL.getMsg());
|
|
|
- return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
|
|
|
+ return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(fail(500, exception.getMessage()));
|
|
|
}
|
|
|
|
|
|
- private ResponseEntity<DefaultResponseVo> handleSpringException(Exception exception) {
|
|
|
+ private ResponseEntity<SingleResponse> handleSpringException(Exception exception) {
|
|
|
if (exception instanceof ServletRequestBindingException) {
|
|
|
- return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(DefaultResponseVo.fail(exception.getMessage()));
|
|
|
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(fail(404,exception.getMessage()));
|
|
|
}
|
|
|
if (exception instanceof HttpRequestMethodNotSupportedException) {
|
|
|
- return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(DefaultResponseVo.fail(exception.getMessage()));
|
|
|
+ return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(fail(405,exception.getMessage()));
|
|
|
}
|
|
|
if (exception instanceof HttpMessageNotReadableException) {
|
|
|
- return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(DefaultResponseVo.fail(exception.getMessage()));
|
|
|
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(fail(406,exception.getMessage()));
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 简单的构建错误信息
|
|
|
+ * @param:
|
|
|
+ * @return:
|
|
|
+ * @author zhangyy
|
|
|
+ * @date: 2024/11/11 10:50
|
|
|
+ */
|
|
|
+ private SingleResponse fail(Integer code,String msg){
|
|
|
+ SingleResponse result = new SingleResponse<>();
|
|
|
+ result.setSuccess(false);
|
|
|
+ JSONObject data = new JSONObject();
|
|
|
+ data.put("code",code);
|
|
|
+ data.put("msg",msg);
|
|
|
+ result.setData(data);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|