From 0a686647d989665bf5b5e30b28815e53c6d2d9d6 Mon Sep 17 00:00:00 2001 From: yndu13 Date: Mon, 22 Jan 2024 16:23:29 +0800 Subject: [PATCH] fix: unescape unicode in sdk error --- tea/tea.go | 7 +++++-- tea/tea_test.go | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tea/tea.go b/tea/tea.go index c984caf..3fc9b08 100644 --- a/tea/tea.go +++ b/tea/tea.go @@ -218,8 +218,11 @@ func NewSDKError(obj map[string]interface{}) *SDKError { } } } - byt, _ := json.Marshal(data) - err.Data = String(string(byt)) + byt := bytes.NewBuffer([]byte{}) + jsonEncoder := json.NewEncoder(byt) + jsonEncoder.SetEscapeHTML(false) + jsonEncoder.Encode(data) + err.Data = String(string(bytes.TrimSpace(byt.Bytes()))) } if statusCode, ok := obj["statusCode"].(int); ok { diff --git a/tea/tea_test.go b/tea/tea_test.go index 297456f..272a030 100644 --- a/tea/tea_test.go +++ b/tea/tea_test.go @@ -168,6 +168,7 @@ func TestSDKError(t *testing.T) { "httpCode": "404", "requestId": "dfadfa32cgfdcasd4313", "hostId": "github.com/alibabacloud/tea", + "recommend": "https://中文?q=a.b&product=c&requestId=123", }, "description": "description", "accessDeniedDetail": map[string]interface{}{ @@ -179,7 +180,7 @@ func TestSDKError(t *testing.T) { }, }) utils.AssertNotNil(t, err) - utils.AssertEqual(t, "SDKError:\n StatusCode: 404\n Code: code\n Message: message\n Data: {\"hostId\":\"github.com/alibabacloud/tea\",\"httpCode\":\"404\",\"requestId\":\"dfadfa32cgfdcasd4313\"}\n", err.Error()) + utils.AssertEqual(t, "SDKError:\n StatusCode: 404\n Code: code\n Message: message\n Data: {\"hostId\":\"github.com/alibabacloud/tea\",\"httpCode\":\"404\",\"recommend\":\"https://中文?q=a.b&product=c&requestId=123\",\"requestId\":\"dfadfa32cgfdcasd4313\"}\n", err.Error()) err.SetErrMsg("test") utils.AssertEqual(t, "test", err.Error())