wanghongbo

异常处理

  1 +package com.wondertek.exception;
  2 +
  3 +public class BaseException extends RuntimeException{
  4 + private static final long serialVersionUID = 4738191554323702328L;
  5 +
  6 + protected final String code;
  7 +
  8 + public String getCode() {
  9 + return code;
  10 + }
  11 +
  12 + public BaseException(String code, String msg) {
  13 + super(msg);
  14 + this.code = code;
  15 + }
  16 +
  17 +}
@@ -2,6 +2,7 @@ package com.wondertek.exception; @@ -2,6 +2,7 @@ package com.wondertek.exception;
2 2
3 3
4 import com.wondertek.util.ResultBean; 4 import com.wondertek.util.ResultBean;
  5 +import jakarta.servlet.http.HttpServletResponse;
5 import jakarta.validation.ConstraintViolation; 6 import jakarta.validation.ConstraintViolation;
6 import jakarta.validation.ConstraintViolationException; 7 import jakarta.validation.ConstraintViolationException;
7 import lombok.extern.slf4j.Slf4j; 8 import lombok.extern.slf4j.Slf4j;
@@ -35,6 +36,24 @@ public class GlobalExceptionHandler { @@ -35,6 +36,24 @@ public class GlobalExceptionHandler {
35 private static final String REQUEST_METHOD_NOT_SUPPORT = "当前requestMethod请求类型不支持"; 36 private static final String REQUEST_METHOD_NOT_SUPPORT = "当前requestMethod请求类型不支持";
36 private static final String UNKNOWN = "未知异常"; 37 private static final String UNKNOWN = "未知异常";
37 38
  39 + private static final String ERROR_MSG = ">>>获取异常信息:{}";
  40 + /**
  41 + * 业务异常
  42 + *
  43 + * @param e
  44 + * @param response
  45 + * @return
  46 + */
  47 + @ExceptionHandler(value = ServiceException.class)
  48 + public Object handlerServiceException(ServiceException e, HttpServletResponse response) {
  49 + if (e.isNeedPrintStack()) {
  50 + log.error(ERROR_MSG, e);
  51 + } else {
  52 + log.error(ERROR_MSG, e.getMessage());
  53 + }
  54 + return ResultBean.error(HttpStatus.BAD_REQUEST.value() + "", e.getMessage());
  55 + }
  56 +
38 @ExceptionHandler(value = Exception.class) 57 @ExceptionHandler(value = Exception.class)
39 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) 58 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
40 public ResultBean unKnownException(Exception e) { 59 public ResultBean unKnownException(Exception e) {
  1 +package com.wondertek.exception;
  2 +
  3 +
  4 +import com.wondertek.enums.GlobalCodeEnum;
  5 +import com.wondertek.util.ResultBean;
  6 +import lombok.Getter;
  7 +
  8 +public class ServiceException extends BaseException {
  9 +
  10 + private static final long serialVersionUID = 1L;
  11 +
  12 + @Getter
  13 + private boolean needPrintStack = true;
  14 +
  15 + public ServiceException(String msg) {
  16 + super(GlobalCodeEnum.FAILURE.getCode(), msg);
  17 + }
  18 +
  19 + public ServiceException(String code, String msg) {
  20 + super(code, msg);
  21 + }
  22 +
  23 + public ServiceException(ResultBean resultBean) {
  24 + super(resultBean.getCode(), resultBean.getMsg());
  25 + }
  26 +
  27 + public ServiceException(String msg, boolean needPrintStack) {
  28 + super(GlobalCodeEnum.FAILURE.getCode(), msg);
  29 + this.needPrintStack = needPrintStack;
  30 + }
  31 +}
@@ -15,6 +15,7 @@ import com.wondertek.entity.BackupMaterial; @@ -15,6 +15,7 @@ import com.wondertek.entity.BackupMaterial;
15 import com.wondertek.entity.StreamTask; 15 import com.wondertek.entity.StreamTask;
16 import com.wondertek.enums.PlayTypeEnum; 16 import com.wondertek.enums.PlayTypeEnum;
17 import com.wondertek.exception.BusinessException; 17 import com.wondertek.exception.BusinessException;
  18 +import com.wondertek.exception.ServiceException;
18 import com.wondertek.mapper.BackupConfigMapper; 19 import com.wondertek.mapper.BackupConfigMapper;
19 import com.wondertek.mapper.BackupMaterialMapper; 20 import com.wondertek.mapper.BackupMaterialMapper;
20 import com.wondertek.mapper.StreamTaskMapper; 21 import com.wondertek.mapper.StreamTaskMapper;
@@ -79,7 +80,7 @@ public class BackupMaterialServiceImpl extends ServiceImpl<BackupMaterialMapper, @@ -79,7 +80,7 @@ public class BackupMaterialServiceImpl extends ServiceImpl<BackupMaterialMapper,
79 public ResultBean upload(MultipartFile backupFile, BackupUploadVo backupUploadVo) { 80 public ResultBean upload(MultipartFile backupFile, BackupUploadVo backupUploadVo) {
80 //校验 81 //校验
81 if(!"mp4".equalsIgnoreCase(backupUploadVo.getFileType())){ 82 if(!"mp4".equalsIgnoreCase(backupUploadVo.getFileType())){
82 - throw new BusinessException("仅支持上传mp4格式的垫片文件!"); 83 + throw new ServiceException("仅支持上传mp4格式的垫片文件!");
83 } 84 }
84 //对应垫片任务 85 //对应垫片任务
85 LambdaQueryWrapper<StreamTask> backupWrapper = new LambdaQueryWrapper<>(); 86 LambdaQueryWrapper<StreamTask> backupWrapper = new LambdaQueryWrapper<>();
@@ -163,7 +164,7 @@ public class BackupMaterialServiceImpl extends ServiceImpl<BackupMaterialMapper, @@ -163,7 +164,7 @@ public class BackupMaterialServiceImpl extends ServiceImpl<BackupMaterialMapper,
163 log.info("请求转码系统换垫片接口成功"); 164 log.info("请求转码系统换垫片接口成功");
164 }else { 165 }else {
165 log.info("请求转码系统换垫片接口响应失败,response:{}",response); 166 log.info("请求转码系统换垫片接口响应失败,response:{}",response);
166 - throw new BusinessException("请求转码系统换垫片接口响应失败"); 167 + throw new ServiceException("请求转码系统换垫片接口响应失败");
167 } 168 }
168 } else { 169 } else {
169 log.error("请求转码系统换垫片接口失败,url:{},body:{}", swapBackupUrl, execute.body()); 170 log.error("请求转码系统换垫片接口失败,url:{},body:{}", swapBackupUrl, execute.body());
@@ -194,7 +195,7 @@ public class BackupMaterialServiceImpl extends ServiceImpl<BackupMaterialMapper, @@ -194,7 +195,7 @@ public class BackupMaterialServiceImpl extends ServiceImpl<BackupMaterialMapper,
194 log.info("请求转码系统切换直播源接口成功"); 195 log.info("请求转码系统切换直播源接口成功");
195 }else { 196 }else {
196 log.info("请求转码系统切换直播源接口响应失败,response:{}",response); 197 log.info("请求转码系统切换直播源接口响应失败,response:{}",response);
197 - throw new BusinessException("请求转码系统切换直播源接口响应失败"); 198 + throw new ServiceException("请求转码系统切换直播源接口响应失败");
198 } 199 }
199 } else { 200 } else {
200 log.error("请求转码系统切换直播源接口失败,url:{},body:{}", switchStreamUrl, execute.body()); 201 log.error("请求转码系统切换直播源接口失败,url:{},body:{}", switchStreamUrl, execute.body());