Skip to content

Commit

Permalink
refactor: add localized exception information for plugin install and …
Browse files Browse the repository at this point in the history
…upgrade
  • Loading branch information
guqing committed Jan 12, 2023
1 parent d4979d9 commit 71b2e21
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.router.IListRequest.QueryListRequest;
import run.halo.app.infra.SystemVersionSupplier;
import run.halo.app.infra.exception.UnsatisfiedAttributeValueException;
import run.halo.app.infra.utils.FileUtils;
import run.halo.app.infra.utils.VersionUtils;
import run.halo.app.plugin.PluginProperties;
Expand Down Expand Up @@ -210,9 +211,12 @@ private void satisfiesRequiresVersion(Plugin newPlugin) {
String systemVersion = version.getNormalVersion();
String requires = newPlugin.getSpec().getRequires();
if (!VersionUtils.satisfiesRequires(systemVersion, requires)) {
throw new ServerWebInputException(String.format(
"Plugin requires a minimum system version of [%s], and you have [%s].",
systemVersion, requires));
throw new UnsatisfiedAttributeValueException(String.format(
"Plugin requires a minimum system version of [%s], but the current version is "
+ "[%s].",
requires, systemVersion),
"problemDetail.plugin.version.unsatisfied.requires",
new String[] {requires, systemVersion});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package run.halo.app.infra.exception;

import jakarta.validation.constraints.Null;
import org.springframework.lang.Nullable;
import org.springframework.web.server.ServerWebInputException;

/**
* {@link ServerWebInputException} subclass that indicates an unsatisfied
* attribute value in request parameters.
*
* @author guqing
* @since 2.2.0
*/
public class UnsatisfiedAttributeValueException extends ServerWebInputException {

public UnsatisfiedAttributeValueException(String reason, @Nullable String messageDetailCode,
@Null Object[] messageDetailArguments) {
super(reason, null, null, messageDetailCode, messageDetailArguments);
}
}
3 changes: 3 additions & 0 deletions src/main/resources/config/i18n/messages.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Title definitions
problemDetail.title.org.springframework.web.server.ServerWebInputException=Bad Request
problemDetail.title.run.halo.app.infra.exception.UnsatisfiedAttributeValueException=Unsatisfied Request Attribute value
problemDetail.title.org.springframework.web.server.UnsupportedMediaTypeStatusException=Unsupported Media Type
problemDetail.title.org.springframework.web.server.MissingRequestValueException=Missing Request Value
problemDetail.title.org.springframework.web.server.UnsatisfiedRequestParameterException=Unsatisfied Request Parameter
Expand Down Expand Up @@ -34,3 +35,5 @@ problemDetail.theme.upgrade.nameMismatch=The current theme name {0} did not matc
problemDetail.theme.install.missingManifest=Missing theme manifest file "theme.yaml" or "theme.yml".
problemDetail.theme.install.alreadyExists=Theme {0} already exists.
problemDetail.directoryTraversal=Directory traversal detected. Base path is {0}, but real path is {1}.

problemDetail.plugin.version.unsatisfied.requires=Plugin requires a minimum system version of {0}, but the current version is {1}.
9 changes: 6 additions & 3 deletions src/main/resources/config/i18n/messages_zh.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
problemDetail.title.org.springframework.web.server.ServerWebInputException=请求参数有误
problemDetail.title.run.halo.app.infra.exception.AttachmentAlreadyExistsException=附件已存在
problemDetail.title.org.springframework.web.server.ServerWebInputException=\u8BF7\u6C42\u53C2\u6570\u6709\u8BEF
problemDetail.title.run.halo.app.infra.exception.UnsatisfiedAttributeValueException=\u8BF7\u6C42\u53C2\u6570\u5C5E\u6027\u503C\u4E0D\u6EE1\u8DB3\u8981\u6C42
problemDetail.title.run.halo.app.infra.exception.AttachmentAlreadyExistsException=\u9644\u4EF6\u5DF2\u5B58\u5728

problemDetail.run.halo.app.infra.exception.AttachmentAlreadyExistsException=文件 {0} 已存在,建议更名后重试。
problemDetail.run.halo.app.infra.exception.AttachmentAlreadyExistsException=\u6587\u4EF6 {0} \u5DF2\u5B58\u5728\uFF0C\u5EFA\u8BAE\u66F4\u540D\u540E\u91CD\u8BD5\u3002

problemDetail.plugin.version.unsatisfied.requires=\u63D2\u4EF6\u8981\u6C42\u4E00\u4E2A\u6700\u5C0F\u7684\u7CFB\u7EDF\u7248\u672C\u4E3A {0}, \u4F46\u5F53\u524D\u7248\u672C\u4E3A {1}\u3002

0 comments on commit 71b2e21

Please sign in to comment.