|
4 | 4 | "bytes" |
5 | 5 | "context" |
6 | 6 | "encoding/json" |
| 7 | + "fmt" |
7 | 8 | "io" |
8 | 9 | "net" |
9 | 10 | "net/http" |
@@ -51,6 +52,12 @@ func (c *RawClient) Get(ctx context.Context, endpoint string, v any) error { |
51 | 52 | } |
52 | 53 | defer response.Body.Close() |
53 | 54 |
|
| 55 | + // Check for HTTP errors |
| 56 | + if response.StatusCode >= 400 { |
| 57 | + buf, _ := io.ReadAll(response.Body) |
| 58 | + return fmt.Errorf("HTTP %d: %s", response.StatusCode, string(buf)) |
| 59 | + } |
| 60 | + |
54 | 61 | buf, err := io.ReadAll(response.Body) |
55 | 62 | if err != nil { |
56 | 63 | return err |
@@ -90,6 +97,12 @@ func (c *RawClient) Post(ctx context.Context, endpoint string, v any, result any |
90 | 97 | } |
91 | 98 | defer response.Body.Close() |
92 | 99 |
|
| 100 | + // Check for HTTP errors |
| 101 | + if response.StatusCode >= 400 { |
| 102 | + buf, _ := io.ReadAll(response.Body) |
| 103 | + return fmt.Errorf("HTTP %d: %s", response.StatusCode, string(buf)) |
| 104 | + } |
| 105 | + |
93 | 106 | if result == nil { |
94 | 107 | _, err := io.Copy(io.Discard, response.Body) |
95 | 108 | return err |
@@ -123,6 +136,12 @@ func (c *RawClient) Delete(ctx context.Context, endpoint string) error { |
123 | 136 | } |
124 | 137 | defer response.Body.Close() |
125 | 138 |
|
| 139 | + // Check for HTTP errors |
| 140 | + if response.StatusCode >= 400 { |
| 141 | + buf, _ := io.ReadAll(response.Body) |
| 142 | + return fmt.Errorf("HTTP %d: %s", response.StatusCode, string(buf)) |
| 143 | + } |
| 144 | + |
126 | 145 | _, err = io.Copy(io.Discard, response.Body) |
127 | 146 | return err |
128 | 147 | } |
0 commit comments