summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsiddharth ravikumar <s@ricketyspace.net>2022-07-02 12:25:31 -0400
committersiddharth ravikumar <s@ricketyspace.net>2022-07-02 12:25:31 -0400
commit64dd223fcc24755956819bad3dcba3ad047b09e8 (patch)
treec741a9358b4471cd7e2ae29f66851cf5afcd088b
parent4d6b34a33a734a81168c3d7d2306b29a59fd0795 (diff)
nws: update `GetForecast`
Change return error type to `nws.Error`
-rw-r--r--nws/nws.go48
-rw-r--r--nws/nws_test.go6
2 files changed, 34 insertions, 20 deletions
diff --git a/nws/nws.go b/nws/nws.go
index 694f077..0e7fae2 100644
--- a/nws/nws.go
+++ b/nws/nws.go
@@ -123,15 +123,12 @@ func GetForecastBundle(lat, lng float32) (*ForecastBundle, *Error) {
if nwsErr != nil {
return nil, nwsErr
}
- f, err := GetForecast(p)
- if err != nil {
- return nil, &Error{
- Title: "unable get forecast",
- Type: "forecast-failed",
- Status: 500,
- Detail: err.Error(),
- }
+
+ f, nwsErr := GetForecast(p)
+ if nwsErr != nil {
+ return nil, nwsErr
}
+
fh, err := GetForecastHourly(p)
if err != nil {
return nil, &Error{
@@ -154,7 +151,6 @@ func GetForecastBundle(lat, lng float32) (*ForecastBundle, *Error) {
}
// NWS `/points` endpoint.
-//
func Points(lat, lng float32) (*Point, *Error) {
var nwsErr *Error
var expires time.Time
@@ -202,25 +198,33 @@ func Points(lat, lng float32) (*Point, *Error) {
}
// NWS forecast endpoint.
-//
-// TODO: return Error instead of error.
-func GetForecast(point *Point) (*Forecast, error) {
+func GetForecast(point *Point) (*Forecast, *Error) {
var nwsErr *Error
var expires time.Time
var body []byte
if point == nil {
- return nil, fmt.Errorf("forecast: point nil")
+ return nil, &Error{
+ Title: "point is nil",
+ Type: "forecast-points-invalid",
+ Status: 500,
+ Detail: "point is nil",
+ }
}
if len(point.Properties.Forecast) == 0 {
- return nil, fmt.Errorf("forecast: link empty")
+ return nil, &Error{
+ Title: "forecast link is empty",
+ Type: "forecast-link-invalid",
+ Status: 500,
+ Detail: "forecast link is empty",
+ }
}
if body = fCache.Get(point.Properties.Forecast); len(body) == 0 {
// Get the forecast
body, expires, nwsErr = get(point.Properties.Forecast)
if nwsErr != nil {
- return nil, fmt.Errorf("forecast: %v", nwsErr)
+ return nil, nwsErr
}
// Cache it.
fCache.Set(point.Properties.Forecast, body, expires)
@@ -230,10 +234,20 @@ func GetForecast(point *Point) (*Forecast, error) {
forecast := new(Forecast)
err := json.Unmarshal(body, forecast)
if err != nil {
- return nil, fmt.Errorf("forecast: decode: %v", err)
+ return nil, &Error{
+ Title: "forecast json unmarshal failed",
+ Type: "forecast-json-error",
+ Status: 500,
+ Detail: "forecast json unmarshal failed",
+ }
}
if len(forecast.Properties.Periods) == 0 {
- return nil, fmt.Errorf("forecast: periods empty")
+ return nil, &Error{
+ Title: "forecast has no periods",
+ Type: "forecast-periods-empty",
+ Status: 500,
+ Detail: "forecast has no periods",
+ }
}
return forecast, nil
}
diff --git a/nws/nws_test.go b/nws/nws_test.go
index 9e199bb..2b29ca1 100644
--- a/nws/nws_test.go
+++ b/nws/nws_test.go
@@ -59,9 +59,9 @@ func TestGetForecast(t *testing.T) {
}
// Get forecast.
- fc, err := GetForecast(np)
- if err != nil {
- t.Errorf("error: %v", err)
+ fc, nwsErr := GetForecast(np)
+ if nwsErr != nil {
+ t.Errorf("error: %v", nwsErr)
return
}