diff --git a/go/appbuilder/util.go b/go/appbuilder/util.go index 26d6f2c2..67e1b627 100644 --- a/go/appbuilder/util.go +++ b/go/appbuilder/util.go @@ -19,6 +19,7 @@ import ( "errors" "fmt" "io" + "log" "net/http" ) @@ -29,16 +30,19 @@ type SSEEvent struct { } func checkHTTPResponse(rsp *http.Response) (string, error) { - requestID := rsp.Header.Get("X-Appbuilder-Request-Id") - if rsp.StatusCode == http.StatusOK { - return requestID, nil - } + requestID := rsp.Header.Get("X-Appbuilder-Request-Id") + if rsp.StatusCode == http.StatusOK { + log.Printf("Successful HTTP response. RequestID: %s", requestID) + return requestID, nil + } - data, err := io.ReadAll(rsp.Body) - if err != nil { - return requestID, err - } - return requestID, fmt.Errorf("http status code is %d, content is %s", rsp.StatusCode, string(data)) + data, err := io.ReadAll(rsp.Body) + if err != nil { + log.Printf("Failed to read response body. RequestID: %s, Error: %v", requestID, err) + return requestID, err + } + log.Printf("HTTP response with unexpected status code. RequestID: %s, StatusCode: %d, Content: %s", requestID, rsp.StatusCode, string(data)) + return requestID, fmt.Errorf("http status code is %d, content is %s", rsp.StatusCode, string(data)) } func NewSSEReader(bufSize int, reader *bufio.Reader) *sseReader { diff --git a/java/src/main/java/com/baidubce/appbuilder/base/utils/http/HttpClient.java b/java/src/main/java/com/baidubce/appbuilder/base/utils/http/HttpClient.java index 507473f4..d2e0b3c1 100644 --- a/java/src/main/java/com/baidubce/appbuilder/base/utils/http/HttpClient.java +++ b/java/src/main/java/com/baidubce/appbuilder/base/utils/http/HttpClient.java @@ -210,12 +210,24 @@ public HttpResponse execute(ClassicHttpRequest request, Type bodyType) HttpResponse response = new HttpResponse().setCode(resp.getCode()).setMessage(resp.getReasonPhrase()) .setRequestId(requestId).setHeaders(headers).setStringBody(stringBody); - if (resp.getCode() == 200 && bodyType != null) { - response.setBody(JsonUtils.deserialize(stringBody, bodyType)); + if (resp.getCode() == 200) { + LOGGER.log(Level.FINE, "Successful response with code 200 for request ID: {}", requestId); + if (bodyType != null) { + response.setBody(JsonUtils.deserialize(stringBody, bodyType)); + } + } else { + LOGGER.log(Level.SEVERE, "Error response with code {} for request ID: {}, message: {}", resp.getCode(), requestId, resp.getReasonPhrase()); } return response; }); if (httpResponse.getCode() != 200) { + String errorMessage = String.format( + "Error after processing response with code %d for request ID: %s, message: %s", + httpResponse.getCode(), + httpResponse.getRequestId(), + httpResponse.getMessage() + ); + LOGGER.log(Level.SEVERE, errorMessage); throw new AppBuilderServerException(httpResponse.getRequestId(), httpResponse.getCode(), httpResponse.getMessage(), httpResponse.getStringBody()); }