diff --git a/larksuite-oapi/pom.xml b/larksuite-oapi/pom.xml index 7c0d40662..9ac27116d 100644 --- a/larksuite-oapi/pom.xml +++ b/larksuite-oapi/pom.xml @@ -60,7 +60,7 @@ maven-surefire-plugin - true + ${skipTests} org.apache.maven.plugins ${maven-surefire-plugin.version} @@ -245,6 +245,7 @@ 2.6 3.0.1 2.12.4 + true 2.7 UTF-8 10-robolectric-5803371 diff --git a/larksuite-oapi/src/main/java/com/lark/oapi/core/utils/UnmarshalRespUtil.java b/larksuite-oapi/src/main/java/com/lark/oapi/core/utils/UnmarshalRespUtil.java index b9c6d0fc4..d9191c824 100644 --- a/larksuite-oapi/src/main/java/com/lark/oapi/core/utils/UnmarshalRespUtil.java +++ b/larksuite-oapi/src/main/java/com/lark/oapi/core/utils/UnmarshalRespUtil.java @@ -13,13 +13,36 @@ package com.lark.oapi.core.utils; import com.lark.oapi.core.response.RawResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.nio.charset.StandardCharsets; public class UnmarshalRespUtil { + private static final Logger log = LoggerFactory.getLogger(UnmarshalRespUtil.class); + private static final int MAX_LOG_BODY_LENGTH = 500; + public static T unmarshalResp(RawResponse resp, Class respClass) { - return Jsons.DEFAULT.fromJson(new String(resp.getBody(), StandardCharsets.UTF_8), - respClass); + String bodyStr = new String(resp.getBody(), StandardCharsets.UTF_8); + int statusCode = resp.getStatusCode(); + if (statusCode < 200 || statusCode >= 300) { + String truncatedBody = truncate(bodyStr); + log.warn("unexpected response status, statusCode={}, body={}", statusCode, truncatedBody); + } + try { + return Jsons.DEFAULT.fromJson(bodyStr, respClass); + } catch (Exception e) { + String truncatedBody = truncate(bodyStr); + log.error("unmarshal response error, statusCode={}, body={}", statusCode, truncatedBody); + throw e; + } + } + + private static String truncate(String body) { + if (body.length() > MAX_LOG_BODY_LENGTH) { + return body.substring(0, MAX_LOG_BODY_LENGTH) + "...(truncated)"; + } + return body; } } diff --git a/larksuite-oapi/src/test/java/com/lark/oapi/core/utils/TestUnmarshalRespUtil.java b/larksuite-oapi/src/test/java/com/lark/oapi/core/utils/TestUnmarshalRespUtil.java new file mode 100644 index 000000000..35ebd2142 --- /dev/null +++ b/larksuite-oapi/src/test/java/com/lark/oapi/core/utils/TestUnmarshalRespUtil.java @@ -0,0 +1,97 @@ +/* + * MIT License + * + * Copyright (c) 2022 Lark Technologies Pte. Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice, shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.lark.oapi.core.utils; + +import com.google.gson.JsonSyntaxException; +import com.google.gson.annotations.SerializedName; +import com.lark.oapi.core.response.RawResponse; +import org.junit.Test; + +import java.nio.charset.StandardCharsets; + +public class TestUnmarshalRespUtil { + + public static class SimpleResp { + @SerializedName("code") + private int code; + @SerializedName("msg") + private String msg; + + public int getCode() { return code; } + public String getMsg() { return msg; } + } + + @Test + public void testUnmarshalSuccess() { + RawResponse raw = new RawResponse(); + raw.setStatusCode(200); + raw.setBody("{\"code\":0,\"msg\":\"ok\"}".getBytes(StandardCharsets.UTF_8)); + + SimpleResp resp = UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class); + assert resp != null; + assert resp.getCode() == 0; + assert "ok".equals(resp.getMsg()); + } + + @Test(expected = JsonSyntaxException.class) + public void testUnmarshalInvalidJson() { + RawResponse raw = new RawResponse(); + raw.setStatusCode(502); + raw.setBody("Bad Gateway".getBytes(StandardCharsets.UTF_8)); + + UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class); + } + + @Test(expected = JsonSyntaxException.class) + public void testUnmarshalStringInsteadOfObject() { + RawResponse raw = new RawResponse(); + raw.setStatusCode(200); + raw.setBody("\"some error message\"".getBytes(StandardCharsets.UTF_8)); + + UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class); + } + + @Test(expected = JsonSyntaxException.class) + public void testUnmarshalHtmlResponse() { + RawResponse raw = new RawResponse(); + raw.setStatusCode(403); + raw.setBody("Forbidden".getBytes(StandardCharsets.UTF_8)); + + UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class); + } + + @Test(expected = JsonSyntaxException.class) + public void testUnmarshalLongBodyIsTruncatedInLog() { + RawResponse raw = new RawResponse(); + raw.setStatusCode(500); + StringBuilder sb = new StringBuilder("\""); + for (int i = 0; i < 600; i++) { + sb.append("x"); + } + sb.append("\""); + raw.setBody(sb.toString().getBytes(StandardCharsets.UTF_8)); + + UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class); + } + + @Test + public void testUnmarshalNon2xxWithValidJson() { + RawResponse raw = new RawResponse(); + raw.setStatusCode(400); + raw.setBody("{\"code\":99991400,\"msg\":\"param error\"}".getBytes(StandardCharsets.UTF_8)); + + SimpleResp resp = UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class); + assert resp != null; + assert resp.getCode() == 99991400; + } +}