Skip to content

Commit

Permalink
improved error messages (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesoreyeram authored Jun 27, 2024
1 parent 9f95a02 commit 843d239
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-guests-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@yesoreyeram/grafana-go-jsonframer": patch
---

Improved error messages in json framer
2 changes: 1 addition & 1 deletion .github/workflows/libs-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ jobs:
echo "-------- go version -----"
go version
- name: Test backend
run: npx --package=turbo@latest -- turbo test:backend
run: npx --package=turbo@2.0.5 -- turbo test:backend
9 changes: 9 additions & 0 deletions lib/go/jsonframer/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package jsonframer

import "errors"

var (
ErrInvalidRootSelector = errors.New("failed to compile JSONata expression")
ErrEvaluatingJSONata = errors.New("error evaluating JSONata expression")
ErrInvalidJSONContent = errors.New("invalid/empty JSON")
)
30 changes: 18 additions & 12 deletions lib/go/jsonframer/jsonframer.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,25 @@ func GetRootData(jsonString string, rootSelector string) (string, error) {
if r.Exists() {
return r.String(), nil
}
if e := jsonata.MustCompile(rootSelector); e != nil {
var data interface{}
err := json.Unmarshal([]byte(jsonString), &data)
if err != nil {
return "", err
}
if res, err := e.Eval(data); err == nil {
if r, err := json.Marshal(res); err == nil {
return string(r), nil
}
}
expr := jsonata.MustCompile(rootSelector)
if expr == nil {
err := errors.New("invalid root selector:" + rootSelector)
return "", errors.Join(ErrInvalidRootSelector, err)
}
var data interface{}
err := json.Unmarshal([]byte(jsonString), &data)
if err != nil {
return "", errors.Join(ErrInvalidJSONContent, err)
}
res, err := expr.Eval(data)
if err != nil {
return "", errors.Join(ErrEvaluatingJSONata, err)
}
r2, err := json.Marshal(res)
if err != nil {
return "", errors.Join(ErrInvalidJSONContent, err)
}
return "", errors.New("root object doesn't exist in the response. Root selector:" + rootSelector)
return string(r2), nil
}
return jsonString, nil

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
"lint": "turbo run lint:backend lint:frontend",
"spellcheck": "cspell -c cspell.config.json \"**/*.{ts,tsx,js,go,md,mdx,yml,yaml,json,scss,css}\""
},
"packageManager": "[email protected]",
"dependencies": {},
"devDependencies": {
"@changesets/cli": "^2.27.3",
"cspell": "^8.8.1",
"turbo": "^1.13.3"
"turbo": "2.0.5"
}
}
2 changes: 1 addition & 1 deletion turbo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"tasks": {
"clean": {
"cache": false,
"outputs": []
Expand Down
80 changes: 40 additions & 40 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2462,47 +2462,47 @@ tty-table@^4.1.5:
wcwidth "^1.0.1"
yargs "^17.7.1"

turbo-darwin-64@1.13.3:
version "1.13.3"
resolved "https://registry.yarnpkg.com/turbo-darwin-64/-/turbo-darwin-64-1.13.3.tgz#01d750e0f9ce4fced510357f1a9f7fe6312756ba"
integrity sha512-glup8Qx1qEFB5jerAnXbS8WrL92OKyMmg5Hnd4PleLljAeYmx+cmmnsmLT7tpaVZIN58EAAwu8wHC6kIIqhbWA==

turbo-darwin-arm64@1.13.3:
version "1.13.3"
resolved "https://registry.yarnpkg.com/turbo-darwin-arm64/-/turbo-darwin-arm64-1.13.3.tgz#a99950c8aff83d14070eeca987b0ee53dc881b2d"
integrity sha512-/np2xD+f/+9qY8BVtuOQXRq5f9LehCFxamiQnwdqWm5iZmdjygC5T3uVSYuagVFsZKMvX3ycySwh8dylGTl6lg==

turbo-linux-64@1.13.3:
version "1.13.3"
resolved "https://registry.yarnpkg.com/turbo-linux-64/-/turbo-linux-64-1.13.3.tgz#a8ea12c3d79f5bbc78b2ef37f47019edb8928219"
integrity sha512-G+HGrau54iAnbXLfl+N/PynqpDwi/uDzb6iM9hXEDG+yJnSJxaHMShhOkXYJPk9offm9prH33Khx2scXrYVW1g==

turbo-linux-arm64@1.13.3:
version "1.13.3"
resolved "https://registry.yarnpkg.com/turbo-linux-arm64/-/turbo-linux-arm64-1.13.3.tgz#e8691ebfab0e31e276020deee83b3866b4eb966f"
integrity sha512-qWwEl5VR02NqRyl68/3pwp3c/olZuSp+vwlwrunuoNTm6JXGLG5pTeme4zoHNnk0qn4cCX7DFrOboArlYxv0wQ==

turbo-windows-64@1.13.3:
version "1.13.3"
resolved "https://registry.yarnpkg.com/turbo-windows-64/-/turbo-windows-64-1.13.3.tgz#6629174c8f654e75c342a0e1b826620cb6e2795b"
integrity sha512-Nudr4bRChfJzBPzEmpVV85VwUYRCGKecwkBFpbp2a4NtrJ3+UP1VZES653ckqCu2FRyRuS0n03v9euMbAvzH+Q==

turbo-windows-arm64@1.13.3:
version "1.13.3"
resolved "https://registry.yarnpkg.com/turbo-windows-arm64/-/turbo-windows-arm64-1.13.3.tgz#327b8c87d8a01533deb3b7c3a108855aa7b6611d"
integrity sha512-ouJCgsVLd3icjRLmRvHQDDZnmGzT64GBupM1Y+TjtYn2LVaEBoV6hicFy8x5DUpnqdLy+YpCzRMkWlwhmkX7sQ==

turbo@^1.13.3:
version "1.13.3"
resolved "https://registry.yarnpkg.com/turbo/-/turbo-1.13.3.tgz#afb7bee4fa9f5b6041dac5b4a7d35fb98f279827"
integrity sha512-n17HJv4F4CpsYTvKzUJhLbyewbXjq1oLCi90i5tW1TiWDz16ML1eDG7wi5dHaKxzh5efIM56SITnuVbMq5dk4g==
turbo-darwin-64@2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/turbo-darwin-64/-/turbo-darwin-64-2.0.5.tgz#23c711370911d66e4589928243ec086c453709df"
integrity sha512-t/9XpWYIjOhIHUdwiR47SYBGYHkR1zWLxTkTNKZwCSn8BN0cfjPZ1BR6kcwYGxLGBhtl5GBf6A29nq2K7iwAjg==

turbo-darwin-arm64@2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/turbo-darwin-arm64/-/turbo-darwin-arm64-2.0.5.tgz#a1e47e97cca55ba6e15d52f2ae7af41e74a4d837"
integrity sha512-//5y4RJvnal8CttOLBwlaBqblcQb1qTlIxLN+I8O3E3rPuvHOupNKB9ZJxYIQ8oWf8ns8Ec8cxQ0GSBLTJIMtA==

turbo-linux-64@2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/turbo-linux-64/-/turbo-linux-64-2.0.5.tgz#ef9da9b8d90ab2d9d0c1c473ee27426e03cdc953"
integrity sha512-LDtEDU2Gm8p3lKu//aHXZFRKUCVu68BNF9LQ+HmiCKFpNyK7khpMTxIAAUhDqt+AzlrbxtrxcCpCJaWg1JDjHg==

turbo-linux-arm64@2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/turbo-linux-arm64/-/turbo-linux-arm64-2.0.5.tgz#a7a0da918effa62ecc55668d4f4dc7266dedd7fa"
integrity sha512-84wdrzntErBNxkHcwHxiTZdaginQAxGPnwLTyZj8lpUYI7okPoxy3jKpUeMHN3adm3iDedl/x0mYSIvVVkmOiA==

turbo-windows-64@2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/turbo-windows-64/-/turbo-windows-64-2.0.5.tgz#bfdce4337e2518eee9cb656258500effb1974b79"
integrity sha512-SgaFZ0VW6kHCJogLNuLEleAauAJx2Y48wazZGVRmBpgSUS2AylXesaBMhJaEScYqLz7mIRn6KOgwM8D4wTxI9g==

turbo-windows-arm64@2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/turbo-windows-arm64/-/turbo-windows-arm64-2.0.5.tgz#eb9d038e976cd3e7e11c6af5271314db9ef093b0"
integrity sha512-foUxLOZoru0IRNIxm53fkfM4ubas9P0nTFjIcHtd+E8YHeogt8GqTweNre2e6ri1EHDo71emmuQgpuoFCOXZMg==

turbo@2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/turbo/-/turbo-2.0.5.tgz#e33eaae264e8e373a1ca80762b9289a802ab4cbc"
integrity sha512-+6+hcWr4nwuESlKqUc626HMOTd3QT8hUOc9QM45PP1d4nErGkNOgExm4Pcov3in7LTuadMnB0gcd/BuzkEDIPw==
optionalDependencies:
turbo-darwin-64 "1.13.3"
turbo-darwin-arm64 "1.13.3"
turbo-linux-64 "1.13.3"
turbo-linux-arm64 "1.13.3"
turbo-windows-64 "1.13.3"
turbo-windows-arm64 "1.13.3"
turbo-darwin-64 "2.0.5"
turbo-darwin-arm64 "2.0.5"
turbo-linux-64 "2.0.5"
turbo-linux-arm64 "2.0.5"
turbo-windows-64 "2.0.5"
turbo-windows-arm64 "2.0.5"

type-fest@^0.13.1:
version "0.13.1"
Expand Down

0 comments on commit 843d239

Please sign in to comment.