Skip to content

Commit 4f3c9a9

Browse files
committed
fix: handle 0 exchange rates incase of json response are not correct.
1 parent 06d5c48 commit 4f3c9a9

File tree

5 files changed

+16
-1
lines changed

5 files changed

+16
-1
lines changed

pkg/exchanger/1forge.go

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ func (c *oneForgeApi) Latest(from string, to string, opt ...interface{}) error {
9494
value := json.GetPath(`value`).
9595
MustFloat64()
9696
// todo handle error
97+
if value <= 0 {
98+
return fmt.Errorf(`error in retrieving exhcange rate is 0`)
99+
}
97100
c.rateValue = value
98101
c.rateDate = time.Now()
99102
return nil

pkg/exchanger/currencylayer.go

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ func (c *currencyLayerApi) Latest(from string, to string, opt ...interface{}) er
9494
value := json.GetPath(`result`).
9595
MustFloat64()
9696
// todo handle error
97+
if value <= 0 {
98+
return fmt.Errorf(`error in retrieving exhcange rate is 0`)
99+
}
97100
c.rateValue = value
98101
c.rateDate = time.Now()
99102
return nil

pkg/exchanger/fixer.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ func (c *fixerApi) GetExchangerName() string {
7171
// Latest ... populate latest exchange rate
7272
func (c *fixerApi) Latest(from string, to string, opt ...interface{}) error {
7373

74-
// todo cache layer
7574
_, err := c.requestRate(from, to, opt)
7675
if err != nil {
7776
log.Print(err)
@@ -95,7 +94,11 @@ func (c *fixerApi) Latest(from string, to string, opt ...interface{}) error {
9594
value := json.GetPath(`result`).
9695
MustFloat64()
9796
// todo handle error
97+
if value <= 0 {
98+
return fmt.Errorf(`error in retrieving exhcange rate is 0`)
99+
}
98100
c.rateValue = value
101+
c.rateDate = time.Now()
99102
return nil
100103
}
101104

pkg/exchanger/openexchangerates.go

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ func (c *openExchangeRatesApi) Latest(from string, to string, opt ...interface{}
9494
value := json.GetPath(`response`).
9595
MustFloat64()
9696
// todo handle error
97+
if value <= 0 {
98+
return fmt.Errorf(`error in retrieving exhcange rate is 0`)
99+
}
97100
c.rateValue = value
98101
c.rateDate = time.Now()
99102
return nil

pkg/exchanger/yahoo.go

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func (c *yahooApi) Latest(from string, to string, opt ...interface{}) error {
9898
GetIndex(0).
9999
MustFloat64()
100100
// todo handle error
101+
if value <= 0 {
102+
return fmt.Errorf(`error in retrieving exhcange rate is 0`)
103+
}
101104
c.rateValue = math.Round(value*1000000) / 1000000
102105
c.rateDate = time.Now()
103106
return nil

0 commit comments

Comments
 (0)