Skip to content

Commit

Permalink
improved error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
yesoreyeram committed Jul 17, 2024
1 parent 9f610b8 commit c9bd79b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
4 changes: 4 additions & 0 deletions lib/go/jsonframer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @grafana/infinity-jsonframer

## 1.1.4

- improved error messages

## 1.1.3

- fixed timeseries single series type
Expand Down
4 changes: 2 additions & 2 deletions lib/go/jsonframer/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func TestJsonStringToFrame(t *testing.T) {
{
name: "empty string should throw error",
responseString: "",
wantErr: errors.New("empty json received"),
wantErr: errors.Join(errors.New("empty json received"), jsonframer.ErrInvalidJSONContent),
},
{
name: "invalid json should throw error",
responseString: "{",
wantErr: errors.New("invalid json response received"),
wantErr: errors.Join(errors.New("invalid json response received"), jsonframer.ErrInvalidJSONContent),
},
{
name: "valid json object should not throw error",
Expand Down
61 changes: 31 additions & 30 deletions lib/go/jsonframer/jsonframer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ type ColumnSelector struct {

func validateJson(jsonString string) (err error) {
if strings.TrimSpace(jsonString) == "" {
return errors.New("empty json received")
return errors.Join(errors.New("empty json received"), ErrInvalidJSONContent)
}
if !gjson.Valid(jsonString) {
return errors.New("invalid json response received")
return errors.Join(errors.New("invalid json response received"), ErrInvalidJSONContent)
}
return err
}
Expand Down Expand Up @@ -140,18 +140,15 @@ func ToFrame(jsonString string, options FramerOptions) (frame *data.Frame, err e
if err != nil {
return frame, err
}
switch options.FramerType {
default:
outString, err := GetRootData(jsonString, options.RootSelector)
if err != nil {
return frame, err
}
outString, err = getColumnValuesFromResponseString(outString, options.Columns)
if err != nil {
return frame, err
}
return getFrameFromResponseString(outString, options)
outString, err := GetRootData(jsonString, options.RootSelector)
if err != nil {
return frame, err
}
outString, err = getColumnValuesFromResponseString(outString, options.Columns)
if err != nil {
return frame, err
}
return getFrameFromResponseString(outString, options)
}

func GetRootData(jsonString string, rootSelector string) (string, error) {
Expand All @@ -160,13 +157,15 @@ func GetRootData(jsonString string, rootSelector string) (string, error) {
if r.Exists() {
return r.String(), nil
}
expr := jsonata.MustCompile(rootSelector)
if expr == nil {
err := errors.New("invalid root selector:" + rootSelector)
expr, err := jsonata.Compile(rootSelector)
if err != nil {
return "", errors.Join(ErrInvalidRootSelector, err)
}
var data interface{}
err := json.Unmarshal([]byte(jsonString), &data)
if expr == nil {
return "", errors.Join(ErrInvalidRootSelector)
}
var data any
err = json.Unmarshal([]byte(jsonString), &data)
if err != nil {
return "", errors.Join(ErrInvalidJSONContent, err)
}
Expand Down Expand Up @@ -215,7 +214,7 @@ func getColumnValuesFromResponseString(responseString string, columns []ColumnSe
}
a, err := json.Marshal(out)
if err != nil {
return "", err
return "", errors.Join(err, ErrInvalidJSONContent)
}
return string(a), nil
}
Expand All @@ -226,7 +225,7 @@ func getFrameFromResponseString(responseString string, options FramerOptions) (f
var out interface{}
err = json.Unmarshal([]byte(responseString), &out)
if err != nil {
return frame, fmt.Errorf("error while un-marshaling response. %s", err.Error())
return frame, errors.Join(fmt.Errorf("error while un-marshaling response. %s", err.Error()), ErrInvalidJSONContent)
}
columns := []gframer.ColumnSelector{}
for _, c := range options.Columns {
Expand All @@ -251,16 +250,18 @@ func getFrameFromResponseString(responseString string, options FramerOptions) (f
Columns: columns,
OverrideColumns: overrides,
})
if frame.Meta == nil {
frame.Meta = &data.FrameMeta{}
}
if options.FrameFormat == FrameFormatTimeSeries {
frame.Meta.Type = data.FrameTypeTimeSeriesWide
frame.Meta.TypeVersion = data.FrameTypeVersion{0, 1}
}
if options.FrameFormat == FrameFormatNumeric {
frame.Meta.Type = data.FrameTypeNumericLong
frame.Meta.TypeVersion = data.FrameTypeVersion{0, 1}
if frame != nil {
if frame.Meta == nil {
frame.Meta = &data.FrameMeta{}
}
if options.FrameFormat == FrameFormatTimeSeries {
frame.Meta.Type = data.FrameTypeTimeSeriesWide
frame.Meta.TypeVersion = data.FrameTypeVersion{0, 1}
}
if options.FrameFormat == FrameFormatNumeric {
frame.Meta.Type = data.FrameTypeNumericLong
frame.Meta.TypeVersion = data.FrameTypeVersion{0, 1}
}
}
return frame, err
}
Expand Down
2 changes: 1 addition & 1 deletion lib/go/jsonframer/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@grafana/infinity-jsonframer",
"private": true,
"version": "1.1.3",
"version": "1.1.4",
"scripts": {
"tidy": "go mod tidy",
"test:backend": "go test -v ./..."
Expand Down

0 comments on commit c9bd79b

Please sign in to comment.