From 3e0f7cf5df3d57d4d1da55eab640a7b9cfc77de6 Mon Sep 17 00:00:00 2001 From: appleboy Date: Sat, 8 Mar 2025 20:57:02 +0800 Subject: [PATCH] fix: enhance error handling and reporting for API responses - Replace direct reading of response body with error handling for reading - Add JSON unmarshalling of the response body for better error reporting - Improve error message format to include details from the JSON response Signed-off-by: appleboy --- plugin.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/plugin.go b/plugin.go index 2d1a9cb..fd47a49 100644 --- a/plugin.go +++ b/plugin.go @@ -322,9 +322,15 @@ func (p *Plugin) SendMessage(ctx context.Context) error { defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent { - bodyBytes, _ := io.ReadAll(resp.Body) - bodyString := string(bodyBytes) - return fmt.Errorf("failed to send message, status code: %d, response: %s", resp.StatusCode, bodyString) + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("failed to read response body: %w", err) + } + var jsonResponse map[string]interface{} + if err := json.Unmarshal(bodyBytes, &jsonResponse); err != nil { + return fmt.Errorf("failed to unmarshal response body: %w", err) + } + return fmt.Errorf("failed to send message, status code: %d, error: %s, code: %v", resp.StatusCode, jsonResponse["message"], jsonResponse["code"]) } return nil