Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine exception message of duplicate key #3219

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -26,7 +28,9 @@ public Mono<ExtensionStore> fetchByName(String name) {

@Override
public Mono<ExtensionStore> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
guqing marked this conversation as resolved.
Show resolved Hide resolved

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);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/config/i18n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config/i18n/messages_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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} 已经被安装。
Expand Down