Skip to content

Commit

Permalink
新增go\java日志处理
Browse files Browse the repository at this point in the history
  • Loading branch information
yinjiaqi authored and yinjiaqi committed Dec 30, 2024
1 parent b32b1eb commit ae35a02
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
22 changes: 13 additions & 9 deletions go/appbuilder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
)

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,24 @@ public <T> HttpResponse<T> execute(ClassicHttpRequest request, Type bodyType)
HttpResponse<T> response =
new HttpResponse<T>().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());
}
Expand Down

0 comments on commit ae35a02

Please sign in to comment.