From 6bedb3d7d4532475752d33c8b0e3210dd6e7556f Mon Sep 17 00:00:00 2001 From: Pil0tXia Date: Sat, 21 Oct 2023 01:44:22 +0800 Subject: [PATCH] rename Errors enum to Status and Message sub-class to StatusMessage --- .../apache/eventmesh/admin/dto/Result.java | 46 +++++++++---------- .../admin/enums/{Errors.java => Status.java} | 6 +-- .../admin/exception/BaseException.java | 18 ++++---- .../exception/EventMeshAdminException.java | 6 +-- .../exception/GlobalExceptionHandler.java | 8 ++-- .../admin/exception/MetaException.java | 10 ++-- .../impl/NacosSubscriptionService.java | 12 ++--- .../ExceptionUtil.java} | 4 +- 8 files changed, 55 insertions(+), 55 deletions(-) rename eventmesh-admin/src/main/java/org/apache/eventmesh/admin/enums/{Errors.java => Status.java} (93%) rename eventmesh-admin/src/main/java/org/apache/eventmesh/admin/{utils/ExceptionUtils.java => util/ExceptionUtil.java} (94%) diff --git a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/dto/Result.java b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/dto/Result.java index 15b3dc2f42..9580564b93 100644 --- a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/dto/Result.java +++ b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/dto/Result.java @@ -17,9 +17,9 @@ package org.apache.eventmesh.admin.dto; -import static org.apache.eventmesh.admin.enums.Errors.SUCCESS; +import static org.apache.eventmesh.admin.enums.Status.SUCCESS; -import org.apache.eventmesh.admin.enums.Errors; +import org.apache.eventmesh.admin.enums.Status; import org.apache.eventmesh.admin.exception.BaseException; import org.springframework.http.HttpStatus; @@ -44,10 +44,10 @@ public class Result { private Integer pages; - private Message message; + private StatusMessage message; - public Result(Message message) { - this.message = message; + public Result(StatusMessage statusMessage) { + this.message = statusMessage; } public Result(T data, Integer pages) { @@ -59,16 +59,16 @@ public Result(T data, Integer pages) { * The request is valid and the result is wrapped in {@link Result}. */ public static Result success() { - return new Result<>(new Message(SUCCESS)); + return new Result<>(new StatusMessage(SUCCESS)); } public static Result success(Result result) { - result.setMessage(new Message(SUCCESS)); + result.setMessage(new StatusMessage(SUCCESS)); return result; } public static Result success(T data) { - return new Result<>(data, null, new Message(SUCCESS)); + return new Result<>(data, null, new StatusMessage(SUCCESS)); } /** @@ -76,11 +76,11 @@ public static Result success(T data) { * Logic issues should use 422 Unprocessable Entity instead of 200 OK. */ public static ResponseEntity> ok() { - return ResponseEntity.ok(new Result<>(new Message(SUCCESS))); + return ResponseEntity.ok(new Result<>(new StatusMessage(SUCCESS))); } public static ResponseEntity> ok(Result result) { - result.setMessage(new Message(SUCCESS)); + result.setMessage(new StatusMessage(SUCCESS)); return ResponseEntity.ok(result); } @@ -88,32 +88,32 @@ public static ResponseEntity> ok(Result result) { * The request is invalid. */ public static ResponseEntity> badRequest(String message) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Result<>(new Message(message))); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Result<>(new StatusMessage(message))); } /** * The request is valid but cannot be processed due to business logic issues. */ public static ResponseEntity> unprocessable(String message) { - return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(new Result<>(new Message(message))); + return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(new Result<>(new StatusMessage(message))); } /** * Uncaught exception happened in EventMeshAdmin application. */ public static ResponseEntity> internalError(String message) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Result<>(new Message(message))); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Result<>(new StatusMessage(message))); } /** * Upstream service unavailable such as Meta. */ public static ResponseEntity> badGateway(String message) { - return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(new Result<>(new Message(message))); + return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(new Result<>(new StatusMessage(message))); } @Data - public static class Message { + public static class StatusMessage { private String name; @@ -121,9 +121,9 @@ public static class Message { private String desc; - public Message(BaseException e) { - this.name = e.getErrors().name(); - this.type = e.getErrors().getType().name(); + public StatusMessage(BaseException e) { + this.name = e.getStatus().name(); + this.type = e.getStatus().getType().name(); this.desc = e.getMessage(); } @@ -131,13 +131,13 @@ public Message(BaseException e) { * Only recommended for returning successful results, * the stack trace cannot be displayed when returning unsuccessful results. */ - public Message(Errors errors) { - this.name = errors.name(); - this.type = errors.getType().name(); - this.desc = errors.getDesc(); // no stack trace + public StatusMessage(Status status) { + this.name = status.name(); + this.type = status.getType().name(); + this.desc = status.getDesc(); // no stack trace } - public Message(String desc) { + public StatusMessage(String desc) { this.desc = desc; } } diff --git a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/enums/Errors.java b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/enums/Status.java similarity index 93% rename from eventmesh-admin/src/main/java/org/apache/eventmesh/admin/enums/Errors.java rename to eventmesh-admin/src/main/java/org/apache/eventmesh/admin/enums/Status.java index d97cfcc8a5..6f34910bf6 100644 --- a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/enums/Errors.java +++ b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/enums/Status.java @@ -24,7 +24,7 @@ import lombok.Getter; /** - * An enumeration class that conforms to the RESTful specifications and custom error reporting requirements. + * An error enumeration class that conforms to the RESTful specifications and custom error reporting requirements. *
    *
  • The 'code' field is used to return the HTTP status code using {@link HttpStatus}.
  • *
  • The 'type' field represents the major category of the error.
  • @@ -33,7 +33,7 @@ */ @Getter -public enum Errors { +public enum Status { SUCCESS(HttpStatus.OK, Types.SUCCESS, "Operation success."), @@ -58,7 +58,7 @@ public enum Errors { // error message private final String desc; - Errors(HttpStatus code, Types type, String desc) { + Status(HttpStatus code, Types type, String desc) { this.code = code; this.type = type; this.desc = desc; diff --git a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/BaseException.java b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/BaseException.java index e6af01ede5..dfaee05550 100644 --- a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/BaseException.java +++ b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/BaseException.java @@ -19,8 +19,8 @@ import static org.apache.eventmesh.admin.constant.ConfigConst.COLON; -import org.apache.eventmesh.admin.enums.Errors; -import org.apache.eventmesh.admin.utils.ExceptionUtils; +import org.apache.eventmesh.admin.enums.Status; +import org.apache.eventmesh.admin.util.ExceptionUtil; import lombok.Getter; @@ -33,7 +33,7 @@ public class BaseException extends RuntimeException { private static final long serialVersionUID = 3509261993355721168L; - private Errors errors; + private Status status; public BaseException(String message) { super(message); @@ -42,13 +42,13 @@ public BaseException(String message) { /** * Customized error reporting using enums and exceptions */ - public BaseException(Errors errors, Throwable cause) { - super(ExceptionUtils.trimDesc(errors.getDesc()) + COLON + cause.getMessage(), cause); - this.errors = errors; + public BaseException(Status status, Throwable cause) { + super(ExceptionUtil.trimDesc(status.getDesc()) + COLON + cause.getMessage(), cause); + this.status = status; } - public BaseException(Errors errors) { - super(errors.getDesc()); - this.errors = errors; + public BaseException(Status status) { + super(status.getDesc()); + this.status = status; } } diff --git a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/EventMeshAdminException.java b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/EventMeshAdminException.java index 3847f680cc..00feec5dbf 100644 --- a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/EventMeshAdminException.java +++ b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/EventMeshAdminException.java @@ -17,7 +17,7 @@ package org.apache.eventmesh.admin.exception; -import org.apache.eventmesh.admin.enums.Errors; +import org.apache.eventmesh.admin.enums.Status; /** * EventMeshAdmin Application side exception @@ -34,7 +34,7 @@ public EventMeshAdminException(String message) { /** * Customized error reporting using enums and exceptions */ - public EventMeshAdminException(Errors errors, Throwable cause) { - super(errors, cause); + public EventMeshAdminException(Status status, Throwable cause) { + super(status, cause); } } diff --git a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/GlobalExceptionHandler.java b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/GlobalExceptionHandler.java index a54e17a58c..9714a548cc 100644 --- a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/GlobalExceptionHandler.java +++ b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/GlobalExceptionHandler.java @@ -18,7 +18,7 @@ package org.apache.eventmesh.admin.exception; import org.apache.eventmesh.admin.dto.Result; -import org.apache.eventmesh.admin.dto.Result.Message; +import org.apache.eventmesh.admin.dto.Result.StatusMessage; import javax.servlet.http.HttpServletRequest; @@ -29,7 +29,7 @@ import lombok.extern.slf4j.Slf4j; /** - * This class, in conjunction with {@linkplain org.apache.eventmesh.admin.enums.Errors Errors} and {@link BaseException}, + * This class, in conjunction with {@linkplain org.apache.eventmesh.admin.enums.Status Status} and {@link BaseException}, * collectively implements customized error reporting. */ @@ -40,8 +40,8 @@ public class GlobalExceptionHandler { @ExceptionHandler(BaseException.class) public ResponseEntity> baseHandler(BaseException e, HttpServletRequest request) { String uri = request.getRequestURI(); - log.error("RESTful API {} service error occurred, name: {}, type: {}", uri, e.getErrors().name(), e.getErrors().getType().name(), e); - return ResponseEntity.status(e.getErrors().getCode()).body(new Result<>(new Message(e))); + log.error("RESTful API {} service error occurred, name: {}, type: {}", uri, e.getStatus().name(), e.getStatus().getType().name(), e); + return ResponseEntity.status(e.getStatus().getCode()).body(new Result<>(new StatusMessage(e))); } @ExceptionHandler(RuntimeException.class) diff --git a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/MetaException.java b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/MetaException.java index 9302102466..2028eae387 100644 --- a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/MetaException.java +++ b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/exception/MetaException.java @@ -17,7 +17,7 @@ package org.apache.eventmesh.admin.exception; -import org.apache.eventmesh.admin.enums.Errors; +import org.apache.eventmesh.admin.enums.Status; /** * Meta side exception with EventMeshAdmin Application @@ -34,11 +34,11 @@ public MetaException(String message) { /** * Customized error reporting using enums and exceptions */ - public MetaException(Errors errors, Throwable cause) { - super(errors, cause); + public MetaException(Status status, Throwable cause) { + super(status, cause); } - public MetaException(Errors errors) { - super(errors); + public MetaException(Status status) { + super(status); } } diff --git a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/service/impl/NacosSubscriptionService.java b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/service/impl/NacosSubscriptionService.java index ea210b0a80..282dab135b 100644 --- a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/service/impl/NacosSubscriptionService.java +++ b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/service/impl/NacosSubscriptionService.java @@ -17,15 +17,15 @@ package org.apache.eventmesh.admin.service.impl; -import static org.apache.eventmesh.admin.enums.Errors.NACOS_EMPTY_RESP_ERR; -import static org.apache.eventmesh.admin.enums.Errors.NACOS_GET_CONFIGS_ERR; -import static org.apache.eventmesh.admin.enums.Errors.NACOS_LOGIN_EMPTY_RESP_ERR; -import static org.apache.eventmesh.admin.enums.Errors.NACOS_LOGIN_ERR; -import static org.apache.eventmesh.admin.enums.Errors.NACOS_SDK_CONFIG_ERR; +import static org.apache.eventmesh.admin.enums.Status.NACOS_EMPTY_RESP_ERR; +import static org.apache.eventmesh.admin.enums.Status.NACOS_GET_CONFIGS_ERR; +import static org.apache.eventmesh.admin.enums.Status.NACOS_LOGIN_EMPTY_RESP_ERR; +import static org.apache.eventmesh.admin.enums.Status.NACOS_LOGIN_ERR; +import static org.apache.eventmesh.admin.enums.Status.NACOS_SDK_CONFIG_ERR; +import org.apache.eventmesh.admin.config.AdminProperties; import org.apache.eventmesh.admin.constant.ConfigConst; import org.apache.eventmesh.admin.constant.NacosConst; -import org.apache.eventmesh.admin.config.AdminProperties; import org.apache.eventmesh.admin.dto.Result; import org.apache.eventmesh.admin.exception.EventMeshAdminException; import org.apache.eventmesh.admin.exception.MetaException; diff --git a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/utils/ExceptionUtils.java b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/util/ExceptionUtil.java similarity index 94% rename from eventmesh-admin/src/main/java/org/apache/eventmesh/admin/utils/ExceptionUtils.java rename to eventmesh-admin/src/main/java/org/apache/eventmesh/admin/util/ExceptionUtil.java index 1069e082fd..db82be63f7 100644 --- a/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/utils/ExceptionUtils.java +++ b/eventmesh-admin/src/main/java/org/apache/eventmesh/admin/util/ExceptionUtil.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package org.apache.eventmesh.admin.utils; +package org.apache.eventmesh.admin.util; -public class ExceptionUtils { +public class ExceptionUtil { /** * Remove the last period of exception description.