Skip to content

Commit

Permalink
[ISSUE apache#4521] A poor naming. (apache#4524)
Browse files Browse the repository at this point in the history
* rename common package to constant

* rename Errors enum to Status and Message sub-class to StatusMessage

* Exact 'Types' to first level of classification 'Category'

* remove 'error' in Status javadoc
  • Loading branch information
Pil0tXia authored Oct 31, 2023
1 parent 55865a6 commit 2bfc1ae
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.eventmesh.admin.config;

import org.apache.eventmesh.admin.common.ConfigConst;
import org.apache.eventmesh.admin.constant.ConfigConst;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package org.apache.eventmesh.admin.config;

import static org.apache.eventmesh.admin.common.ConfigConst.META_TYPE_ETCD;
import static org.apache.eventmesh.admin.common.ConfigConst.META_TYPE_NACOS;
import static org.apache.eventmesh.admin.constant.ConfigConst.META_TYPE_ETCD;
import static org.apache.eventmesh.admin.constant.ConfigConst.META_TYPE_NACOS;

import org.apache.eventmesh.admin.service.ConnectionService;
import org.apache.eventmesh.admin.service.SubscriptionService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.eventmesh.admin.common;
package org.apache.eventmesh.admin.constant;

public class ConfigConst {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.eventmesh.admin.common;
package org.apache.eventmesh.admin.constant;

public class NacosConst {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,10 +44,10 @@ public class Result<T> {

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) {
Expand All @@ -59,85 +59,85 @@ public Result(T data, Integer pages) {
* The request is valid and the result is wrapped in {@link Result}.
*/
public static <T> Result<T> success() {
return new Result<>(new Message(SUCCESS));
return new Result<>(new StatusMessage(SUCCESS));
}

public static <T> Result<T> success(Result<T> result) {
result.setMessage(new Message(SUCCESS));
result.setMessage(new StatusMessage(SUCCESS));
return result;
}

public static <T> Result<T> success(T data) {
return new Result<>(data, null, new Message(SUCCESS));
return new Result<>(data, null, new StatusMessage(SUCCESS));
}

/**
* The request is valid and the result is returned in {@link ResponseEntity}.
* Logic issues should use 422 Unprocessable Entity instead of 200 OK.
*/
public static <T> ResponseEntity<Result<T>> ok() {
return ResponseEntity.ok(new Result<>(new Message(SUCCESS)));
return ResponseEntity.ok(new Result<>(new StatusMessage(SUCCESS)));
}

public static <T> ResponseEntity<Result<T>> ok(Result<T> result) {
result.setMessage(new Message(SUCCESS));
result.setMessage(new StatusMessage(SUCCESS));
return ResponseEntity.ok(result);
}

/**
* The request is invalid.
*/
public static <T> ResponseEntity<Result<T>> 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 <T> ResponseEntity<Result<T>> 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 <T> ResponseEntity<Result<T>> 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 <T> ResponseEntity<Result<T>> 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;
private String status;

private String type;
private String category;

private String desc;

public Message(BaseException e) {
this.name = e.getErrors().name();
this.type = e.getErrors().getType().name();
public StatusMessage(BaseException e) {
this.status = e.getStatus().name();
this.category = e.getStatus().getCategory().name();
this.desc = e.getMessage();
}

/**
* 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.status = status.name();
this.category = status.getCategory().name();
this.desc = status.getDesc(); // no stack trace
}

public Message(String desc) {
public StatusMessage(String desc) {
this.desc = desc;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.eventmesh.admin.enums;

import static org.apache.eventmesh.admin.common.ConfigConst.COLON;
import static org.apache.eventmesh.admin.constant.ConfigConst.COLON;

import org.springframework.http.HttpStatus;

Expand All @@ -27,50 +27,50 @@
* An enumeration class that conforms to the RESTful specifications and custom error reporting requirements.
* <ul>
* <li>The 'code' field is used to return the HTTP status code using {@link HttpStatus}.</li>
* <li>The 'type' field represents the major category of the error.</li>
* <li>The 'category' field represents the major category of the error.</li>
* <li>the 'desc' field represents the detailed subcategory and information of the error.</li>
* </ul>
*/

@Getter
public enum Errors {
public enum Status {

SUCCESS(HttpStatus.OK, Types.SUCCESS, "Operation success."),
SUCCESS(HttpStatus.OK, Category.SUCCESS, "Operation success."),

NACOS_SDK_CONFIG_ERR(HttpStatus.INTERNAL_SERVER_ERROR, Types.SDK_CONFIG_ERR,
NACOS_SDK_CONFIG_ERR(HttpStatus.INTERNAL_SERVER_ERROR, Category.SDK_CONFIG_ERR,
"Failed to create Nacos ConfigService. Please check EventMeshAdmin application configuration."),

NACOS_GET_CONFIGS_ERR(HttpStatus.BAD_GATEWAY, Types.META_COM_ERR, "Failed to retrieve Nacos config(s)."),
NACOS_GET_CONFIGS_ERR(HttpStatus.BAD_GATEWAY, Category.META_COM_ERR, "Failed to retrieve Nacos config(s)."),

NACOS_EMPTY_RESP_ERR(HttpStatus.BAD_GATEWAY, Types.META_COM_ERR, "No result returned by Nacos. Please check Nacos."),
NACOS_EMPTY_RESP_ERR(HttpStatus.BAD_GATEWAY, Category.META_COM_ERR, "No result returned by Nacos. Please check Nacos."),

NACOS_LOGIN_ERR(HttpStatus.UNAUTHORIZED, Types.META_COM_ERR, "Nacos login failed."),
NACOS_LOGIN_ERR(HttpStatus.UNAUTHORIZED, Category.META_COM_ERR, "Nacos login failed."),

NACOS_LOGIN_EMPTY_RESP_ERR(HttpStatus.BAD_GATEWAY, Types.META_COM_ERR, "Nacos didn't return accessToken. Please check Nacos status."),
NACOS_LOGIN_EMPTY_RESP_ERR(HttpStatus.BAD_GATEWAY, Category.META_COM_ERR, "Nacos didn't return accessToken. Please check Nacos status."),
;

// error code
private final HttpStatus code;

// error type
private final Types type;
private final Category category;

// error message
private final String desc;

Errors(HttpStatus code, Types type, String desc) {
Status(HttpStatus code, Category category, String desc) {
this.code = code;
this.type = type;
this.category = category;
this.desc = desc;
}

@Override
public String toString() {
return name() + " of " + type + COLON + desc;
return name() + " of " + category + COLON + desc;
}

@Getter
public enum Types {
public enum Category {

SUCCESS("Successfully received and processed"),

Expand All @@ -84,7 +84,7 @@ public enum Types {
*/
private final String desc;

Types(String desc) {
Category(String desc) {
this.desc = desc;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package org.apache.eventmesh.admin.exception;

import static org.apache.eventmesh.admin.common.ConfigConst.COLON;
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;

Expand All @@ -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);
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
*/

Expand All @@ -40,8 +40,9 @@ public class GlobalExceptionHandler {
@ExceptionHandler(BaseException.class)
public ResponseEntity<Result<Object>> 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: {}, category: {}",
uri, e.getStatus().name(), e.getStatus().getCategory().name(), e);
return ResponseEntity.status(e.getStatus().getCode()).body(new Result<>(new StatusMessage(e)));
}

@ExceptionHandler(RuntimeException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 org.apache.eventmesh.admin.common.ConfigConst;
import org.apache.eventmesh.admin.common.NacosConst;
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.dto.Result;
import org.apache.eventmesh.admin.exception.EventMeshAdminException;
import org.apache.eventmesh.admin.exception.MetaException;
Expand Down
Loading

0 comments on commit 2bfc1ae

Please sign in to comment.