summaryrefslogtreecommitdiffstats
path: root/nws
diff options
context:
space:
mode:
authorsiddharth ravikumar <s@ricketyspace.net>2022-07-02 12:16:18 -0400
committersiddharth ravikumar <s@ricketyspace.net>2022-07-02 12:16:18 -0400
commit4d6b34a33a734a81168c3d7d2306b29a59fd0795 (patch)
treec84e4f0cd3ef3fc9c973d0b1a7025eaf78b65b47 /nws
parent6d7b28bca84e7c7be45470c24286adc779c945fb (diff)
nws: update `Points`
Change return error type to `nws.Error`
Diffstat (limited to 'nws')
-rw-r--r--nws/nws.go37
-rw-r--r--nws/nws_test.go12
2 files changed, 29 insertions, 20 deletions
diff --git a/nws/nws.go b/nws/nws.go
index c5dfe19..694f077 100644
--- a/nws/nws.go
+++ b/nws/nws.go
@@ -119,14 +119,9 @@ func (e Error) Error() string {
// Gets NWS's forecast and hourly forecast.
func GetForecastBundle(lat, lng float32) (*ForecastBundle, *Error) {
- p, err := Points(lat, lng)
- if err != nil {
- return nil, &Error{
- Title: "unable get points",
- Type: "points-failed",
- Status: 500,
- Detail: err.Error(),
- }
+ p, nwsErr := Points(lat, lng)
+ if nwsErr != nil {
+ return nil, nwsErr
}
f, err := GetForecast(p)
if err != nil {
@@ -160,8 +155,7 @@ func GetForecastBundle(lat, lng float32) (*ForecastBundle, *Error) {
// NWS `/points` endpoint.
//
-// TODO: return Error instead of error
-func Points(lat, lng float32) (*Point, error) {
+func Points(lat, lng float32) (*Point, *Error) {
var nwsErr *Error
var expires time.Time
var body []byte
@@ -171,7 +165,7 @@ func Points(lat, lng float32) (*Point, error) {
url := fmt.Sprintf("https://api.weather.gov/points/%s", ll)
body, expires, nwsErr = get(url)
if nwsErr != nil {
- return nil, fmt.Errorf("points: %v", nwsErr)
+ return nil, nwsErr
}
// Cache it.
pCache.Set(ll, body, expires)
@@ -181,13 +175,28 @@ func Points(lat, lng float32) (*Point, error) {
point := new(Point)
err := json.Unmarshal(body, point)
if err != nil {
- return nil, fmt.Errorf("points: decode: %v", err)
+ return nil, &Error{
+ Title: "unable json unmarshal",
+ Type: "points-json-error",
+ Status: 500,
+ Detail: err.Error(),
+ }
}
if point.Properties.Forecast == "" {
- return nil, fmt.Errorf("points: forecast empty")
+ return nil, &Error{
+ Title: "forecast empty",
+ Type: "points-forecast-error",
+ Status: 500,
+ Detail: "forecast is empty",
+ }
}
if point.Properties.ForecastHourly == "" {
- return nil, fmt.Errorf("points: forecasthourly empty")
+ return nil, &Error{
+ Title: "forecast hourly empty",
+ Type: "points-forecast-error",
+ Status: 500,
+ Detail: "forecast hourly is empty",
+ }
}
return point, nil
}
diff --git a/nws/nws_test.go b/nws/nws_test.go
index 623ec83..9e199bb 100644
--- a/nws/nws_test.go
+++ b/nws/nws_test.go
@@ -52,9 +52,9 @@ func TestPoints(t *testing.T) {
func TestGetForecast(t *testing.T) {
// Get point.
- np, err := Points(41.115, -83.177)
- if err != nil {
- t.Errorf("error: %v", err)
+ np, nwsErr := Points(41.115, -83.177)
+ if nwsErr != nil {
+ t.Errorf("error: %v", nwsErr)
return
}
@@ -106,9 +106,9 @@ func TestGetForecast(t *testing.T) {
func TestGetForecastHourly(t *testing.T) {
// Get point.
- np, err := Points(41.115, -83.177)
- if err != nil {
- t.Errorf("error: %v", err)
+ np, nwsErr := Points(41.115, -83.177)
+ if nwsErr != nil {
+ t.Errorf("error: %v", nwsErr)
return
}