From 4b1ce77f238db7f97577c2a4da832f3bf4a6fc52 Mon Sep 17 00:00:00 2001 From: David D'Amario Date: Tue, 12 Dec 2023 16:42:34 -0500 Subject: [PATCH] added error handling to file.close() fns --- pkg/dia/scraper/exchange-scrapers/BitstampScraper.go | 7 ++++++- pkg/dia/scraper/exchange-scrapers/MEXCScraper.go | 6 +++++- pkg/dia/service/assetservice/source/osmosis.go | 6 +++++- pkg/utils/datasource.go | 7 ++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/dia/scraper/exchange-scrapers/BitstampScraper.go b/pkg/dia/scraper/exchange-scrapers/BitstampScraper.go index 31467b6d8..51268cb3a 100644 --- a/pkg/dia/scraper/exchange-scrapers/BitstampScraper.go +++ b/pkg/dia/scraper/exchange-scrapers/BitstampScraper.go @@ -239,7 +239,12 @@ func (s *BitstampScraper) FetchAvailablePairs() (pairs []dia.ExchangePair, err e log.Error("Get Pairs:", err) } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + log.Warn("Error closing file: ", err) + } + }() + body, err := io.ReadAll(resp.Body) if err != nil { log.Error("Read pair body:", err) diff --git a/pkg/dia/scraper/exchange-scrapers/MEXCScraper.go b/pkg/dia/scraper/exchange-scrapers/MEXCScraper.go index e8b3fab08..1a950b104 100644 --- a/pkg/dia/scraper/exchange-scrapers/MEXCScraper.go +++ b/pkg/dia/scraper/exchange-scrapers/MEXCScraper.go @@ -267,7 +267,11 @@ func (s *MEXCScraper) FetchAvailablePairs() (pairs []dia.ExchangePair, err error log.Error("get symbols: ", err) } - defer response.Body.Close() + defer func() { + if err := response.Body.Close(); err != nil { + log.Warn("Error closing file: ", err) + } + }() body, err := ioutil.ReadAll(response.Body) diff --git a/pkg/dia/service/assetservice/source/osmosis.go b/pkg/dia/service/assetservice/source/osmosis.go index 2fe795b07..a33230fde 100644 --- a/pkg/dia/service/assetservice/source/osmosis.go +++ b/pkg/dia/service/assetservice/source/osmosis.go @@ -238,7 +238,11 @@ func GetAssetsJson() (map[string]*OsmosisAsset, error) { if err != nil { return nil, err } - defer res.Body.Close() + defer func() { + if err := res.Body.Close(); err != nil { + log.Warn("Error closing file: ", err) + } + }() data, err := ioutil.ReadAll(res.Body) if err != nil { return nil, err diff --git a/pkg/utils/datasource.go b/pkg/utils/datasource.go index cb1313448..6ab79e29f 100644 --- a/pkg/utils/datasource.go +++ b/pkg/utils/datasource.go @@ -32,7 +32,12 @@ func (cg *Coingecko) Price(assetName string) (float64, error) { return 0.0, err } - defer response.Body.Close() + defer func() { + if err := response.Body.Close(); err != nil { + log.Warn("Error closing file: ", err) + } + }() + if 200 != response.StatusCode { return 0.0, fmt.Errorf("Error on coingecko API call with return code %d", response.StatusCode) }