Skip to content

Commit

Permalink
feat: better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
miton18 committed Jul 1, 2024
1 parent d257bfe commit 0d363ca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
29 changes: 19 additions & 10 deletions base/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package base
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"strconv"
"strings"
"time"
"strconv"
)

// Exec execute a WarpScript on the backend, returning resultat as byte array
Expand All @@ -25,10 +25,19 @@ func (c *Client) Exec(warpScript string) ([]byte, error) {
}

if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Header.Get(HeaderErrorMessage))

if h := res.Header.Get(HeaderErrorMessage); h != "" {
return nil, errors.New(h)
}

if body, err := io.ReadAll(res.Body); err == nil {

Check failure on line 33 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll

Check failure on line 33 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll

Check failure on line 33 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll

Check failure on line 33 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll
return nil, errors.New(string(body))
}

return nil, fmt.Errorf("received status code '%s'", res.Status)
}

bts, err := ioutil.ReadAll(res.Body)
bts, err := io.ReadAll(res.Body)

Check failure on line 40 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll

Check failure on line 40 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll

Check failure on line 40 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll

Check failure on line 40 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -59,7 +68,7 @@ func (c *Client) Find(selector Selector) ([]byte, error) {
return nil, err
}

bts, err := ioutil.ReadAll(res.Body)
bts, err := io.ReadAll(res.Body)

Check failure on line 71 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll

Check failure on line 71 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll

Check failure on line 71 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll

Check failure on line 71 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -90,7 +99,7 @@ func (c *Client) Update(gts GTSList) error {
return err
}

bts, err := ioutil.ReadAll(res.Body)
bts, err := io.ReadAll(res.Body)

Check failure on line 102 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll

Check failure on line 102 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll
if err != nil {
return err
}
Expand Down Expand Up @@ -122,13 +131,13 @@ func (c *Client) Meta(gtsList GTSList) ([]byte, error) {
return nil, err
}

bts, err := ioutil.ReadAll(res.Body)
bts, err := io.ReadAll(res.Body)

Check failure on line 134 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll

Check failure on line 134 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll
if err != nil {
return nil, err
}

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Headers: %+v\nBody: %+v", res.Header, string(bts))
return nil, fmt.Errorf("headers: %+v\nBody: %+v", res.Header, string(bts))
}

return nil, res.Body.Close()
Expand Down Expand Up @@ -159,7 +168,7 @@ func (c *Client) Fetch(selector Selector, start time.Time, stop time.Time) ([]by
return nil, err
}

bts, err := ioutil.ReadAll(res.Body)
bts, err := io.ReadAll(res.Body)

Check failure on line 171 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll

Check failure on line 171 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -205,7 +214,7 @@ func (c *Client) Delete(selector Selector, start time.Time, stop time.Time) ([]b
return nil, err
}

bts, err := ioutil.ReadAll(res.Body)
bts, err := io.ReadAll(res.Body)

Check failure on line 217 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll

Check failure on line 217 in base/api.go

View workflow job for this annotation

GitHub Actions / Build

undefined: io.ReadAll
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions base/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package base
import "errors"

var (
NoReadTokenError = errors.New("This Warp10 call need a READ token access on the data")
NoWriteTokenError = errors.New("This Warp10 call need a WRITE token access on the data")
)
NoReadTokenError = errors.New("this Warp10 call need a READ token access on the data")
NoWriteTokenError = errors.New("this Warp10 call need a WRITE token access on the data")
)
20 changes: 10 additions & 10 deletions base/gts-helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,44 +100,44 @@ func parseSensisionLine(in string) (int64, float64, float64, float64, string, La

ctx := strings.Split(g[0], "/")
if len(ctx) != 3 {
err = errors.New("Cannot parse datapoint: " + in)
err = errors.New("cannot parse datapoint: " + in)
return ts, lat, long, alt, c, l, a, v, err
}

if ctx[0] == "" {
err = errors.New("No timestamp provided")
err = errors.New("no timestamp provided")
return ts, lat, long, alt, c, l, a, v, err
}
ts, err = strconv.ParseInt(ctx[0], 10, 64)
if err != nil {
err = errors.New("Cannot parse " + ctx[0] + " as valid timestamp")
err = errors.New("cannot parse " + ctx[0] + " as valid timestamp")
return ts, lat, long, alt, c, l, a, v, err
}

if ctx[2] != "" {
alt, err = strconv.ParseFloat(ctx[2], 64)
if err != nil {
err = errors.New("Cannot parse " + ctx[0] + " as valid altitude")
err = errors.New("cannot parse " + ctx[0] + " as valid altitude")
return ts, lat, long, alt, c, l, a, v, err
}
}

if ctx[1] != "" {
latlong := strings.Split(ctx[1], ":")
if len(latlong) != 2 {
err = errors.New("Cannot parse " + ctx[1] + " as valid latitude:longitude")
err = errors.New("cannot parse " + ctx[1] + " as valid latitude:longitude")
return ts, lat, long, alt, c, l, a, v, err
}

lat, err = strconv.ParseFloat(latlong[0], 64)
if err != nil {
err = errors.New("Cannot parse " + ctx[0] + " as valid latitude")
err = errors.New("cannot parse " + ctx[0] + " as valid latitude")
return ts, lat, long, alt, c, l, a, v, err
}

long, err = strconv.ParseFloat(latlong[1], 64)
if err != nil {
err = errors.New("Cannot parse " + ctx[0] + " as valid latitude")
err = errors.New("cannot parse " + ctx[0] + " as valid latitude")
return ts, lat, long, alt, c, l, a, v, err
}
}
Expand All @@ -149,7 +149,7 @@ func parseSensisionLine(in string) (int64, float64, float64, float64, string, La
// test }
//fmt.Println(fmt.Sprintf("%+v", classLabelsAttributes))
if len(classLabelsAttributes) != 2 {
err = errors.New("Cannot parse " + g[1] + " as valid class + labels + attributes")
err = errors.New("cannot parse " + g[1] + " as valid class + labels + attributes")
return ts, lat, long, alt, c, l, a, v, err
}

Expand All @@ -168,7 +168,7 @@ func parseSensisionLine(in string) (int64, float64, float64, float64, string, La
for _, labelPair := range labelsValue {
keyVal := strings.Split(labelPair, "=")
if len(keyVal) != 2 {
err = errors.New("Cannot parse " + labelPair + " as valid key and value label")
err = errors.New("cannot parse " + labelPair + " as valid key and value label")
return ts, lat, long, alt, c, l, a, v, err
}
l[keyVal[0]] = keyVal[1]
Expand All @@ -179,7 +179,7 @@ func parseSensisionLine(in string) (int64, float64, float64, float64, string, La
vStr = strings.Trim(vStr, "'")

if len(vStr) == 0 {
err = errors.New("Cannot parse " + strings.Join(g[2:], " ") + " as valid value")
err = errors.New("cannot parse " + strings.Join(g[2:], " ") + " as valid value")
return ts, lat, long, alt, c, l, a, v, err
}

Expand Down

0 comments on commit 0d363ca

Please sign in to comment.