Skip to content

Commit 744db25

Browse files
committed
modelerrors: include HTTP status code in StatusError.Error()
StatusError.Error() previously delegated directly to the wrapped error, discarding the status code. This made debugging harder since log lines wouldn't reveal the HTTP status. Now Error() returns a message like "HTTP 429: rate limit exceeded" for better observability. Assisted-By: docker-agent
1 parent 4fd44ed commit 744db25

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

pkg/modelerrors/modelerrors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package modelerrors
77
import (
88
"context"
99
"errors"
10+
"fmt"
1011
"log/slog"
1112
"net"
1213
"net/http"
@@ -38,7 +39,7 @@ type StatusError struct {
3839
}
3940

4041
func (e *StatusError) Error() string {
41-
return e.Err.Error()
42+
return fmt.Sprintf("HTTP %d: %s", e.StatusCode, e.Err.Error())
4243
}
4344

4445
func (e *StatusError) Unwrap() error {

pkg/modelerrors/modelerrors_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,11 @@ func TestParseRetryAfterHeader(t *testing.T) {
265265
func TestStatusError(t *testing.T) {
266266
t.Parallel()
267267

268-
t.Run("Error() delegates to wrapped error", func(t *testing.T) {
268+
t.Run("Error() includes status code and wrapped message", func(t *testing.T) {
269269
t.Parallel()
270270
underlying := errors.New("rate limit exceeded")
271271
se := &StatusError{StatusCode: 429, Err: underlying}
272-
assert.Equal(t, underlying.Error(), se.Error())
272+
assert.Equal(t, "HTTP 429: rate limit exceeded", se.Error())
273273
})
274274

275275
t.Run("Unwrap() allows errors.Is traversal", func(t *testing.T) {
@@ -315,7 +315,7 @@ func TestWrapHTTPError(t *testing.T) {
315315
require.ErrorAs(t, result, &se)
316316
assert.Equal(t, 429, se.StatusCode)
317317
assert.Equal(t, time.Duration(0), se.RetryAfter)
318-
assert.Equal(t, origErr.Error(), se.Error())
318+
assert.Equal(t, "HTTP 429: rate limited", se.Error())
319319
})
320320

321321
t.Run("429 with Retry-After header sets RetryAfter", func(t *testing.T) {

0 commit comments

Comments
 (0)