Comments (5)
Okay, let me think for a few days. Maybe interface{ ErrorValue() interface{} }
would fit.
from gojq.
Throw any error, that will be caught by try
. So exporting exitCodeError
doesn't make sense.
package main
import (
"errors"
"fmt"
"log"
"github.com/itchyny/gojq"
)
func main() {
query, err := gojq.Parse("try f catch .")
if err != nil {
log.Fatalln(err)
}
code, err := gojq.Compile(
query,
gojq.WithFunction("f", 0, 0,
func(v interface{}, _ []interface{}) interface{} {
return errors.New("error: custom error")
},
))
if err != nil {
log.Fatalln(err)
}
iter := code.Run(nil)
for {
v, ok := iter.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
log.Fatalln(err)
}
fmt.Printf("%#v\n", v)
}
}
5 is not a random exit code. It follows the jq specification (exit codes 1 through 5 have each meaning in jq).
% jq -n '"x" | error'; echo $?
jq: error (at <unknown>): x
5
from gojq.
Aha sorry I must have tested it wrongly somehow. But I was probably a bit unclear that I wanted to be able to throw any value, currently with error
you catch the Error()
string. So something like this:
$ jq -n 'try error({a: "some value"}) catch .'
{
"a": "some value"
}
In my example the custom function would return &gojq.ExitCodeError{Value: map[string]interface{}{"a": "some value"}
to get the same behaviour.
from gojq.
I implemented the catch
handler to use the error value when the error implements the following interface.
type ValueError interface {
error
Value() interface{}
}
from gojq.
Ok, thanks!
from gojq.
Related Issues (20)
- Can gojq be used in a streaming way? HOT 2
- Unexpected results occur when passing based on pointer HOT 1
- Change in behavior between go1.20 and go 1.21 (unable to query document) HOT 3
- Compiled JQ in not execution safe for constant mutable structs HOT 8
- Ability to concurrently run many queries on a single JSON object
- Duplicate keys in object literals have inconsistent behavior HOT 4
- Curious bug when reading from /dev/urandom or /dev/random HOT 2
- Allocated maps are ignored
- Support strings with JSON as input HOT 3
- `halt/0` (& co.) have the old jq 1.6 behaviour HOT 1
- Example of retrieving JSON or unmarshalled object HOT 4
- error parsing regexp: invalid named capture HOT 2
- Tests fail for s390x in Fedora since 0.12.15 HOT 3
- Question about handling NDJSON with the library HOT 1
- Week number is not taken into account during date creation in `strptime` HOT 5
- error parsing regexp that contains "?<=" HOT 3
- Add files/line/column to run errors like iteratorError?
- Possible memory leak from calling functions HOT 3
- Using "/" in a string constant HOT 2
- Question on the use of "as" in v0.12.16 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gojq.