Skip to content

Commit c923456

Browse files
toppercodesampagent
andcommitted
tokens: treat wrapped 404 errors as missing resources
Amp-Thread-ID: https://ampcode.com/threads/T-019cb11d-d0b4-724b-938e-a9eb7f44c002 Co-authored-by: Amp <amp@ampcode.com>
1 parent 2df38e7 commit c923456

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

axiom/utils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,20 @@ func convertAttribute(resourceAttribute resourceschema.Attribute) datasourcesche
160160
}
161161

162162
func isNotFoundError(err error) bool {
163+
if errors.Is(err, axiom.ErrNotFound) {
164+
return true
165+
}
166+
167+
// Keep backward compatibility for callers that may wrap concrete HTTPError values.
163168
var apiError axiom.HTTPError
164169
if errors.As(err, &apiError) {
165170
return apiError.Status == 404
166171
}
172+
173+
var apiErrorPtr *axiom.HTTPError
174+
if errors.As(err, &apiErrorPtr) && apiErrorPtr != nil {
175+
return apiErrorPtr.Status == 404
176+
}
177+
167178
return false
168179
}

axiom/utils_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package axiom
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"testing"
7+
8+
ax "github.com/axiomhq/axiom-go/axiom"
9+
)
10+
11+
func TestIsNotFoundError(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
err error
15+
want bool
16+
}{
17+
{
18+
name: "axiom ErrNotFound",
19+
err: ax.ErrNotFound,
20+
want: true,
21+
},
22+
{
23+
name: "wrapped value HTTPError 404",
24+
err: fmt.Errorf("wrapped: %w", ax.HTTPError{Status: 404, Message: "Not Found"}),
25+
want: true,
26+
},
27+
{
28+
name: "wrapped pointer HTTPError 404",
29+
err: fmt.Errorf("wrapped: %w", &ax.HTTPError{Status: 404, Message: "Not Found"}),
30+
want: true,
31+
},
32+
{
33+
name: "other status",
34+
err: ax.HTTPError{Status: 500, Message: "Internal Server Error"},
35+
want: false,
36+
},
37+
{
38+
name: "generic error",
39+
err: errors.New("boom"),
40+
want: false,
41+
},
42+
}
43+
44+
for _, tt := range tests {
45+
t.Run(tt.name, func(t *testing.T) {
46+
got := isNotFoundError(tt.err)
47+
if got != tt.want {
48+
t.Fatalf("isNotFoundError() = %v, want %v", got, tt.want)
49+
}
50+
})
51+
}
52+
}

0 commit comments

Comments
 (0)