You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by du...@apache.org on 2022/08/02 03:13:16 UTC
[rocketmq-schema-registry] branch main updated: make exception handler return error code and message with json
This is an automated email from the ASF dual-hosted git repository.
duhengforever pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/rocketmq-schema-registry.git
The following commit(s) were added to refs/heads/main by this push:
new 7c9f619 make exception handler return error code and message with json
new f26155c Merge pull request #21 from ferrirW/make-exception-return-json
7c9f619 is described below
commit 7c9f619e849f4c72fa432cd6281f1f8a7aea3156
Author: wangfan <18...@qq.com>
AuthorDate: Mon Aug 1 22:03:25 2022 +0800
make exception handler return error code and message with json
---
README.md | 2 +-
.../client/exceptions/RestClientException.java | 11 +++--
.../client/serializer/AbstractAvroSerializer.java | 6 ---
.../schema/registry/client/util/HttpUtil.java | 29 ++++++++----
.../schema/registry/common/utils/ErrorMessage.java | 52 ++++++++++++++++++++++
.../core/expection/RequestExceptionHandler.java | 17 ++++---
6 files changed, 89 insertions(+), 28 deletions(-)
diff --git a/README.md b/README.md
index 5200e67..b57a2cb 100644
--- a/README.md
+++ b/README.md
@@ -48,7 +48,7 @@ $ cd rocketmq-4.9.3/bin
# Start namesrv & broker
$ nohup sh mqnamesrv &
-$ nohup sh bin/mqbroker -n localhost:9876 &
+$ nohup sh mqbroker -n localhost:9876 &
```
#### Edit configuration (Optional)
diff --git a/client/src/main/java/org/apache/rocketmq/schema/registry/client/exceptions/RestClientException.java b/client/src/main/java/org/apache/rocketmq/schema/registry/client/exceptions/RestClientException.java
index a5061fe..ccf229f 100644
--- a/client/src/main/java/org/apache/rocketmq/schema/registry/client/exceptions/RestClientException.java
+++ b/client/src/main/java/org/apache/rocketmq/schema/registry/client/exceptions/RestClientException.java
@@ -18,21 +18,20 @@
package org.apache.rocketmq.schema.registry.client.exceptions;
public class RestClientException extends Exception {
+ private static final long serialVersionUID = 5169207805322753739L;
private final int status;
- private final int errorCode;
- public RestClientException(final String message, final int status, final int errorCode) {
- super(message + "; error code: " + errorCode);
+ public RestClientException(int status, String detailMessage) {
+ super(detailMessage);
this.status = status;
- this.errorCode = errorCode;
}
public int getStatus() {
return status;
}
- public int getErrorCode() {
- return errorCode;
+ public String getMessage() {
+ return super.getMessage();
}
}
\ No newline at end of file
diff --git a/client/src/main/java/org/apache/rocketmq/schema/registry/client/serializer/AbstractAvroSerializer.java b/client/src/main/java/org/apache/rocketmq/schema/registry/client/serializer/AbstractAvroSerializer.java
index a9550f0..520ecaa 100644
--- a/client/src/main/java/org/apache/rocketmq/schema/registry/client/serializer/AbstractAvroSerializer.java
+++ b/client/src/main/java/org/apache/rocketmq/schema/registry/client/serializer/AbstractAvroSerializer.java
@@ -49,12 +49,6 @@ public class AbstractAvroSerializer<T> {
return null;
}
- try {
-
- } catch (Exception e) {
- throw new SerializationException("get schema by subject failed", e);
- }
-
try {
GetSchemaResponse response = getSchemaBySubject(subject);
long schemaId = response.getSchemaId();
diff --git a/client/src/main/java/org/apache/rocketmq/schema/registry/client/util/HttpUtil.java b/client/src/main/java/org/apache/rocketmq/schema/registry/client/util/HttpUtil.java
index 2a452c2..c4f80b0 100644
--- a/client/src/main/java/org/apache/rocketmq/schema/registry/client/util/HttpUtil.java
+++ b/client/src/main/java/org/apache/rocketmq/schema/registry/client/util/HttpUtil.java
@@ -17,22 +17,25 @@
package org.apache.rocketmq.schema.registry.client.util;
+import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
-import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.apache.rocketmq.schema.registry.client.exceptions.RestClientException;
import org.apache.rocketmq.schema.registry.client.rest.JacksonMapper;
+import org.apache.rocketmq.schema.registry.common.utils.ErrorMessage;
public class HttpUtil {
public static ObjectMapper jsonParser = JacksonMapper.INSTANCE;
private static final int HTTP_CONNECT_TIMEOUT_MS = 30000;
private static final int HTTP_READ_TIMEOUT_MS = 30000;
- private static final int ERROR_CODE = 5001;
+ private static final int PARSE_ERROR_CODE = 5001;
public static <T> T sendHttpRequest(String requestUrl, String method, String requestBodyData,
Map<String, String> requestProperties,
@@ -47,8 +50,8 @@ public class HttpUtil {
if (requestBodyData != null) {
connection.setDoOutput(true);
- try (DataOutputStream os = new DataOutputStream(connection.getOutputStream())) {
- os.writeBytes(requestBodyData);
+ try (OutputStream os = connection.getOutputStream()) {
+ os.write(requestBodyData.getBytes(StandardCharsets.UTF_8));
os.flush();
} catch (IOException e) {
throw e;
@@ -56,16 +59,24 @@ public class HttpUtil {
}
int responseCode = connection.getResponseCode();
- if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {
+ if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream is = connection.getInputStream();
T result = jsonParser.readValue(is, responseFormat);
is.close();
return result;
- } else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
- return null;
} else {
- throw new RestClientException("send request failed", responseCode,
- ERROR_CODE);
+ ErrorMessage errorMessage;
+ try (InputStream es = connection.getErrorStream()) {
+ if (es != null) {
+ errorMessage = jsonParser.readValue(es, ErrorMessage.class);
+ } else {
+ errorMessage = new ErrorMessage(PARSE_ERROR_CODE, "request error");
+ }
+ } catch (JsonProcessingException e) {
+ errorMessage = new ErrorMessage(PARSE_ERROR_CODE, e.getMessage());
+ }
+
+ throw new RestClientException(errorMessage.getStatus(), errorMessage.getMessage());
}
} finally {
diff --git a/common/src/main/java/org/apache/rocketmq/schema/registry/common/utils/ErrorMessage.java b/common/src/main/java/org/apache/rocketmq/schema/registry/common/utils/ErrorMessage.java
new file mode 100644
index 0000000..8fe4361
--- /dev/null
+++ b/common/src/main/java/org/apache/rocketmq/schema/registry/common/utils/ErrorMessage.java
@@ -0,0 +1,52 @@
+/*
+ * 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.rocketmq.schema.registry.common.utils;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.NoArgsConstructor;
+import org.apache.rocketmq.schema.registry.common.exception.SchemaException;
+
+@Data
+@EqualsAndHashCode(callSuper = false)
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class ErrorMessage {
+
+ public int status;
+ public String message;
+
+ public ErrorMessage(SchemaException exception) {
+ this.status = exception.getErrorCode();
+ this.message = exception.getMessage();
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder sb = new StringBuilder("{");
+ sb.append("\"status\":")
+ .append(status);
+ sb.append(",\"message\":\"")
+ .append(message).append('\"');
+ sb.append('}');
+ return sb.toString();
+ }
+}
diff --git a/core/src/main/java/org/apache/rocketmq/schema/registry/core/expection/RequestExceptionHandler.java b/core/src/main/java/org/apache/rocketmq/schema/registry/core/expection/RequestExceptionHandler.java
index 9a59830..60dd653 100644
--- a/core/src/main/java/org/apache/rocketmq/schema/registry/core/expection/RequestExceptionHandler.java
+++ b/core/src/main/java/org/apache/rocketmq/schema/registry/core/expection/RequestExceptionHandler.java
@@ -21,10 +21,11 @@ import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.schema.registry.common.exception.SchemaException;
+import org.apache.rocketmq.schema.registry.common.utils.ErrorMessage;
+import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.RestControllerAdvice;
-@RestControllerAdvice
+@ControllerAdvice
@Slf4j
public class RequestExceptionHandler {
@@ -37,11 +38,15 @@ public class RequestExceptionHandler {
*/
@ExceptionHandler(SchemaException.class)
public void handleException(
- final HttpServletResponse response,
- final SchemaException e
+ final SchemaException e,
+ final HttpServletResponse response
) throws IOException {
- log.error("Global handle SchemaException: " + e.getMessage(), e);
- response.sendError(e.getErrorCode(), e.getMessage());
+ ErrorMessage errorMessage = new ErrorMessage(e);
+ log.error("Global handle SchemaException: " + errorMessage, e);
+ response.setContentType("application/json");
+ response.setStatus(e.getErrorCode());
+ response.setCharacterEncoding("UTF-8");
+ response.getWriter().write(errorMessage.toString());
}
}