Skip to content

Commit

Permalink
Improve and export errors in packages (#8)
Browse files Browse the repository at this point in the history
* Improve and export errors

* Update lib/go/csvframer/CHANGELOG.md

Co-authored-by: Tim Mulqueen <[email protected]>

* Update lib/go/framesql/CHANGELOG.md

Co-authored-by: Tim Mulqueen <[email protected]>

* Update lib/go/transformations/CHANGELOG.md

Co-authored-by: Tim Mulqueen <[email protected]>

* Bump versions in package.json

---------

Co-authored-by: Tim Mulqueen <[email protected]>
  • Loading branch information
ivanahuckova and Multimo authored Sep 11, 2024
1 parent c9bd79b commit 8657a8c
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 20 deletions.
4 changes: 4 additions & 0 deletions lib/go/csvframer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @grafana/infinity-csvframer

## 1.0.1

- ⚙️ **Chore**: improved error messages

## 1.0.0

- chore release
Expand Down
8 changes: 8 additions & 0 deletions lib/go/csvframer/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package csvframer

import "errors"

var (
ErrEmptyCsv = errors.New("empty/invalid csv")
ErrReadingCsvResponse = errors.New("error reading csv response")
)
4 changes: 2 additions & 2 deletions lib/go/csvframer/framer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type FramerOptions struct {

func ToFrame(csvString string, options FramerOptions) (frame *data.Frame, err error) {
if strings.TrimSpace(csvString) == "" {
return frame, errors.New("empty/invalid csv")
return frame, ErrEmptyCsv
}
r := csv.NewReader(strings.NewReader(csvString))
r.LazyQuotes = true
Expand All @@ -47,7 +47,7 @@ func ToFrame(csvString string, options FramerOptions) (frame *data.Frame, err er
continue
}
if !options.SkipLinesWithError {
return frame, fmt.Errorf("error reading csv response. %w, %v", err, record)
return frame, errors.Join(ErrReadingCsvResponse, fmt.Errorf("%w, %v", err, record))
}
}
out := []interface{}{}
Expand Down
3 changes: 1 addition & 2 deletions lib/go/csvframer/framer_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package csvframer_test

import (
"errors"
"strings"
"testing"

Expand All @@ -21,7 +20,7 @@ func TestCsvStringToFrame(t *testing.T) {
}{
{
name: "empty csv should return error",
wantError: errors.New("empty/invalid csv"),
wantError: csvframer.ErrEmptyCsv,
},
{
name: "valid csv should not return error",
Expand Down
2 changes: 1 addition & 1 deletion lib/go/csvframer/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@grafana/infinity-csvframer",
"private": true,
"version": "1.0.0",
"version": "1.0.1",
"scripts": {
"tidy": "go mod tidy",
"test:backend": "go test -v ./..."
Expand Down
4 changes: 4 additions & 0 deletions lib/go/framesql/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @grafana/infinity-framesql

## 1.0.1

- ⚙️ **Chore**: improved error messages

## 1.0.0

- chore release
Expand Down
7 changes: 7 additions & 0 deletions lib/go/framesql/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package framesql

import "errors"

var (
ErrEmptySummarizeExpression = errors.New("empty/invalid summarize expression")
)
3 changes: 1 addition & 2 deletions lib/go/framesql/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package framesql

import (
"errors"
"fmt"
"regexp"
"strings"

Expand All @@ -12,7 +11,7 @@ import (

func EvaluateInFrame(expression string, input *data.Frame) (any, error) {
if strings.TrimSpace(expression) == "" {
return nil, errors.New(strings.TrimSpace(fmt.Sprintf("empty/invalid expression. %s", expression)))
return nil, ErrEmptySummarizeExpression
}
parsedExpression, err := govaluate.NewEvaluableExpressionWithFunctions(expression, expressionFunctions)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions lib/go/framesql/frame_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package framesql_test

import (
"errors"
"testing"

"github.com/grafana/grafana-plugin-sdk-go/data"
Expand All @@ -19,7 +18,7 @@ func TestEvaluateInFrame(t *testing.T) {
wantErr error
}{
{
wantErr: errors.New("empty/invalid expression."),
wantErr: framesql.ErrEmptySummarizeExpression,
},
{
input: data.NewFrame("test", data.NewField("sample", nil, []*float64{toFP(1), toFP(2), toFP(0.5), toFP(1.5)})),
Expand Down
2 changes: 1 addition & 1 deletion lib/go/framesql/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@grafana/infinity-framesql",
"private": true,
"version": "1.0.0",
"version": "1.0.1",
"scripts": {
"tidy": "go mod tidy",
"test:backend": "go test -v ./..."
Expand Down
4 changes: 4 additions & 0 deletions lib/go/transformations/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @grafana/infinity-transformations

## 1.0.1

- ⚙️ **Chore**: improved error messages

## 1.0.0

- chore release
Expand Down
14 changes: 14 additions & 0 deletions lib/go/transformations/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package transformations

import "errors"

var (
ErrSummarizeByFieldNotFound = errors.New("summarize by field not found. Not applying summarize")
ErrNotUniqueFieldNames = errors.New("field names are not unique. Not applying filter")
ErrEvaluatingFilterExpression = errors.New("error evaluating filter expression")

ErrMergeTransformationNoFrameSupplied = errors.New("no frames supplied for merge frame transformation")
ErrMergeTransformationDifferentFields = errors.New("unable to merge fields due to different fields")
ErrMergeTransformationDifferentFieldNames = errors.New("unable to merge field due to different field names")
ErrMergeTransformationDifferentFieldTypes = errors.New("unable to merge fields due to different field types")
)
4 changes: 2 additions & 2 deletions lib/go/transformations/filterExpression.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ApplyFilter(frame *data.Frame, filterExpression string) (*data.Frame, error
fieldKeys := map[string]bool{}
for _, field := range frame.Fields {
if fieldKeys[field.Name] {
return frame, errors.New("field names are not unique. Not applying filter")
return frame, ErrNotUniqueFieldNames
}
fieldKeys[field.Name] = true
}
Expand All @@ -57,7 +57,7 @@ func ApplyFilter(frame *data.Frame, filterExpression string) (*data.Frame, error
}
result, err := parsedExpression.Evaluate(parameters)
if err != nil {
return frame, fmt.Errorf("error evaluating filter expression for row %d. error: %w. Not applying filter", inRowIdx, err)
return frame, errors.Join(ErrEvaluatingFilterExpression, fmt.Errorf("error: %w. row %d. Not applying filter", err, inRowIdx))
}
if currentMatch, ok := result.(bool); ok {
match = &currentMatch
Expand Down
10 changes: 4 additions & 6 deletions lib/go/transformations/merge.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package transformations

import (
"errors"

"github.com/grafana/grafana-plugin-sdk-go/data"
)

Expand All @@ -17,22 +15,22 @@ type MergeFramesOptions struct {
// Ref: https://github.com/grafana/grafana/blob/v9.5.2/packages/grafana-data/src/transformations/transformers/merge.ts
func Merge(inputFrames []*data.Frame, option MergeFramesOptions) (*data.Frame, error) {
if len(inputFrames) < 1 {
return nil, errors.New("no frames supplied for merge frame transformation")
return nil, ErrMergeTransformationNoFrameSupplied
}
outFrame := inputFrames[0]
for frameId, frame := range inputFrames {
if frameId == 0 {
continue
}
if len(outFrame.Fields) != len(frame.Fields) {
return nil, errors.New("unable to merge fields due to different fields")
return nil, ErrMergeTransformationDifferentFields
}
for fi, f := range frame.Fields {
if f.Name != outFrame.Fields[fi].Name {
return nil, errors.New("unable to merge field due to different field names")
return nil, ErrMergeTransformationDifferentFieldNames
}
if f.Type() != outFrame.Fields[fi].Type() {
return nil, errors.New("unable to merge fields due to different field types")
return nil, ErrMergeTransformationDifferentFieldTypes
}
}
for rowId := 0; rowId < frame.Rows(); rowId++ {
Expand Down
2 changes: 1 addition & 1 deletion lib/go/transformations/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@grafana/infinity-transformations",
"private": true,
"version": "1.0.0",
"version": "1.0.1",
"scripts": {
"tidy": "go mod tidy",
"test:backend": "go test -v ./..."
Expand Down
2 changes: 1 addition & 1 deletion lib/go/transformations/summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func GetSummarizeByFrame(frame *data.Frame, expression, by string, alias string)
}
}
if byField == nil {
return frame, errors.New("summarize by field not found. Not applying summarize")
return frame, ErrSummarizeByFieldNotFound
}
uniqueValuesArray := []any{}
uniqueValues := map[any]bool{}
Expand Down

0 comments on commit 8657a8c

Please sign in to comment.