【file】整理file的异常

pull/3/head
fengshuonan 2021-02-08 10:21:44 +08:00
parent 6d84980b6f
commit 7499b60000
9 changed files with 38 additions and 105 deletions

View File

@ -1,5 +1,6 @@
package cn.stylefeng.roses.kernel.file.exception;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.file.constants.FileConstants;
import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
@ -12,16 +13,12 @@ import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
*/
public class FileException extends ServiceException {
public FileException(String errorCode, String userTip) {
super(FileConstants.FILE_MODULE_NAME, errorCode, userTip);
}
public FileException(AbstractExceptionEnum exception) {
super(FileConstants.FILE_MODULE_NAME, exception);
}
public FileException(AbstractExceptionEnum exception, String userTip) {
super(FileConstants.FILE_MODULE_NAME, exception.getErrorCode(), userTip);
public FileException(AbstractExceptionEnum exception, Object... params) {
super(FileConstants.FILE_MODULE_NAME, exception.getErrorCode(), StrUtil.format(exception.getUserTip(), params));
}
}

View File

@ -3,7 +3,6 @@ package cn.stylefeng.roses.kernel.file.util;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.stylefeng.roses.kernel.file.exception.FileException;
import cn.stylefeng.roses.kernel.file.exception.enums.FileExceptionEnum;
@ -43,8 +42,7 @@ public class DownloadUtil {
response.setContentType("application/octet-stream;charset=UTF-8");
IoUtil.write(response.getOutputStream(), true, fileBytes);
} catch (IOException e) {
String userTip = StrUtil.format(FileExceptionEnum.DOWNLOAD_FILE_ERROR.getUserTip(), e.getMessage());
throw new FileException(FileExceptionEnum.DOWNLOAD_FILE_ERROR, userTip);
throw new FileException(FileExceptionEnum.DOWNLOAD_FILE_ERROR, e.getMessage());
}
}
@ -66,8 +64,7 @@ public class DownloadUtil {
try {
fileName = URLEncoder.encode(file.getName(), CharsetUtil.UTF_8);
} catch (UnsupportedEncodingException e) {
String userTip = StrUtil.format(FileExceptionEnum.DOWNLOAD_FILE_ERROR.getUserTip(), e.getMessage());
throw new FileException(FileExceptionEnum.DOWNLOAD_FILE_ERROR, userTip);
throw new FileException(FileExceptionEnum.DOWNLOAD_FILE_ERROR, e.getMessage());
}
//下载文件
@ -91,8 +88,7 @@ public class DownloadUtil {
// 输出字节流
IoUtil.write(outputStream, true, fileBytes);
} catch (IOException e) {
String userTip = StrUtil.format(FileExceptionEnum.WRITE_BYTES_ERROR.getUserTip(), e.getMessage());
throw new FileException(FileExceptionEnum.WRITE_BYTES_ERROR, userTip);
throw new FileException(FileExceptionEnum.WRITE_BYTES_ERROR, e.getMessage());
}
}

View File

@ -37,8 +37,6 @@ import static cn.stylefeng.roses.kernel.file.constants.FileConstants.*;
* 1.ID<br>
*
* 2.UICODE<br>
* <p>
* 线banner~
*
* @author majianguo
* @date 2020/12/27 13:39
@ -62,7 +60,6 @@ public class SysFileInfoController {
return new SuccessResponseData(fileUploadInfoResult);
}
/**
* tinymce
*

View File

@ -61,8 +61,7 @@ public class FileInfoFactory {
bytes = file.getBytes();
fileOperatorApi.storageFile(DEFAULT_BUCKET_NAME, finalFileName, bytes);
} catch (IOException e) {
String userTip = StrUtil.format(FileExceptionEnum.ERROR_FILE.getUserTip(), e.getMessage());
throw new FileException(FileExceptionEnum.ERROR_FILE, userTip);
throw new FileException(FileExceptionEnum.ERROR_FILE, e.getMessage());
}
// 计算文件大小kb

View File

@ -205,8 +205,7 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
String fileOriginName = sysFileInfoResponse.getFileOriginName();
// 判断公有文件下载时是否包含私有文件
if (secretFlag.equals(YesOrNotEnum.N.getCode()) && !secretFlag.equals(sysFileInfoResponse.getSecretFlag())) {
String userTip = StrUtil.format(FileExceptionEnum.SECRET_FLAG_INFO_ERROR.getUserTip(), fileOriginName);
throw new FileException(FileExceptionEnum.SECRET_FLAG_INFO_ERROR, userTip);
throw new FileException(FileExceptionEnum.SECRET_FLAG_INFO_ERROR, fileOriginName);
}
byte[] fileBytes = fileOperatorApi.getFileBytes(DEFAULT_BUCKET_NAME, sysFileInfoResponse.getFileObjectName());
@ -299,7 +298,7 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
if (ObjectUtil.isEmpty(fileInfo)) {
String userTip = FileExceptionEnum.FILE_NOT_FOUND.getUserTip();
String errorMessage = StrUtil.format(userTip, "文件:" + fileInfo.getFileId() + "未找到!");
throw new FileException(FILE_NOT_FOUND.getErrorCode(), errorMessage);
throw new FileException(FILE_NOT_FOUND, errorMessage);
}
// 把之前的文件刷回
@ -405,8 +404,7 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
// 输出字节流
IoUtil.write(outputStream, true, fileBytes);
} catch (IOException e) {
String userTip = StrUtil.format(FileExceptionEnum.WRITE_BYTES_ERROR.getUserTip(), e.getMessage());
throw new FileException(FileExceptionEnum.WRITE_BYTES_ERROR, userTip);
throw new FileException(FileExceptionEnum.WRITE_BYTES_ERROR, e.getMessage());
}
} else {
// 不支持别的文件预览
@ -423,8 +421,7 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
private SysFileInfo querySysFileInfo(SysFileInfoRequest sysFileInfoRequest) {
SysFileInfo sysFileInfo = this.getById(sysFileInfoRequest.getFileId());
if (ObjectUtil.isEmpty(sysFileInfo) || sysFileInfo.getDelFlag().equals(YesOrNotEnum.Y.getCode())) {
String userTip = StrUtil.format(FileExceptionEnum.NOT_EXISTED.getUserTip(), sysFileInfoRequest.getFileId());
throw new FileException(FileExceptionEnum.NOT_EXISTED, userTip);
throw new FileException(FileExceptionEnum.NOT_EXISTED, sysFileInfoRequest.getFileId());
}
return sysFileInfo;
}

View File

@ -1,7 +1,6 @@
package cn.stylefeng.roses.kernel.file.aliyun;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.file.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.enums.BucketAuthEnum;
import cn.stylefeng.roses.kernel.file.exception.FileException;
@ -69,9 +68,7 @@ public class AliyunFileOperator implements FileOperatorApi {
return ossClient.doesBucketExist(bucketName);
} catch (OSSException | ClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
}
}
@ -87,9 +84,7 @@ public class AliyunFileOperator implements FileOperatorApi {
}
} catch (OSSException | ClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
}
}
@ -99,9 +94,7 @@ public class AliyunFileOperator implements FileOperatorApi {
return ossClient.doesObjectExist(bucketName, key);
} catch (OSSException | ClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
}
}
@ -111,9 +104,7 @@ public class AliyunFileOperator implements FileOperatorApi {
ossClient.putObject(bucketName, key, new ByteArrayInputStream(bytes));
} catch (OSSException | ClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
}
}
@ -124,9 +115,7 @@ public class AliyunFileOperator implements FileOperatorApi {
ossClient.putObject(putObjectRequest);
} catch (OSSException | ClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
}
}
@ -139,9 +128,7 @@ public class AliyunFileOperator implements FileOperatorApi {
return IoUtil.readBytes(objectContent);
} catch (OSSException | ClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
} finally {
IoUtil.close(objectContent);
}
@ -160,9 +147,7 @@ public class AliyunFileOperator implements FileOperatorApi {
}
} catch (OSSException | ClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
}
}
@ -172,9 +157,7 @@ public class AliyunFileOperator implements FileOperatorApi {
ossClient.copyObject(originBucketName, originFileKey, newBucketName, newFileKey);
} catch (OSSException | ClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
}
}
@ -186,9 +169,7 @@ public class AliyunFileOperator implements FileOperatorApi {
return url.toString();
} catch (OSSException | ClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.ALIYUN_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, e.getMessage());
}
}

View File

@ -114,9 +114,7 @@ public class LocalFileOperator implements FileOperatorApi {
if (!FileUtil.exist(absoluteFile)) {
// 组装返回信息
String errorMessage = StrUtil.format("bucket={},key={}", bucketName, key);
String userTip = FileExceptionEnum.FILE_NOT_FOUND.getUserTip();
String finalUserTip = StrUtil.format(userTip, errorMessage);
throw new FileException(FileExceptionEnum.FILE_NOT_FOUND.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.FILE_NOT_FOUND, errorMessage);
} else {
return FileUtil.readBytes(absoluteFile);
}
@ -135,9 +133,7 @@ public class LocalFileOperator implements FileOperatorApi {
if (!FileUtil.exist(originFile)) {
// 组装返回信息
String errorMessage = StrUtil.format("bucket={},key={}", originBucketName, originFileKey);
String userTip = FileExceptionEnum.FILE_NOT_FOUND.getUserTip();
String finalUserTip = StrUtil.format(userTip, errorMessage);
throw new FileException(FileExceptionEnum.FILE_NOT_FOUND.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.FILE_NOT_FOUND, errorMessage);
} else {
// 拷贝文件

View File

@ -3,7 +3,6 @@ package cn.stylefeng.roses.kernel.file.minio;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.file.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.constants.FileConstants;
@ -64,9 +63,7 @@ public class MinIoFileOperator implements FileOperatorApi {
minioClient = new MinioClient(endpoint, accessKey, secretKey);
} catch (InvalidEndpointException | InvalidPortException e) {
// 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
}
}
@ -86,9 +83,7 @@ public class MinIoFileOperator implements FileOperatorApi {
return minioClient.bucketExists(bucketName);
} catch (Exception e) {
// 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
}
}
@ -129,9 +124,7 @@ public class MinIoFileOperator implements FileOperatorApi {
} catch (Exception e) {
// 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
}
}
}
@ -151,9 +144,7 @@ public class MinIoFileOperator implements FileOperatorApi {
return IoUtil.readBytes(inputStream);
} catch (Exception e) {
// 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
}
}
@ -172,9 +163,7 @@ public class MinIoFileOperator implements FileOperatorApi {
} catch (Exception e) {
// 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
}
}
@ -184,9 +173,7 @@ public class MinIoFileOperator implements FileOperatorApi {
minioClient.copyObject(originBucketName, originFileKey, newBucketName, newFileKey);
} catch (Exception e) {
// 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
}
}
@ -209,9 +196,7 @@ public class MinIoFileOperator implements FileOperatorApi {
minioClient.removeObject(bucketName, key);
} catch (Exception e) {
// 组装提示信息
String userTip = FileExceptionEnum.MINIO_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, e.getMessage());
}
}

View File

@ -2,7 +2,6 @@ package cn.stylefeng.roses.kernel.file.tencent;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.file.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.enums.BucketAuthEnum;
import cn.stylefeng.roses.kernel.file.exception.FileException;
@ -93,9 +92,7 @@ public class TenFileOperator implements FileOperatorApi {
return cosClient.doesBucketExist(bucketName);
} catch (CosClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
}
}
@ -111,9 +108,7 @@ public class TenFileOperator implements FileOperatorApi {
}
} catch (CosClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
}
}
@ -144,9 +139,7 @@ public class TenFileOperator implements FileOperatorApi {
cosClient.putObject(bucketName, key, new ByteArrayInputStream(bytes), objectMetadata);
} catch (CosClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
} finally {
IoUtil.close(byteArrayInputStream);
}
@ -169,9 +162,7 @@ public class TenFileOperator implements FileOperatorApi {
cosClient.putObject(bucketName, key, inputStream, objectMetadata);
} catch (CosClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
} finally {
IoUtil.close(inputStream);
}
@ -187,9 +178,7 @@ public class TenFileOperator implements FileOperatorApi {
return IoUtil.readBytes(cosObjectInput);
} catch (CosClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
} finally {
IoUtil.close(cosObjectInput);
}
@ -218,9 +207,7 @@ public class TenFileOperator implements FileOperatorApi {
transferManager.copy(copyObjectRequest, cosClient, null);
} catch (CosClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
}
}
@ -234,9 +221,7 @@ public class TenFileOperator implements FileOperatorApi {
url = cosClient.generatePresignedUrl(presignedUrlRequest);
} catch (CosClientException e) {
// 组装提示信息
String userTip = FileExceptionEnum.TENCENT_FILE_ERROR.getUserTip();
String finalUserTip = StrUtil.format(userTip, e.getMessage());
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR.getErrorCode(), finalUserTip);
throw new FileException(FileExceptionEnum.TENCENT_FILE_ERROR, e.getMessage());
}
return url.toString();
}