Skip to content

Commit

Permalink
rename Errors enum to Status and Message sub-class to StatusMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
Pil0tXia committed Oct 20, 2023
1 parent 1de66a0 commit 6bedb3d
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 55 deletions.
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 type;

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();
}

/**
* 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <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>
Expand All @@ -33,7 +33,7 @@
*/

@Getter
public enum Errors {
public enum Status {

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

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

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,8 @@ 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: {}, 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)
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 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6bedb3d

Please sign in to comment.