Skip to content

Commit 8581d46

Browse files
authored
Merge pull request #1142 from mattn/fix/handle-io-readall-errors
fix: handle ignored io.ReadAll errors across codebase
2 parents 651cb2e + 03d6ad4 commit 8581d46

9 files changed

Lines changed: 82 additions & 18 deletions

File tree

cmd/picoclaw-launcher-tui/internal/ui/model.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,11 @@ func (s *appState) testModel(model *picoclawconfig.ModelConfig) {
335335
s.showMessage("Test OK", resp.Status)
336336
return
337337
}
338-
body, _ := io.ReadAll(io.LimitReader(resp.Body, 2048))
338+
body, err := io.ReadAll(io.LimitReader(resp.Body, 2048))
339+
if err != nil {
340+
s.showMessage("Test failed", fmt.Sprintf("failed to read response: %v", err))
341+
return
342+
}
339343
s.showMessage(
340344
"Test failed",
341345
fmt.Sprintf("%s: %s", resp.Status, strings.TrimSpace(string(body))),

cmd/picoclaw-launcher/internal/server/auth_handlers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,10 @@ func fetchGoogleUserEmail(accessToken string) (string, error) {
297297
}
298298
defer resp.Body.Close()
299299

300-
body, _ := io.ReadAll(resp.Body)
300+
body, err := io.ReadAll(resp.Body)
301+
if err != nil {
302+
return "", fmt.Errorf("reading userinfo response: %w", err)
303+
}
301304
if resp.StatusCode != http.StatusOK {
302305
return "", fmt.Errorf("userinfo request failed: %s", string(body))
303306
}

cmd/picoclaw/internal/auth/helpers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ func fetchGoogleUserEmail(accessToken string) (string, error) {
177177
}
178178
defer resp.Body.Close()
179179

180-
body, _ := io.ReadAll(resp.Body)
180+
body, err := io.ReadAll(resp.Body)
181+
if err != nil {
182+
return "", fmt.Errorf("reading userinfo response: %w", err)
183+
}
181184
if resp.StatusCode != http.StatusOK {
182185
return "", fmt.Errorf("userinfo request failed: %s", string(body))
183186
}

pkg/auth/oauth.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,10 @@ func RequestDeviceCode(cfg OAuthProviderConfig) (*DeviceCodeInfo, error) {
212212
}
213213
defer resp.Body.Close()
214214

215-
body, _ := io.ReadAll(resp.Body)
215+
body, err := io.ReadAll(resp.Body)
216+
if err != nil {
217+
return nil, fmt.Errorf("reading device code response: %w", err)
218+
}
216219
if resp.StatusCode != http.StatusOK {
217220
return nil, fmt.Errorf("device code request failed: %s", string(body))
218221
}
@@ -300,7 +303,10 @@ func LoginDeviceCode(cfg OAuthProviderConfig) (*AuthCredential, error) {
300303
}
301304
defer resp.Body.Close()
302305

303-
body, _ := io.ReadAll(resp.Body)
306+
body, err := io.ReadAll(resp.Body)
307+
if err != nil {
308+
return nil, fmt.Errorf("reading device code response: %w", err)
309+
}
304310
if resp.StatusCode != http.StatusOK {
305311
return nil, fmt.Errorf("device code request failed: %s", string(body))
306312
}
@@ -360,7 +366,10 @@ func pollDeviceCode(cfg OAuthProviderConfig, deviceAuthID, userCode string) (*Au
360366
return nil, fmt.Errorf("pending")
361367
}
362368

363-
body, _ := io.ReadAll(resp.Body)
369+
body, err := io.ReadAll(resp.Body)
370+
if err != nil {
371+
return nil, fmt.Errorf("reading device token response: %w", err)
372+
}
364373

365374
var tokenResp struct {
366375
AuthorizationCode string `json:"authorization_code"`
@@ -401,7 +410,10 @@ func RefreshAccessToken(cred *AuthCredential, cfg OAuthProviderConfig) (*AuthCre
401410
}
402411
defer resp.Body.Close()
403412

404-
body, _ := io.ReadAll(resp.Body)
413+
body, err := io.ReadAll(resp.Body)
414+
if err != nil {
415+
return nil, fmt.Errorf("reading token refresh response: %w", err)
416+
}
405417
if resp.StatusCode != http.StatusOK {
406418
return nil, fmt.Errorf("token refresh failed: %s", string(body))
407419
}
@@ -494,7 +506,10 @@ func ExchangeCodeForTokens(cfg OAuthProviderConfig, code, codeVerifier, redirect
494506
}
495507
defer resp.Body.Close()
496508

497-
body, _ := io.ReadAll(resp.Body)
509+
body, err := io.ReadAll(resp.Body)
510+
if err != nil {
511+
return nil, fmt.Errorf("reading token exchange response: %w", err)
512+
}
498513
if resp.StatusCode != http.StatusOK {
499514
return nil, fmt.Errorf("token exchange failed: %s", string(body))
500515
}

pkg/channels/line/line.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,10 @@ func (c *LINEChannel) callAPI(ctx context.Context, endpoint string, payload any)
654654
defer resp.Body.Close()
655655

656656
if resp.StatusCode != http.StatusOK {
657-
respBody, _ := io.ReadAll(resp.Body)
657+
respBody, err := io.ReadAll(resp.Body)
658+
if err != nil {
659+
return channels.ClassifySendError(resp.StatusCode, fmt.Errorf("reading LINE API error response: %w", err))
660+
}
658661
return channels.ClassifySendError(resp.StatusCode, fmt.Errorf("LINE API error: %s", string(respBody)))
659662
}
660663

pkg/channels/wecom/aibot.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,10 @@ func (c *WeComAIBotChannel) sendViaResponseURL(responseURL, content string) erro
793793
return nil
794794
}
795795

796-
respBody, _ := io.ReadAll(resp.Body)
796+
respBody, err := io.ReadAll(resp.Body)
797+
if err != nil {
798+
return fmt.Errorf("reading response_url body: %w: %w", channels.ErrTemporary, err)
799+
}
797800
switch {
798801
case resp.StatusCode == http.StatusTooManyRequests:
799802
return fmt.Errorf("response_url rate limited (%d): %s: %w",

pkg/channels/wecom/app.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,17 @@ func (c *WeComAppChannel) uploadMedia(ctx context.Context, accessToken, mediaTyp
321321
defer resp.Body.Close()
322322

323323
if resp.StatusCode != http.StatusOK {
324-
respBody, _ := io.ReadAll(resp.Body)
325-
return "", channels.ClassifySendError(resp.StatusCode, fmt.Errorf("wecom upload error: %s", string(respBody)))
324+
respBody, readErr := io.ReadAll(resp.Body)
325+
if readErr != nil {
326+
return "", channels.ClassifySendError(
327+
resp.StatusCode,
328+
fmt.Errorf("reading wecom upload error response: %w", readErr),
329+
)
330+
}
331+
return "", channels.ClassifySendError(
332+
resp.StatusCode,
333+
fmt.Errorf("wecom upload error: %s", string(respBody)),
334+
)
326335
}
327336

328337
var result struct {
@@ -371,8 +380,17 @@ func (c *WeComAppChannel) sendWeComMessage(ctx context.Context, accessToken stri
371380
defer resp.Body.Close()
372381

373382
if resp.StatusCode != http.StatusOK {
374-
respBody, _ := io.ReadAll(resp.Body)
375-
return channels.ClassifySendError(resp.StatusCode, fmt.Errorf("wecom_app API error: %s", string(respBody)))
383+
respBody, readErr := io.ReadAll(resp.Body)
384+
if readErr != nil {
385+
return channels.ClassifySendError(
386+
resp.StatusCode,
387+
fmt.Errorf("reading wecom_app error response: %w", readErr),
388+
)
389+
}
390+
return channels.ClassifySendError(
391+
resp.StatusCode,
392+
fmt.Errorf("wecom_app API error: %s", string(respBody)),
393+
)
376394
}
377395

378396
respBody, err := io.ReadAll(resp.Body)

pkg/channels/wecom/bot.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,17 @@ func (c *WeComBotChannel) sendWebhookReply(ctx context.Context, userID, content
453453
defer resp.Body.Close()
454454

455455
if resp.StatusCode != http.StatusOK {
456-
body, _ := io.ReadAll(resp.Body)
457-
return channels.ClassifySendError(resp.StatusCode, fmt.Errorf("webhook API error: %s", string(body)))
456+
body, readErr := io.ReadAll(resp.Body)
457+
if readErr != nil {
458+
return channels.ClassifySendError(
459+
resp.StatusCode,
460+
fmt.Errorf("reading webhook error response: %w", readErr),
461+
)
462+
}
463+
return channels.ClassifySendError(
464+
resp.StatusCode,
465+
fmt.Errorf("webhook API error: %s", string(body)),
466+
)
458467
}
459468

460469
body, err := io.ReadAll(resp.Body)

pkg/providers/antigravity_provider.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,10 @@ func FetchAntigravityProjectID(accessToken string) (string, error) {
640640
}
641641
defer resp.Body.Close()
642642

643-
body, _ := io.ReadAll(resp.Body)
643+
body, err := io.ReadAll(resp.Body)
644+
if err != nil {
645+
return "", fmt.Errorf("reading loadCodeAssist response: %w", err)
646+
}
644647
if resp.StatusCode != http.StatusOK {
645648
return "", fmt.Errorf("loadCodeAssist failed: %s", string(body))
646649
}
@@ -681,7 +684,10 @@ func FetchAntigravityModels(accessToken, projectID string) ([]AntigravityModelIn
681684
}
682685
defer resp.Body.Close()
683686

684-
body, _ := io.ReadAll(resp.Body)
687+
body, err := io.ReadAll(resp.Body)
688+
if err != nil {
689+
return nil, fmt.Errorf("reading fetchAvailableModels response: %w", err)
690+
}
685691
if resp.StatusCode != http.StatusOK {
686692
return nil, fmt.Errorf(
687693
"fetchAvailableModels failed (HTTP %d): %s",

0 commit comments

Comments
 (0)