forked from apache/eventmesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE apache#4494] RESTful API framework for EventMeshAdmin (apache#…
…4498) * restful response optimize part1 * restful response optimize part2 * restful response optimize part3 * restful response optimize part4 * exception handling * exception handling part2 * exception handling part3 * exception handling part4 * exception handling part5 * add error type to error displaying * warp json message response instead of string
- Loading branch information
Showing
16 changed files
with
516 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
eventmesh-admin/src/main/java/org/apache/eventmesh/admin/common/NacosConst.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.eventmesh.admin.common; | ||
|
||
public class NacosConst { | ||
|
||
public static final String LOGIN_API = "/nacos/v1/auth/login"; | ||
|
||
public static final String LOGIN_REQ_USERNAME = "username"; | ||
public static final String LOGIN_REQ_PASSWORD = "password"; | ||
|
||
public static final String LOGIN_RESP_TOKEN = "accessToken"; | ||
|
||
public static final String CONFIGS_API = "/nacos/v1/cs/configs"; | ||
|
||
public static final String CONFIGS_REQ_PAGE = "pageNo"; | ||
public static final String CONFIGS_REQ_PAGE_SIZE = "pageSize"; | ||
public static final String CONFIGS_REQ_DATAID = "dataId"; | ||
public static final String CONFIGS_REQ_GROUP = "group"; | ||
public static final String CONFIGS_REQ_SEARCH = "search"; | ||
public static final String CONFIGS_REQ_TOKEN = "accessToken"; | ||
|
||
public static final String CONFIGS_RESP_CONTENT_LIST = "pageItems"; // json page data list field | ||
public static final String CONFIGS_RESP_CONTENT = "content"; // json page data field | ||
public static final String CONFIGS_RESP_PAGES = "pagesAvailable"; // json total pages field | ||
public static final String CONFIGS_RESP_DATAID = "dataId"; | ||
public static final String CONFIGS_RESP_GROUP = "group"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
eventmesh-admin/src/main/java/org/apache/eventmesh/admin/dto/Result.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.eventmesh.admin.dto; | ||
|
||
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; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* A RESTful response DTO. | ||
*/ | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Result<T> { | ||
|
||
private T data; | ||
|
||
private Integer pages; | ||
|
||
private Message message; | ||
|
||
public Result(Message message) { | ||
this.message = message; | ||
} | ||
|
||
public Result(T data, Integer pages) { | ||
this.data = data; | ||
this.pages = 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)); | ||
} | ||
|
||
public static <T> Result<T> success(Result<T> result) { | ||
result.setMessage(new Message(SUCCESS)); | ||
return result; | ||
} | ||
|
||
public static <T> Result<T> success(T data) { | ||
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<>(new Message(SUCCESS))); | ||
} | ||
|
||
public static <T> ResponseEntity<Result<T>> ok(Result<T> result) { | ||
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<>(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<>(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<>(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<>(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; | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
eventmesh-admin/src/main/java/org/apache/eventmesh/admin/enums/Errors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.eventmesh.admin.enums; | ||
|
||
import static org.apache.eventmesh.admin.common.ConfigConst.COLON; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import lombok.Getter; | ||
|
||
/** | ||
* 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 'desc' field represents the detailed subcategory and information of the error.</li> | ||
* </ul> | ||
*/ | ||
|
||
@Getter | ||
public enum Errors { | ||
|
||
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."), | ||
|
||
NACOS_GET_CONFIGS_ERR(HttpStatus.BAD_GATEWAY, Types.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_LOGIN_ERR(HttpStatus.UNAUTHORIZED, Types.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."), | ||
; | ||
|
||
// error code | ||
private final HttpStatus code; | ||
|
||
// error type | ||
private final Types type; | ||
|
||
// error message | ||
private final String desc; | ||
|
||
Errors(HttpStatus code, Types type, String desc) { | ||
this.code = code; | ||
this.type = type; | ||
this.desc = desc; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name() + " of " + type + COLON + desc; | ||
} | ||
|
||
@Getter | ||
public enum Types { | ||
|
||
SUCCESS("Successfully received and processed"), | ||
|
||
SDK_CONFIG_ERR("The Meta SDK config in EventMeshAdmin application.yml error"), | ||
|
||
META_COM_ERR("Network communication to Meta error"), | ||
; | ||
|
||
/** | ||
* Helpful for understanding and not used for now | ||
*/ | ||
private final String desc; | ||
|
||
Types(String desc) { | ||
this.desc = desc; | ||
} | ||
} | ||
} |
Oops, something went wrong.