A continuation of HomeBox the inventory and organization system built for the Home User
ProductBarcode: backend: fix resource leak with defer
Crumb Owlf72fcb080021
diff --git a/backend/app/api/handlers/v1/v1_ctrl_product_search.go b/backend/app/api/handlers/v1/v1_ctrl_product_search.goindex 6e5c3978..db7caca4 100644--- a/backend/app/api/handlers/v1/v1_ctrl_product_search.go+++ b/backend/app/api/handlers/v1/v1_ctrl_product_search.go@@ -192,7 +192,6 @@ func (ctrl *V1Controller) HandleProductSearchFromBarcode(conf config.BarcodeAPICif err != nil {return nil, err}- defer resp.Body.Close()if resp.StatusCode != http.StatusOK {return nil, fmt.Errorf("barcodespider API returned status code: %d", resp.StatusCode)@@ -204,6 +203,8 @@ func (ctrl *V1Controller) HandleProductSearchFromBarcode(conf config.BarcodeAPICreturn nil, err}+ resp.Body.Close()+// Convert the body to type stringsb := string(body)log.Info().Msg("Response: " + sb)@@ -259,15 +260,15 @@ func (ctrl *V1Controller) HandleProductSearchFromBarcode(conf config.BarcodeAPIClog.Warn().Msg("Cannot fetch image for URL: " + p.ImageURL + ": " + err.Error())}- defer res.Body.Close()-// Validate responseif res.StatusCode != http.StatusOK {+ res.Body.Close()continue}// Read data of imagebytes, err := io.ReadAll(res.Body)+ res.Body.Close()if err != nil {log.Warn().Msg(err.Error())continue