Skip to content

Commit

Permalink
warp json message response instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
Pil0tXia committed Oct 19, 2023
1 parent 4d877ee commit cec6a20
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import static org.apache.eventmesh.admin.enums.Errors.SUCCESS;

import org.apache.eventmesh.admin.enums.Errors;
import org.apache.eventmesh.admin.exception.BaseException;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

Expand All @@ -41,9 +44,9 @@ public class Result<T> {

private Integer pages;

private String message;
private Message message;

public Result(String message) {
public Result(Message message) {
this.message = message;
}

Expand All @@ -56,56 +59,86 @@ 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<>(SUCCESS.getDesc());
return new Result<>(new Message(SUCCESS));
}

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

public static <T> Result<T> success(T data) {
return new Result<>(data, null, SUCCESS.getDesc());
return new Result<>(data, null, new Message(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<>(SUCCESS.getDesc()));
return ResponseEntity.ok(new Result<>(new Message(SUCCESS)));
}

public static <T> ResponseEntity<Result<T>> ok(Result<T> result) {
result.setMessage(SUCCESS.getDesc());
result.setMessage(new Message(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<>(message));
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Result<>(new Message(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<>(message));
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(new Result<>(new Message(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<>(message));
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Result<>(new Message(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<>(message));
return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(new Result<>(new Message(message)));
}

@Data
public static class Message {

private String name;

private String type;

private String desc;

public Message(BaseException e) {
this.name = e.getErrors().name();
this.type = e.getErrors().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 Message(String desc) {
this.desc = desc;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@Getter
public enum Errors {

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

NACOS_SDK_CONFIG_ERR(HttpStatus.INTERNAL_SERVER_ERROR, Types.SDK_CONFIG_ERR,
"Failed to create Nacos ConfigService. Please check EventMeshAdmin application configuration."),
Expand Down Expand Up @@ -72,7 +72,7 @@ public String toString() {
@Getter
public enum Types {

SUCCESS("Operation Success"),
SUCCESS("Successfully received and processed"),

SDK_CONFIG_ERR("The Meta SDK config in EventMeshAdmin application.yml error"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public BaseException(String message) {
* Customized error reporting using enums and exceptions
*/
public BaseException(Errors errors, Throwable cause) {
super(ExceptionUtils.trimDesc(errors.toString()) + COLON + cause.getMessage(), cause);
super(ExceptionUtils.trimDesc(errors.getDesc()) + COLON + cause.getMessage(), cause);
this.errors = errors;
}

public BaseException(Errors errors) {
super(errors.toString());
super(errors.getDesc());
this.errors = errors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.eventmesh.admin.exception;

import org.apache.eventmesh.admin.dto.Result;
import org.apache.eventmesh.admin.dto.Result.Message;

import javax.servlet.http.HttpServletRequest;

Expand All @@ -38,22 +39,22 @@ public class GlobalExceptionHandler {

@ExceptionHandler(BaseException.class)
public ResponseEntity<Result<Object>> baseHandler(BaseException e, HttpServletRequest request) {
String requestURI = request.getRequestURI();
log.error("RESTful API {} service error occurred: ", requestURI, e);
return ResponseEntity.status(e.getErrors().getCode()).body(new Result<>(e.getMessage()));
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)));
}

@ExceptionHandler(RuntimeException.class)
public ResponseEntity<Result<Object>> runtimeHandler(RuntimeException e, HttpServletRequest request) {
String requestURI = request.getRequestURI();
log.error("RESTful API {} runtime error occurred: ", requestURI, e);
String uri = request.getRequestURI();
log.error("RESTful API {} runtime error occurred.", uri, e);
return Result.internalError(e.getMessage());
}

@ExceptionHandler(Exception.class)
public ResponseEntity<Result<Object>> exceptionHandler(Exception e, HttpServletRequest request) {
String requestURI = request.getRequestURI();
log.error("RESTful API {} unknown error occurred: ", requestURI, e);
String uri = request.getRequestURI();
log.error("RESTful API {} unknown error occurred.", uri, e);
return Result.internalError(e.getMessage());
}

Expand Down

0 comments on commit cec6a20

Please sign in to comment.