wanghongbo

异常处理

package com.wondertek.exception;
public class BaseException extends RuntimeException{
private static final long serialVersionUID = 4738191554323702328L;
protected final String code;
public String getCode() {
return code;
}
public BaseException(String code, String msg) {
super(msg);
this.code = code;
}
}
... ...
... ... @@ -2,6 +2,7 @@ package com.wondertek.exception;
import com.wondertek.util.ResultBean;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
... ... @@ -35,6 +36,24 @@ public class GlobalExceptionHandler {
private static final String REQUEST_METHOD_NOT_SUPPORT = "当前requestMethod请求类型不支持";
private static final String UNKNOWN = "未知异常";
private static final String ERROR_MSG = ">>>获取异常信息:{}";
/**
* 业务异常
*
* @param e
* @param response
* @return
*/
@ExceptionHandler(value = ServiceException.class)
public Object handlerServiceException(ServiceException e, HttpServletResponse response) {
if (e.isNeedPrintStack()) {
log.error(ERROR_MSG, e);
} else {
log.error(ERROR_MSG, e.getMessage());
}
return ResultBean.error(HttpStatus.BAD_REQUEST.value() + "", e.getMessage());
}
@ExceptionHandler(value = Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResultBean unKnownException(Exception e) {
... ...
package com.wondertek.exception;
import com.wondertek.enums.GlobalCodeEnum;
import com.wondertek.util.ResultBean;
import lombok.Getter;
public class ServiceException extends BaseException {
private static final long serialVersionUID = 1L;
@Getter
private boolean needPrintStack = true;
public ServiceException(String msg) {
super(GlobalCodeEnum.FAILURE.getCode(), msg);
}
public ServiceException(String code, String msg) {
super(code, msg);
}
public ServiceException(ResultBean resultBean) {
super(resultBean.getCode(), resultBean.getMsg());
}
public ServiceException(String msg, boolean needPrintStack) {
super(GlobalCodeEnum.FAILURE.getCode(), msg);
this.needPrintStack = needPrintStack;
}
}
... ...
... ... @@ -15,6 +15,7 @@ import com.wondertek.entity.BackupMaterial;
import com.wondertek.entity.StreamTask;
import com.wondertek.enums.PlayTypeEnum;
import com.wondertek.exception.BusinessException;
import com.wondertek.exception.ServiceException;
import com.wondertek.mapper.BackupConfigMapper;
import com.wondertek.mapper.BackupMaterialMapper;
import com.wondertek.mapper.StreamTaskMapper;
... ... @@ -79,7 +80,7 @@ public class BackupMaterialServiceImpl extends ServiceImpl<BackupMaterialMapper,
public ResultBean upload(MultipartFile backupFile, BackupUploadVo backupUploadVo) {
//校验
if(!"mp4".equalsIgnoreCase(backupUploadVo.getFileType())){
throw new BusinessException("仅支持上传mp4格式的垫片文件!");
throw new ServiceException("仅支持上传mp4格式的垫片文件!");
}
//对应垫片任务
LambdaQueryWrapper<StreamTask> backupWrapper = new LambdaQueryWrapper<>();
... ... @@ -163,7 +164,7 @@ public class BackupMaterialServiceImpl extends ServiceImpl<BackupMaterialMapper,
log.info("请求转码系统换垫片接口成功");
}else {
log.info("请求转码系统换垫片接口响应失败,response:{}",response);
throw new BusinessException("请求转码系统换垫片接口响应失败");
throw new ServiceException("请求转码系统换垫片接口响应失败");
}
} else {
log.error("请求转码系统换垫片接口失败,url:{},body:{}", swapBackupUrl, execute.body());
... ... @@ -194,7 +195,7 @@ public class BackupMaterialServiceImpl extends ServiceImpl<BackupMaterialMapper,
log.info("请求转码系统切换直播源接口成功");
}else {
log.info("请求转码系统切换直播源接口响应失败,response:{}",response);
throw new BusinessException("请求转码系统切换直播源接口响应失败");
throw new ServiceException("请求转码系统切换直播源接口响应失败");
}
} else {
log.error("请求转码系统切换直播源接口失败,url:{},body:{}", switchStreamUrl, execute.body());
... ...