[优化] 代码优化

This commit is contained in:
ryan
2026-05-31 20:09:40 +08:00
parent 81ba309650
commit edb356f40e
6 changed files with 42 additions and 56 deletions
@@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"errors"
"io"
"log/slog"
"net/http"
"strings"
@@ -107,7 +108,12 @@ func (c *Client) do(req *http.Request, target any) error {
slog.Error("http request failed", "method", req.Method, "path", req.URL.Path, "error", err)
return err
}
defer res.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
slog.Error("failed to close response body", "error", err)
}
}(res.Body)
if res.StatusCode != http.StatusOK {
slog.Warn("http request returned non-200", "method", req.Method, "path", req.URL.Path, "status", res.Status)
return errors.New(res.Status)