detached 97c8607

fix(cache): improve error handling and use standard io constants

commit
97c8607f82f119f00aabed0abd9e210ed672a18c
author
bamen · · 2025-09-20 05:21 UTC
parent
cbb68b6
signature
unsigned
2 files · +17 -6
M internal/cache/favicon.go +13 -3
@@ -106,7 +106,11 @@ func (fc *FaviconCache) downloadFavicon(faviconURL string) error {
106106 if err != nil {
107107 return fmt.Errorf("failed to download favicon: %w", err)
108108 }
109- defer resp.Body.Close()
109+ defer func() {
110+ if err := resp.Body.Close(); err != nil {
111+ log.Printf("[favicon] failed to close response body: %v", err)
112+ }
113+ }()
110114
111115 // Log redirects for debugging
112116 if resp.Request.URL.String() != faviconURL {
@@ -126,14 +130,20 @@ func (fc *FaviconCache) downloadFavicon(faviconURL string) error {
126130 if err != nil {
127131 return fmt.Errorf("failed to create download temp file: %w", err)
128132 }
129- defer downloadFile.Close()
133+ defer func() {
134+ if err := downloadFile.Close(); err != nil {
135+ log.Printf("[favicon] failed to close download file: %v", err)
136+ }
137+ }()
130138 defer os.Remove(tempDownloadFile)
131139
132140 // Save raw favicon data
133141 if _, err := io.Copy(downloadFile, limitedBody); err != nil {
134142 return fmt.Errorf("failed to save favicon data: %w", err)
135143 }
136- downloadFile.Close()
144+ if err := downloadFile.Close(); err != nil {
145+ log.Printf("[favicon] failed to close download file: %v", err)
146+ }
137147
138148 // Detect if it's an ICO file and convert with ffmpeg if needed
139149 if err := fc.convertToPNG(tempDownloadFile, cachedPath, faviconURL); err != nil {
@@ -106,7 +106,11 @@ func (fc *FaviconCache) downloadFavicon(faviconURL string) error {
106 if err != nil {106 if err != nil {
107 return fmt.Errorf("failed to download favicon: %w", err)107 return fmt.Errorf("failed to download favicon: %w", err)
108 }108 }
109- defer resp.Body.Close()109+ defer func() {
110+ if err := resp.Body.Close(); err != nil {
111+ log.Printf("[favicon] failed to close response body: %v", err)
112+ }
113+ }()
110 114
111 // Log redirects for debugging115 // Log redirects for debugging
112 if resp.Request.URL.String() != faviconURL {116 if resp.Request.URL.String() != faviconURL {
@@ -126,14 +130,20 @@ func (fc *FaviconCache) downloadFavicon(faviconURL string) error {
126 if err != nil {130 if err != nil {
127 return fmt.Errorf("failed to create download temp file: %w", err)131 return fmt.Errorf("failed to create download temp file: %w", err)
128 }132 }
129- defer downloadFile.Close()133+ defer func() {
134+ if err := downloadFile.Close(); err != nil {
135+ log.Printf("[favicon] failed to close download file: %v", err)
136+ }
137+ }()
130 defer os.Remove(tempDownloadFile)138 defer os.Remove(tempDownloadFile)
131 139
132 // Save raw favicon data140 // Save raw favicon data
133 if _, err := io.Copy(downloadFile, limitedBody); err != nil {141 if _, err := io.Copy(downloadFile, limitedBody); err != nil {
134 return fmt.Errorf("failed to save favicon data: %w", err)142 return fmt.Errorf("failed to save favicon data: %w", err)
135 }143 }
136- downloadFile.Close()144+ if err := downloadFile.Close(); err != nil {
145+ log.Printf("[favicon] failed to close download file: %v", err)
146+ }
137 147
138 // Detect if it's an ICO file and convert with ffmpeg if needed148 // Detect if it's an ICO file and convert with ffmpeg if needed
139 if err := fc.convertToPNG(tempDownloadFile, cachedPath, faviconURL); err != nil {149 if err := fc.convertToPNG(tempDownloadFile, cachedPath, faviconURL); err != nil {
M internal/cache/filter_cache.go +4 -3
@@ -3,6 +3,7 @@ package cache
33 import (
44 "encoding/binary"
55 "fmt"
6+ "io"
67 "os"
78 "syscall"
89 "time"
@@ -233,7 +234,7 @@ func (fc *FilterCache) Write(data *FilterData) error {
233234 return fmt.Errorf("failed to write header: %w", err)
234235 }
235236
236- dataStart, _ := file.Seek(0, os.SEEK_CUR)
237+ dataStart, _ := file.Seek(0, io.SeekCurrent)
237238
238239 // Write network rules
239240 for _, rule := range data.NetworkRules {
@@ -252,11 +253,11 @@ func (fc *FilterCache) Write(data *FilterData) error {
252253 }
253254
254255 // Calculate data size
255- dataEnd, _ := file.Seek(0, os.SEEK_CUR)
256+ dataEnd, _ := file.Seek(0, io.SeekCurrent)
256257 dataSize := uint64(dataEnd - dataStart)
257258
258259 // Update header with data size
259- if _, err := file.Seek(0, os.SEEK_SET); err != nil {
260+ if _, err := file.Seek(0, io.SeekStart); err != nil {
260261 os.Remove(tmpPath)
261262 return err
262263 }
@@ -3,6 +3,7 @@ package cache
3 import (3 import (
4 "encoding/binary"4 "encoding/binary"
5 "fmt"5 "fmt"
6+ "io"
6 "os"7 "os"
7 "syscall"8 "syscall"
8 "time"9 "time"
@@ -233,7 +234,7 @@ func (fc *FilterCache) Write(data *FilterData) error {
233 return fmt.Errorf("failed to write header: %w", err)234 return fmt.Errorf("failed to write header: %w", err)
234 }235 }
235 236
236- dataStart, _ := file.Seek(0, os.SEEK_CUR)237+ dataStart, _ := file.Seek(0, io.SeekCurrent)
237 238
238 // Write network rules239 // Write network rules
239 for _, rule := range data.NetworkRules {240 for _, rule := range data.NetworkRules {
@@ -252,11 +253,11 @@ func (fc *FilterCache) Write(data *FilterData) error {
252 }253 }
253 254
254 // Calculate data size255 // Calculate data size
255- dataEnd, _ := file.Seek(0, os.SEEK_CUR)256+ dataEnd, _ := file.Seek(0, io.SeekCurrent)
256 dataSize := uint64(dataEnd - dataStart)257 dataSize := uint64(dataEnd - dataStart)
257 258
258 // Update header with data size259 // Update header with data size
259- if _, err := file.Seek(0, os.SEEK_SET); err != nil {260+ if _, err := file.Seek(0, io.SeekStart); err != nil {
260 os.Remove(tmpPath)261 os.Remove(tmpPath)
261 return err262 return err
262 }263 }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 8

Tags 42

↑↓ moveenter openesc close