diff --git a/response/error.go b/response/error.go index 1f86b68..15368ea 100644 --- a/response/error.go +++ b/response/error.go @@ -27,18 +27,15 @@ type APIError struct { // Error returns a string representation of the APIError, making it compatible with the error interface. func (e *APIError) Error() string { - // Attempt to marshal the APIError instance into a JSON string. data, err := json.Marshal(e) if err == nil { return string(data) } - // Use the standard HTTP status text as the error message if 'Message' field is empty. if e.Message == "" { e.Message = http.StatusText(e.StatusCode) } - // Fallback to a simpler error message format if JSON marshaling fails. return fmt.Sprintf("API Error: StatusCode=%d, Message=%s", e.StatusCode, e.Message) } @@ -179,7 +176,6 @@ func parseHTMLResponse(bodyBytes []byte, apiError *APIError) { parse(doc) - // Concatenate all accumulated messages and links with a separator. if len(messages) > 0 { apiError.Message = strings.Join(messages, "; ") } else { diff --git a/response/parse.go b/response/parse.go index a5704db..b772241 100644 --- a/response/parse.go +++ b/response/parse.go @@ -6,11 +6,11 @@ import "strings" // parseHeader generalizes the parsing of headers like Content-Type and Content-Disposition. // It extracts the main value (e.g., MIME type for Content-Type) and any parameters (like charset). func parseHeader(header string) (string, map[string]string) { - parts := strings.SplitN(header, ";", 2) // Split into main value and parameters + parts := strings.SplitN(header, ";", 2) mainValue := strings.TrimSpace(parts[0]) params := make(map[string]string) - if len(parts) > 1 { // Check if there are parameters + if len(parts) > 1 { for _, part := range strings.Split(parts[1], ";") { kv := strings.SplitN(part, "=", 2) if len(kv) == 2 { diff --git a/response/success.go b/response/success.go index 06355e3..8ff60bb 100644 --- a/response/success.go +++ b/response/success.go @@ -32,20 +32,16 @@ func HandleAPISuccessResponse(resp *http.Response, out interface{}, sugar *zap.S return handleDeleteRequest(resp, sugar) } - // Read the response body into a buffer bodyBytes, err := io.ReadAll(resp.Body) if err != nil { sugar.Error("Failed to read response body", zap.Error(err)) return err } - // After reading, reset resp.Body so it can be read again. sugar.Debug("HTTP Response Headers", zap.Any("Headers", resp.Header)) sugar.Debug("Raw HTTP Response", zap.String("Body", string(bodyBytes))) - // Use the buffer to create a new io.Reader for unmarshalling bodyReader := bytes.NewReader(bodyBytes) - mimeType, _ := parseHeader(resp.Header.Get("Content-Type")) contentDisposition := resp.Header.Get("Content-Disposition")