Skip to content

Commit

Permalink
[Optimization-3957][web] Improved the missing exception message when …
Browse files Browse the repository at this point in the history
…uploading files in the Resource Center. (#3958)
  • Loading branch information
zhuangchong authored Nov 26, 2024
1 parent 117e1a6 commit fa16032
Show file tree
Hide file tree
Showing 8 changed files with 5,015 additions and 404 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public class WebExceptionHandler {
@ExceptionHandler
@ResponseBody
public Result<Void> busException(BusException e) {
log.error("BusException:", e);
return Result.failed(e.getMsg());
log.error(e.getMessage(), e);
return Result.failed(e.getMessage());
}

private static final Map<String, Status> ERR_CODE_MAPPING = MapUtil.<String, Status>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class RsURLConnection extends URLConnection {
public void connect() {
BaseResourceManager instance = BaseResourceManager.getInstance();
if (instance == null) {
throw BusException.valueOf("ResourceManager is disabled");
throw BusException.of("ResourceManager is disabled");
}
inputStream = instance.readFile(getURL().getPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public void remove(String path) {
try {
boolean isSuccess = FileUtil.del(getFilePath(path));
if (!isSuccess) {
throw new BusException("remove file failed,reason unknown");
throw BusException.of(Status.RESOURCE_FILE_DELETE_FAILED, "unknown");
}
} catch (IORuntimeException e) {
throw new BusException(Status.RESOURCE_FILE_DELETE_FAILED, e);
throw BusException.of(e, Status.RESOURCE_FILE_DELETE_FAILED);
}
}

Expand All @@ -67,7 +67,7 @@ public void rename(String path, String newPath) {
String newName = FileUtil.getName(newPath);
FileUtil.rename(new File(getFilePath(path)), newName, true);
} catch (Exception e) {
throw new BusException(Status.RESOURCE_FILE_RENAME_FAILED, e);
throw BusException.of(e, Status.RESOURCE_FILE_RENAME_FAILED);
}
}

Expand All @@ -76,7 +76,7 @@ public void putFile(String path, InputStream fileStream) {
try {
FileUtil.writeFromStream(fileStream, getFilePath(path));
} catch (Exception e) {
throw new BusException(Status.RESOURCE_FILE_UPLOAD_FAILED, e);
throw BusException.of(e, Status.RESOURCE_FILE_UPLOAD_FAILED);
}
}

Expand Down Expand Up @@ -125,7 +125,7 @@ public List<ResourcesVO> getFullDirectoryStructure(int rootId) {
.filter(Objects::nonNull)
.collect(Collectors.toList());
} catch (IOException e) {
throw new BusException(Status.RESOURCE_FILE_PATH_VISIT_FAILED, e);
throw BusException.of(e, Status.RESOURCE_FILE_PATH_VISIT_FAILED);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class RsURLConnection extends URLConnection {
public void connect() {
BaseResourceManager instance = BaseResourceManager.getInstance();
if (instance == null) {
throw BusException.valueOf("ResourceManager is disabled");
throw BusException.of("ResourceManager is disabled");
}
inputStream = instance.readFile(getURL().getPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.dinky.data.enums.Status;

import javax.annotation.Nullable;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -39,69 +41,92 @@
@Setter
public class BusException extends RuntimeException {

private static final long serialVersionUID = -2955156471454043812L;
private static final long serialVersionUID = 1L;

/** 异常信息的code码 */
/** Exception status code */
private Status code;

/** 异常信息的参数 */
/** Exception parameters */
private Object[] errorArgs;

/** 如果有值得话,将不会取i18n里面的错误信息 */
private String msg;

/**
* Constructs a BusException with the specified message.
*
* @param message the detail message
*/
public BusException(String message) {
super(message);
setMsg(message);
}

public BusException(Status status) {
super(status.getMessage());
setCode(status);
setMsg(status.getMessage());
}

/**
* Constructs a BusException with the specified status and error arguments.
*
* @param status the status code representing the exception
* @param errorArgs the arguments used for error message formatting
*/
public BusException(Status status, Object... errorArgs) {
super(status.getMessage());
setCode(status);
setMsg(StrUtil.format(status.getMessage(), errorArgs));
super(formatMessage(null, status, errorArgs));
this.code = status;
this.errorArgs = errorArgs;
}

/**
* An exception that gets the error message through i 18n
* Constructs a BusException with the specified cause, status, and error arguments.
*
* @param message code
* @param e e
* @return {@link BusException}
* @param cause the cause of the exception
* @param status the status code representing the exception
* @param errorArgs the arguments used for error message formatting
*/
public static BusException valueOf(String message, Throwable e) {
log.error(message, e);
return new BusException(message + e.getMessage());
public BusException(Throwable cause, Status status, Object... errorArgs) {
super(formatMessage(cause.getMessage(), status, errorArgs), cause);
this.code = status;
this.errorArgs = errorArgs;
}

public static BusException valueOf(Status code, Throwable e) {
log.error(code.getMessage(), e);
return new BusException(code, e.getMessage());
/**
* Creates a BusException instance with the specified message.
*
* @param message the detail message
* @return a new BusException instance
*/
public static BusException of(String message) {
return new BusException(message);
}

/**
* An exception that gets the error message through i 18n
* Creates a BusException instance with the specified status, and error arguments.
*
* @param code code
* @param errorArgs errorArgs
* @return {@link BusException}
* @param status the status code representing the exception
* @param errorArgs the arguments used for error message formatting
* @return a new BusException instance
*/
public static BusException valueOf(Status code, Object... errorArgs) {
return new BusException(code, errorArgs);
public static BusException of(Status status, Object... errorArgs) {
return new BusException(status, errorArgs);
}

/**
* Without passing the exception to i 18n, it is directly returned to the msg past
* Creates a BusException instance with the specified cause, status, and error arguments.
*
* @param msg msg
* @return {@link BusException}
* @param cause the cause of the exception
* @param status the status code representing the exception
* @param errorArgs the arguments used for error message formatting
* @return a new BusException instance
*/
public static BusException valueOf(String msg) {
return new BusException(msg);
public static BusException of(Throwable cause, Status status, Object... errorArgs) {
return new BusException(cause, status, errorArgs);
}

/** Formats the exception message with optional cause message and error arguments. */
private static String formatMessage(@Nullable String causeMessage, Status status, Object... errorArgs) {
Object[] args = errorArgs == null ? new Object[0] : errorArgs;

if (causeMessage != null) {
Object[] extendedArgs = new Object[args.length + 1];
System.arraycopy(args, 0, extendedArgs, 0, args.length);
extendedArgs[args.length] = causeMessage;
args = extendedArgs;
}

return StrUtil.format(status.getMessage(), args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ role.binding.row.permission=Role Already Binding Row Permission , Can Not Delete
# dinky-admin
unknown.i18n=Unknown i18n information, please check. . .

file.upload.failed=File upload failed, reason: {0}
file.rename.failed=File rename failed, reason: {0}
file.delete.failed=File delete failed, reason: {0}
file.read.failed=File read failed, reason: {0}
file.upload.failed=File upload failed, reason: {}
file.rename.failed=File rename failed, reason: {}
file.delete.failed=File delete failed, reason: {}
file.read.failed=File read failed, reason: {}

catalogue.sort.value.first_letter=First Letter
catalogue.sort.value.create_time=Create Time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ role.binding.row.permission=该角色已绑定行权限,无法删除
# dinky-admin
unknown.i18n=未知 i18n 信息,请检查. . .

file.upload.failed=文件上传失败, 原因: {0}
file.rename.failed=文件重命名失败, 原因: {0}
file.delete.failed=文件删除失败, 原因: {0}
file.read.failed=文件读取失败, 原因: {0}
file.upload.failed=文件上传失败, 原因: {}
file.rename.failed=文件重命名失败, 原因: {}
file.delete.failed=文件删除失败, 原因: {}
file.read.failed=文件读取失败, 原因: {}

daemon.task.config.not.exist=线程任务配置不能为空
daemon.task.not.support=不支持线程任务类型:
Expand Down
Loading

0 comments on commit fa16032

Please sign in to comment.