From 3a255c0c64f858d5c05797aba9a6d159b0c7d82f Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 8 Mar 2026 08:38:05 +0200 Subject: fix(task-373): handle runtime cleanup errors in production paths --- internal/image/download.go | 10 +++++++--- internal/image/openai.go | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'internal/image') diff --git a/internal/image/download.go b/internal/image/download.go index 8ea897e..b2af843 100644 --- a/internal/image/download.go +++ b/internal/image/download.go @@ -46,8 +46,8 @@ func NewDownloader(searcher ImageSearcher, options *DownloadOptions) *Downloader } } -// DownloadImage downloads a single image to the specified path -func (d *Downloader) DownloadImage(ctx context.Context, result *SearchResult, outputPath string) error { +// DownloadImage downloads a single image to the specified path. +func (d *Downloader) DownloadImage(ctx context.Context, result *SearchResult, outputPath string) (err error) { // Ensure directory exists dir := filepath.Dir(outputPath) if dir != "" && dir != "." { @@ -77,7 +77,11 @@ func (d *Downloader) DownloadImage(ctx context.Context, result *SearchResult, ou if err != nil { return fmt.Errorf("create output file %q: %w", outputPath, err) } - defer file.Close() + defer func() { + if closeErr := file.Close(); err == nil && closeErr != nil { + err = fmt.Errorf("close output file %q: %w", outputPath, closeErr) + } + }() // Copy with size limit if specified var written int64 diff --git a/internal/image/openai.go b/internal/image/openai.go index 5bb7db4..f2db41d 100644 --- a/internal/image/openai.go +++ b/internal/image/openai.go @@ -197,7 +197,9 @@ func (c *OpenAIClient) Download(ctx context.Context, url string) (io.ReadCloser, } if resp.StatusCode != http.StatusOK { - resp.Body.Close() + if closeErr := resp.Body.Close(); closeErr != nil { + return nil, fmt.Errorf("HTTP %d: %s (failed to close response body: %v)", resp.StatusCode, resp.Status, closeErr) + } return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, resp.Status) } -- cgit v1.2.3