Skip to content

Commit

Permalink
restful response optimize part1
Browse files Browse the repository at this point in the history
  • Loading branch information
Pil0tXia committed Oct 17, 2023
1 parent f629730 commit e03e6b4
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package org.apache.eventmesh.admin.controller;

import org.apache.eventmesh.admin.dto.SubscriptionResponse;
import org.apache.eventmesh.admin.dto.Result;
import org.apache.eventmesh.admin.exception.EventMeshAdminException;
import org.apache.eventmesh.admin.model.SubscriptionInfo;
import org.apache.eventmesh.admin.service.SubscriptionService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -68,15 +71,15 @@ public ResponseEntity<String> retrieveSubscription(@RequestParam("dataId") Strin
* @return config properties and base64 encoded config content
*/
@GetMapping("/subscriptions")
public ResponseEntity<SubscriptionResponse> listSubscriptions(
public ResponseEntity<Result<List<SubscriptionInfo>>> listSubscriptions(
@RequestParam(name = "page", defaultValue = "1") Integer page,
@RequestParam(name = "size", defaultValue = "10") Integer size,
@RequestParam(name = "dataId", defaultValue = CLIENT_DATA_ID_PATTERN) String dataId,
@RequestParam(name = "group", defaultValue = "") String group) {
try {
return ResponseEntity.ok(subscriptionService.retrieveConfigs(page, size, dataId, group));
} catch (EventMeshAdminException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new SubscriptionResponse(e.getMessage()));
return Result.ok(subscriptionService.retrieveConfigs(page, size, dataId, group));
} catch (RuntimeException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Result<>(e.getMessage()));
}
}

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 org.apache.eventmesh.admin.model.SubscriptionInfo;
import static org.apache.eventmesh.admin.enums.ErrorType.SUCCESS;

import java.util.List;
import org.springframework.http.ResponseEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -30,15 +30,38 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SubscriptionResponse {
public class Result<T> {

private List<SubscriptionInfo> subscriptionInfos;
private T data;

private Integer pages;

private String message;

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

public Result(T data, Integer pages) {
this.data = data;
this.pages = pages;
}

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

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

public static <T> ResponseEntity<Result<T>> ok() {
return ResponseEntity.ok(new Result<>(SUCCESS.getDesc()));
}

public static <T> ResponseEntity<Result<T>> ok(Result<T> result) {
result.setMessage(SUCCESS.getDesc());
return ResponseEntity.ok(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 lombok.Getter;

@Getter
public enum ErrorType {

SUCCESS("SUCCESS", "success"),

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

NACOS_GET_CONFIG_ERR("META_COM_ERR", "Failed to retrieve Nacos config(s)."),

NACOS_EMPTY_RESP_ERR("META_COM_ERR", "No result returned by Nacos. Please check Nacos."),

NACOS_LOGIN_ERR("META_COM_ERR", "Nacos login failed."),

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

// error type
private final String type;

// error message
private final String desc;

ErrorType(String type, String desc) {
this.type = type;
this.desc = desc;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

package org.apache.eventmesh.admin.exception;

import org.apache.eventmesh.admin.enums.ErrorType;
import org.apache.eventmesh.admin.utils.ExceptionUtils;

/**
* EventMeshAdmin Application side exception
*/
public class EventMeshAdminException extends RuntimeException {

private static final long serialVersionUID = 2002022502005456586L;
Expand All @@ -29,11 +35,10 @@ public EventMeshAdminException(String message, Throwable cause) {
super(message, cause);
}

public EventMeshAdminException(Throwable cause) {
super(cause);
}

public EventMeshAdminException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
/**
* Customized error reporting with exception
*/
public EventMeshAdminException(ErrorType errorType, Throwable cause) {
super(ExceptionUtils.trimDesc(errorType.getDesc()) + ": " + cause.getMessage(), cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package org.apache.eventmesh.admin.exception;

public class EventMeshException extends EventMeshAdminException {
/**
* EventMesh Runtime side exception
*/
public class EventMeshException extends RuntimeException {

private static final long serialVersionUID = 5648256502005456586L;

Expand All @@ -28,16 +31,4 @@ public EventMeshException(String message) {
public EventMeshException(String message, Throwable cause) {
super(message, cause);
}

public EventMeshException(Throwable cause) {
super(cause);
}

public EventMeshException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

public EventMeshException(Integer errCode, String errMsg) {
super(String.format("errorCode: %s, errorMessage: %s", errCode, errMsg));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@

package org.apache.eventmesh.admin.exception;

public class MetaException extends EventMeshAdminException {
import org.apache.eventmesh.admin.enums.ErrorType;
import org.apache.eventmesh.admin.utils.ExceptionUtils;

/**
* Meta side exception with EventMeshAdmin Application
*/
public class MetaException extends RuntimeException {

private static final long serialVersionUID = 6246145526338359773L;

Expand All @@ -28,4 +34,11 @@ public MetaException(String message) {
public MetaException(String message, Throwable cause) {
super(message, cause);
}

/**
* Customized error reporting with exception
*/
public MetaException(ErrorType errorType, Throwable cause) {
super(ExceptionUtils.trimDesc(errorType.getDesc()) + ": " + cause.getMessage(), cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import static org.apache.eventmesh.admin.config.Constants.NACOS_CONFIGS_API;
import static org.apache.eventmesh.admin.config.Constants.NACOS_LOGIN_API;
import static org.apache.eventmesh.admin.enums.ErrorType.NACOS_EMPTY_RESP_ERR;
import static org.apache.eventmesh.admin.enums.ErrorType.NACOS_GET_CONFIG_ERR;
import static org.apache.eventmesh.admin.enums.ErrorType.NACOS_LOGIN_EMPTY_RESP_ERR;
import static org.apache.eventmesh.admin.enums.ErrorType.NACOS_SDK_CONFIG_ERR;

import org.apache.eventmesh.admin.config.AdminProperties;
import org.apache.eventmesh.admin.config.Constants;
Expand Down Expand Up @@ -97,14 +101,14 @@ public String retrieveConfig(String dataId, String group) {
try {
configService = NacosFactory.createConfigService(nacosProps);
} catch (Exception e) {
log.error("Failed to create Nacos ConfigService", e);
throw new EventMeshAdminException("Failed to create Nacos ConfigService: " + e.getMessage());
log.error(NACOS_SDK_CONFIG_ERR.getDesc(), e);
throw new EventMeshAdminException(NACOS_SDK_CONFIG_ERR, e);
}
try {
return configService.getConfig(dataId, group, adminProperties.getConfig().getTimeoutMs());
} catch (Exception e) {
log.error("Failed to retrieve Nacos config", e);
throw new MetaException("Failed to retrieve Nacos config: " + e.getMessage());
log.error(NACOS_GET_CONFIG_ERR.getDesc(), e);
throw new MetaException(NACOS_GET_CONFIG_ERR, e);
}
}

Expand All @@ -129,12 +133,12 @@ public SubscriptionResponse retrieveConfigs(Integer page, Integer size, String d
try {
response = restTemplate.getForEntity(urlBuilder.toUriString(), String.class);
} catch (Exception e) {
log.error("Failed to retrieve Nacos config list.", e);
throw new MetaException("Failed to retrieve Nacos config list: " + e.getMessage());
log.error(NACOS_GET_CONFIG_ERR.getDesc(), e);
throw new MetaException(NACOS_GET_CONFIG_ERR, e);
}
if (response.getBody() == null || response.getBody().isEmpty()) {
log.error("No result returned by Nacos. Please check Nacos.");
throw new MetaException("No result returned by Nacos. Please check Nacos.");
log.error(NACOS_EMPTY_RESP_ERR.getDesc());
throw new MetaException(NACOS_EMPTY_RESP_ERR.getDesc());
}

return toSubscriptionResponse(JSON.parseObject(response.getBody()));
Expand Down Expand Up @@ -188,8 +192,8 @@ private String loginGetAccessToken() {
throw new MetaException("Nacos login failed: " + e.getMessage());
}
if (loginResponse.getBody() == null || loginResponse.getBody().isEmpty()) {
log.error("Nacos didn't return accessToken. Please check Nacos status. Status code: {}", loginResponse.getStatusCode());
throw new MetaException("Nacos didn't return accessToken. Please check Nacos status. Status code: " + loginResponse.getStatusCode());
log.error(NACOS_LOGIN_EMPTY_RESP_ERR + " Status code: {}", loginResponse.getStatusCode());
throw new MetaException(NACOS_LOGIN_EMPTY_RESP_ERR + " Status code: " + loginResponse.getStatusCode());
}
return JSON.parseObject(loginResponse.getBody()).getString("accessToken");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.utils;

public class ExceptionUtils {

/**
* Remove the last period of exception description.
*/
public static String trimDesc(String desc) {
if (desc == null) {
return "";
}
if (desc.charAt(desc.length() - 1) == '.') {
return desc.substring(0, desc.length() - 1);
}
return desc;
}

}

0 comments on commit e03e6b4

Please sign in to comment.