Skip to content

Commit 1225f28

Browse files
fix: resolve errcheck lint violations in api package
1 parent c1317ba commit 1225f28

7 files changed

Lines changed: 43 additions & 27 deletions

File tree

internal/cmd/api/api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestNewCmd_RequestFlags(t *testing.T) {
4343
func TestNewCmd_GETRequest(t *testing.T) {
4444
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
4545
if r.URL.Path == "/openapi.json" {
46-
json.NewEncoder(w).Encode(map[string]any{"openapi": "3.1.0", "paths": map[string]any{}})
46+
_ = json.NewEncoder(w).Encode(map[string]any{"openapi": "3.1.0", "paths": map[string]any{}})
4747
return
4848
}
4949
if r.Method != "GET" {
@@ -74,7 +74,7 @@ func TestNewCmd_GETRequest(t *testing.T) {
7474
func TestNewCmd_POSTWithBody(t *testing.T) {
7575
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7676
if r.URL.Path == "/openapi.json" {
77-
json.NewEncoder(w).Encode(map[string]any{"openapi": "3.1.0", "paths": map[string]any{}})
77+
_ = json.NewEncoder(w).Encode(map[string]any{"openapi": "3.1.0", "paths": map[string]any{}})
7878
return
7979
}
8080
if r.Method != "POST" {

internal/cmd/api/info_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func newDetailedSpecServer(t *testing.T) *httptest.Server {
6969
}
7070
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7171
if r.URL.Path == "/openapi.json" {
72-
json.NewEncoder(w).Encode(spec)
72+
_ = json.NewEncoder(w).Encode(spec)
7373
return
7474
}
7575
http.NotFound(w, r)
@@ -133,7 +133,9 @@ func TestInfoCmd_Shorthand(t *testing.T) {
133133
}
134134

135135
var detail EndpointDetail
136-
json.Unmarshal(out.Bytes(), &detail)
136+
if err := json.Unmarshal(out.Bytes(), &detail); err != nil {
137+
t.Fatal(err)
138+
}
137139
if detail.Path != "/api/v1/sessions" {
138140
t.Errorf("expected resolved path /api/v1/sessions, got %q", detail.Path)
139141
}
@@ -159,7 +161,9 @@ func TestInfoCmd_WithRequestBody(t *testing.T) {
159161
}
160162

161163
var detail EndpointDetail
162-
json.Unmarshal(out.Bytes(), &detail)
164+
if err := json.Unmarshal(out.Bytes(), &detail); err != nil {
165+
t.Fatal(err)
166+
}
163167
if detail.RequestBody == nil {
164168
t.Fatal("expected request_body to be non-nil")
165169
}

internal/cmd/api/ls_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func newTestSpecServer(t *testing.T) *httptest.Server {
2929
}
3030
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3131
if r.URL.Path == "/openapi.json" {
32-
json.NewEncoder(w).Encode(spec)
32+
_ = json.NewEncoder(w).Encode(spec)
3333
return
3434
}
3535
http.NotFound(w, r)
@@ -84,7 +84,9 @@ func TestLsCmd_FilterByTag(t *testing.T) {
8484
}
8585

8686
var endpoints []Endpoint
87-
json.Unmarshal(out.Bytes(), &endpoints)
87+
if err := json.Unmarshal(out.Bytes(), &endpoints); err != nil {
88+
t.Fatal(err)
89+
}
8890
if len(endpoints) != 2 {
8991
t.Errorf("expected 2 dataset endpoints, got %d", len(endpoints))
9092
}
@@ -115,7 +117,9 @@ func TestLsCmd_Search(t *testing.T) {
115117
}
116118

117119
var endpoints []Endpoint
118-
json.Unmarshal(out.Bytes(), &endpoints)
120+
if err := json.Unmarshal(out.Bytes(), &endpoints); err != nil {
121+
t.Fatal(err)
122+
}
119123
if len(endpoints) != 1 {
120124
t.Errorf("expected 1 match for 'query', got %d", len(endpoints))
121125
}

internal/cmd/api/request.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ func runRequest(apiURL, apiKey, method, path, body string, headers []string, inc
6868
if json.Indent(&prettyBuf, respBody, "", " ") == nil {
6969
fmt.Fprintln(w, prettyBuf.String())
7070
} else {
71-
w.Write(respBody)
71+
if _, err := w.Write(respBody); err != nil {
72+
return statusCode, fmt.Errorf("writing response: %w", err)
73+
}
7274
fmt.Fprintln(w)
7375
}
7476

internal/cmd/api/request_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestRunRequest_POSTWithBody(t *testing.T) {
4747
}
4848
body, _ := io.ReadAll(r.Body)
4949
var data map[string]any
50-
json.Unmarshal(body, &data)
50+
_ = json.Unmarshal(body, &data)
5151
if data["name"] != "test" {
5252
t.Errorf("expected name=test, got %v", data["name"])
5353
}
@@ -128,12 +128,12 @@ func TestRunRequest_BodyFromFile(t *testing.T) {
128128
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
129129
body, _ := io.ReadAll(r.Body)
130130
w.WriteHeader(200)
131-
w.Write(body)
131+
_, _ = w.Write(body)
132132
}))
133133
defer ts.Close()
134134

135135
f, _ := os.CreateTemp(t.TempDir(), "body-*.json")
136-
f.WriteString(`{"from":"file"}`)
136+
_, _ = f.WriteString(`{"from":"file"}`)
137137
f.Close()
138138

139139
var out bytes.Buffer

internal/cmd/api/spec.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (s *OpenAPISpec) Endpoints() []Endpoint {
6565
Summary string `json:"summary"`
6666
Tags []string `json:"tags"`
6767
}
68-
json.Unmarshal(raw, &detail)
68+
_ = json.Unmarshal(raw, &detail)
6969
tag := ""
7070
if len(detail.Tags) > 0 {
7171
tag = detail.Tags[0]
@@ -114,7 +114,7 @@ func (s *OpenAPISpec) LookupEndpoint(method, path string) (*EndpointDetail, erro
114114
RequestBody json.RawMessage `json:"requestBody"`
115115
Responses json.RawMessage `json:"responses"`
116116
}
117-
json.Unmarshal(raw, &parsed)
117+
_ = json.Unmarshal(raw, &parsed)
118118

119119
tag := ""
120120
if len(parsed.Tags) > 0 {
@@ -133,7 +133,7 @@ func (s *OpenAPISpec) LookupEndpoint(method, path string) (*EndpointDetail, erro
133133
Type string `json:"type"`
134134
} `json:"schema"`
135135
}
136-
json.Unmarshal(pRaw, &p)
136+
_ = json.Unmarshal(pRaw, &p)
137137
params = append(params, Parameter{
138138
Name: p.Name,
139139
In: p.In,
@@ -219,14 +219,14 @@ func (s *OpenAPISpec) resolveRef(raw json.RawMessage) any {
219219
var obj map[string]json.RawMessage
220220
if err := json.Unmarshal(raw, &obj); err != nil {
221221
var result any
222-
json.Unmarshal(raw, &result)
222+
_ = json.Unmarshal(raw, &result)
223223
return result
224224
}
225225

226226
// Check for $ref
227227
if refRaw, ok := obj["$ref"]; ok {
228228
var ref string
229-
json.Unmarshal(refRaw, &ref)
229+
_ = json.Unmarshal(refRaw, &ref)
230230
resolved := s.resolveComponentRef(ref)
231231
if resolved != nil {
232232
return resolved
@@ -235,7 +235,7 @@ func (s *OpenAPISpec) resolveRef(raw json.RawMessage) any {
235235

236236
// Otherwise return as generic map
237237
var result any
238-
json.Unmarshal(raw, &result)
238+
_ = json.Unmarshal(raw, &result)
239239
return result
240240
}
241241

@@ -263,7 +263,7 @@ func (s *OpenAPISpec) resolveComponentRef(ref string) any {
263263

264264
// Parse one level — don't recurse into nested $refs
265265
var schema any
266-
json.Unmarshal(schemaRaw, &schema)
266+
_ = json.Unmarshal(schemaRaw, &schema)
267267
return schema
268268
}
269269

@@ -299,9 +299,13 @@ func loadSpec(apiURL, cacheDir string, forceRefresh bool) (*OpenAPISpec, error)
299299
return nil, fmt.Errorf("parsing OpenAPI spec: %w", err)
300300
}
301301

302-
// Write cache
303-
os.MkdirAll(filepath.Dir(cachePath), 0755)
304-
os.WriteFile(cachePath, data, 0644)
302+
// Write cache (best-effort; ignore errors so a read-only cache dir doesn't break the command)
303+
if err := os.MkdirAll(filepath.Dir(cachePath), 0755); err != nil {
304+
return &spec, nil
305+
}
306+
if err := os.WriteFile(cachePath, data, 0644); err != nil {
307+
return &spec, nil
308+
}
305309

306310
return &spec, nil
307311
}

internal/cmd/api/spec_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestLoadSpec_FromServer(t *testing.T) {
3838
http.NotFound(w, r)
3939
return
4040
}
41-
json.NewEncoder(w).Encode(spec)
41+
_ = json.NewEncoder(w).Encode(spec)
4242
}))
4343
defer ts.Close()
4444

@@ -59,7 +59,7 @@ func TestLoadSpec_UsesCache(t *testing.T) {
5959
callCount := 0
6060
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6161
callCount++
62-
json.NewEncoder(w).Encode(map[string]any{
62+
_ = json.NewEncoder(w).Encode(map[string]any{
6363
"openapi": "3.1.0",
6464
"paths": map[string]any{},
6565
})
@@ -89,7 +89,7 @@ func TestLoadSpec_RefreshBypassesCache(t *testing.T) {
8989
callCount := 0
9090
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
9191
callCount++
92-
json.NewEncoder(w).Encode(map[string]any{
92+
_ = json.NewEncoder(w).Encode(map[string]any{
9393
"openapi": "3.1.0",
9494
"paths": map[string]any{},
9595
})
@@ -113,7 +113,7 @@ func TestLoadSpec_ExpiredCache(t *testing.T) {
113113
callCount := 0
114114
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
115115
callCount++
116-
json.NewEncoder(w).Encode(map[string]any{
116+
_ = json.NewEncoder(w).Encode(map[string]any{
117117
"openapi": "3.1.0",
118118
"paths": map[string]any{},
119119
})
@@ -125,7 +125,9 @@ func TestLoadSpec_ExpiredCache(t *testing.T) {
125125
_, _ = loadSpec(ts.URL, cacheDir, false)
126126
cachePath := specCachePath(cacheDir, ts.URL)
127127
old := time.Now().Add(-25 * time.Hour)
128-
os.Chtimes(cachePath, old, old)
128+
if err := os.Chtimes(cachePath, old, old); err != nil {
129+
t.Fatal(err)
130+
}
129131

130132
_, _ = loadSpec(ts.URL, cacheDir, false)
131133
if callCount != 2 {

0 commit comments

Comments
 (0)