From 53def5bda853f30b4d7a1c21e4c0c4552737da8e Mon Sep 17 00:00:00 2001 From: Halo Dev Bot <87291978+halo-dev-bot@users.noreply.github.com> Date: Fri, 10 Feb 2023 14:46:14 +0800 Subject: [PATCH] [release-2.2] Refine exception message of duplicate key (#3278) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is an automated cherry-pick of #3219 /assign ruibaby ```release-note 优化名称重复的错误提示 ``` --- .../ReactiveExtensionStoreClientImpl.java | 6 ++++- .../exception/DuplicateNameException.java | 24 +++++++++++++++++++ .../resources/config/i18n/messages.properties | 2 ++ .../config/i18n/messages_zh.properties | 2 ++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/main/java/run/halo/app/infra/exception/DuplicateNameException.java diff --git a/src/main/java/run/halo/app/extension/store/ReactiveExtensionStoreClientImpl.java b/src/main/java/run/halo/app/extension/store/ReactiveExtensionStoreClientImpl.java index 0aff607864..b8056d291b 100644 --- a/src/main/java/run/halo/app/extension/store/ReactiveExtensionStoreClientImpl.java +++ b/src/main/java/run/halo/app/extension/store/ReactiveExtensionStoreClientImpl.java @@ -1,9 +1,11 @@ package run.halo.app.extension.store; +import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import run.halo.app.infra.exception.DuplicateNameException; @Component public class ReactiveExtensionStoreClientImpl implements ReactiveExtensionStoreClient { @@ -26,7 +28,9 @@ public Mono fetchByName(String name) { @Override public Mono create(String name, byte[] data) { - return repository.save(new ExtensionStore(name, data)); + return repository.save(new ExtensionStore(name, data)) + .onErrorMap(DuplicateKeyException.class, + t -> new DuplicateNameException("Duplicate name detected.", t)); } @Override diff --git a/src/main/java/run/halo/app/infra/exception/DuplicateNameException.java b/src/main/java/run/halo/app/infra/exception/DuplicateNameException.java new file mode 100644 index 0000000000..a68889c109 --- /dev/null +++ b/src/main/java/run/halo/app/infra/exception/DuplicateNameException.java @@ -0,0 +1,24 @@ +package run.halo.app.infra.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.server.ResponseStatusException; + +public class DuplicateNameException extends ResponseStatusException { + + public DuplicateNameException() { + this("Duplicate name detected"); + } + + public DuplicateNameException(String reason) { + this(reason, null); + } + + public DuplicateNameException(String reason, Throwable cause) { + this(reason, cause, null, null); + } + + public DuplicateNameException(String reason, Throwable cause, String messageDetailCode, + Object[] messageDetailArguments) { + super(HttpStatus.BAD_REQUEST, reason, cause, messageDetailCode, messageDetailArguments); + } +} diff --git a/src/main/resources/config/i18n/messages.properties b/src/main/resources/config/i18n/messages.properties index f0dd3280c1..eb0900a310 100644 --- a/src/main/resources/config/i18n/messages.properties +++ b/src/main/resources/config/i18n/messages.properties @@ -15,6 +15,7 @@ problemDetail.title.reactor.core.Exceptions.RetryExhaustedException=Retry Exhaus problemDetail.title.run.halo.app.infra.exception.ThemeInstallationException=Theme Install Error problemDetail.title.run.halo.app.infra.exception.ThemeUpgradeException=Theme Upgrade Error problemDetail.title.run.halo.app.infra.exception.PluginInstallationException=Plugin Install Error +problemDetail.title.run.halo.app.infra.exception.DuplicateNameException=Duplicate Name Error # Detail definitions problemDetail.org.springframework.web.server.UnsupportedMediaTypeStatusException=Content type {0} is not supported. Supported media types: {1}. @@ -28,6 +29,7 @@ problemDetail.org.springframework.web.server.ServerErrorException={0}. problemDetail.org.springframework.web.server.MethodNotAllowedException=Request method {0} is not supported. Supported methods: {1}. problemDetail.run.halo.app.extension.exception.SchemaViolationException={1} of schema {0}. problemDetail.run.halo.app.infra.exception.AttachmentAlreadyExistsException=File {0} already exists, please rename it and try again. +problemDetail.run.halo.app.infra.exception.DuplicateNameException=Duplicate name detected, please rename it and retry. problemDetail.comment.turnedOff=The comment function has been turned off. problemDetail.comment.systemUsersOnly=Allow only system users to comment diff --git a/src/main/resources/config/i18n/messages_zh.properties b/src/main/resources/config/i18n/messages_zh.properties index a35ca8f251..92bca6fbb1 100644 --- a/src/main/resources/config/i18n/messages_zh.properties +++ b/src/main/resources/config/i18n/messages_zh.properties @@ -2,8 +2,10 @@ problemDetail.title.org.springframework.web.server.ServerWebInputException=请 problemDetail.title.run.halo.app.infra.exception.UnsatisfiedAttributeValueException=请求参数属性值不满足要求 problemDetail.title.run.halo.app.infra.exception.PluginInstallationException=插件安装失败 problemDetail.title.run.halo.app.infra.exception.AttachmentAlreadyExistsException=附件已存在 +problemDetail.title.run.halo.app.infra.exception.DuplicateNameException=名称重复 problemDetail.run.halo.app.infra.exception.AttachmentAlreadyExistsException=文件 {0} 已存在,建议更名后重试。 +problemDetail.run.halo.app.infra.exception.DuplicateNameException=检测到有重复的名称,请重命名后重试。 problemDetail.plugin.version.unsatisfied.requires=插件要求一个最小的系统版本为 {0}, 但当前版本为 {1}。 problemDetail.plugin.install.alreadyInstalled=插件 {0} 已经被安装。