From fbe418453218d1b2b4c06f2d2753114197dfd152 Mon Sep 17 00:00:00 2001 From: RiceChuan Date: Sat, 21 Dec 2024 18:56:17 +0800 Subject: [PATCH] chore: suggest using errors.New instead of fmt.Errorf with no parameters Signed-off-by: RiceChuan --- exp/services/market-tracker/price.go | 11 ++++++----- exp/services/market-tracker/volume.go | 9 +++++---- utils/apiclient/client.go | 4 ++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/exp/services/market-tracker/price.go b/exp/services/market-tracker/price.go index 780e5d8f5d..90b02e2ff0 100644 --- a/exp/services/market-tracker/price.go +++ b/exp/services/market-tracker/price.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "errors" "fmt" "io/ioutil" "log" @@ -117,7 +118,7 @@ func parseStellarExpertPriceHistory(body string) ([]xlmPrice, error) { // with the most recent timestamp and price first. We split that array to get strings of only "timestamp,price". // We then split each of those strings and define a struct containing the timestamp and price. if len(body) < 5 { - return []xlmPrice{}, fmt.Errorf("got ill-formed response body from stellar expert") + return []xlmPrice{}, errors.New("got ill-formed response body from stellar expert") } body = body[2 : len(body)-2] @@ -127,7 +128,7 @@ func parseStellarExpertPriceHistory(body string) ([]xlmPrice, error) { for _, timePriceStr := range timePriceStrs { timePrice := strings.Split(timePriceStr, ",") if len(timePrice) != 2 { - return []xlmPrice{}, fmt.Errorf("got ill-formed time/price from stellar expert") + return []xlmPrice{}, errors.New("got ill-formed time/price from stellar expert") } ts, err := strconv.ParseInt(timePrice[0], 10, 64) @@ -157,12 +158,12 @@ func parseStellarExpertLatestPrice(body string) (float64, error) { // We format and return the most recent price. lists := strings.Split(body, ",") if len(lists) < 2 { - return 0.0, fmt.Errorf("mis-formed response from stellar expert") + return 0.0, errors.New("mis-formed response from stellar expert") } rawPriceStr := lists[1] if len(rawPriceStr) < 2 { - return 0.0, fmt.Errorf("mis-formed price from stellar expert") + return 0.0, errors.New("mis-formed price from stellar expert") } priceStr := rawPriceStr[:len(rawPriceStr)-1] @@ -227,7 +228,7 @@ func getAssetUSDPrice(body, currency string) (float64, error) { rates := make(map[string]interface{}) var ok bool if rates, ok = m["rates"].(map[string]interface{}); !ok { - return 0.0, fmt.Errorf("could not get rates from api response") + return 0.0, errors.New("could not get rates from api response") } var rate float64 diff --git a/exp/services/market-tracker/volume.go b/exp/services/market-tracker/volume.go index df4367a27d..d6b853e31e 100644 --- a/exp/services/market-tracker/volume.go +++ b/exp/services/market-tracker/volume.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "sort" "strconv" @@ -205,7 +206,7 @@ func getAggRecords(taps []hProtocol.TradeAggregationsPage) (records []hProtocol. func constructVolumeHistory(tas []hProtocol.TradeAggregation, xlmPrices []xlmPrice, assetPrice float64, start, end time.Time, res int) ([]volumeHist, error) { if len(xlmPrices) < 2 { - return []volumeHist{}, fmt.Errorf("mis-formed xlm price history from stellar expert") + return []volumeHist{}, errors.New("mis-formed xlm price history from stellar expert") } volumeHistory := []volumeHist{} @@ -414,18 +415,18 @@ func calcWeightedPrice(timestamp int64, startIndex int, prices []xlmPrice) (floa // TODO: Use resolution to weight prices. if startIndex < 0 { if timestamp < prices[0].timestamp { - return 0.0, fmt.Errorf("update price index before calculating price") + return 0.0, errors.New("update price index before calculating price") } return prices[0].price, nil } else if startIndex >= len(prices)-1 { if timestamp > prices[len(prices)-1].timestamp { - return 0.0, fmt.Errorf("update price index before calculating price") + return 0.0, errors.New("update price index before calculating price") } return prices[len(prices)-1].price, nil } if timestamp > prices[startIndex].timestamp || timestamp < prices[startIndex+1].timestamp { - return 0.0, fmt.Errorf("update price index before calculating price") + return 0.0, errors.New("update price index before calculating price") } avgPrice := (prices[startIndex].price + prices[startIndex+1].price) / 2 diff --git a/utils/apiclient/client.go b/utils/apiclient/client.go index 82501df3fc..069788db61 100644 --- a/utils/apiclient/client.go +++ b/utils/apiclient/client.go @@ -42,13 +42,13 @@ func (c *APIClient) CallAPI(reqParams RequestParams) (interface{}, error) { } if reqParams.Endpoint == "" { - return nil, fmt.Errorf("Please set endpoint to query") + return nil, errors.New("Please set endpoint to query") } url := c.GetURL(reqParams.Endpoint, reqParams.QueryParams) reqBody, err := CreateRequestBody(reqParams.RequestType, url) if err != nil { - return nil, fmt.Errorf("http request creation failed") + return nil, errors.New("http request creation failed") } SetAuthHeaders(reqBody, c.AuthType, c.AuthHeaders)