@@ -112,7 +112,11 @@ func DownloadFile(filepath string, url string) error {
112112 if err != nil {
113113 return err
114114 }
115- defer resp .Body .Close ()
115+ defer func () {
116+ if cerr := resp .Body .Close (); cerr != nil {
117+ log .Printf ("error closing response body from %s: %v" , url , cerr )
118+ }
119+ }()
116120
117121 if resp .StatusCode != 200 {
118122 return fmt .Errorf ("failed to get the file %d status code for %s file" , resp .StatusCode , url )
@@ -123,7 +127,11 @@ func DownloadFile(filepath string, url string) error {
123127 if err != nil {
124128 return err
125129 }
126- defer out .Close ()
130+ defer func () {
131+ if cerr := out .Close (); cerr != nil {
132+ log .Printf ("error closing file %s: %v" , filepath , cerr )
133+ }
134+ }()
127135
128136 // Write the body to file
129137 _ , err = io .Copy (out , resp .Body )
@@ -311,16 +319,17 @@ func WriteToFile(path string, content string) error {
311319 if err != nil {
312320 return ErrCreateFile (err , path )
313321 }
322+ defer func () {
323+ if cerr := file .Close (); cerr != nil {
324+ log .Printf ("error closing file %s: %v" , path , cerr )
325+ }
326+ }()
314327
315328 _ , err = file .WriteString (content )
316329 if err != nil {
317330 return ErrWriteFile (err , path )
318331 }
319- // Close the file to save the changes.
320- err = file .Close ()
321- if err != nil {
322- return ErrWriteFile (err , path )
323- }
332+
324333 return nil
325334}
326335
@@ -371,12 +380,19 @@ func WriteYamlToFile[K any](outputPath string, data K) error {
371380 if err != nil {
372381 return ErrCreateFile (err , outputPath )
373382 }
374- defer file .Close ()
383+ defer func () {
384+ if cerr := file .Close (); cerr != nil {
385+ log .Printf ("error closing YAML file %s: %v" , outputPath , cerr )
386+ }
387+ }()
375388
376389 encoder := yaml .NewEncoder (file )
377390 encoder .SetIndent (2 )
378-
379- defer encoder .Close ()
391+ defer func () {
392+ if cerr := encoder .Close (); cerr != nil {
393+ log .Printf ("error closing YAML encoder for file %s: %v" , outputPath , cerr )
394+ }
395+ }()
380396
381397 if err := encoder .Encode (data ); err != nil {
382398 return ErrMarshal (err )
@@ -561,9 +577,17 @@ func ReadSVGData(baseDir, path string) (string, error) {
561577}
562578func Compress (src string , buf io.Writer ) error {
563579 zr := gzip .NewWriter (buf )
564- defer zr .Close ()
580+ defer func () {
581+ if cerr := zr .Close (); cerr != nil {
582+ log .Printf ("error closing gzip writer: %v" , cerr )
583+ }
584+ }()
565585 tw := tar .NewWriter (zr )
566- defer tw .Close ()
586+ defer func () {
587+ if cerr := tw .Close (); cerr != nil {
588+ log .Printf ("error closing tar writer: %v" , cerr )
589+ }
590+ }()
567591
568592 return filepath .Walk (src , func (file string , fi os.FileInfo , err error ) error {
569593 if err != nil {
@@ -590,7 +614,11 @@ func Compress(src string, buf io.Writer) error {
590614 if err != nil {
591615 return err
592616 }
593- defer data .Close ()
617+ defer func () {
618+ if cerr := data .Close (); cerr != nil {
619+ log .Printf ("error closing file %s: %v" , file , cerr )
620+ }
621+ }()
594622
595623 _ , err = io .Copy (tw , data )
596624 if err != nil {
@@ -893,4 +921,4 @@ func TruncateErrorMessage(err error, wordLimit int) error {
893921 }
894922
895923 return err
896- }
924+ }
0 commit comments