Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement error/0 and fix try catch against non-string errors #237

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

import com.fasterxml.jackson.databind.JsonNode;

import net.thisptr.jackson.jq.internal.misc.JsonNodeUtils;

public class JsonQueryUserException extends JsonQueryException {
private static final long serialVersionUID = -2719442463094461632L;

public JsonQueryUserException(final JsonNode msg) {
super(msg.asText());
private JsonNode value;

public JsonQueryUserException(final JsonNode value) {
super(value.isTextual() ? value.asText() : JsonNodeUtils.toString(value));
this.value = value;
}

@Override
public JsonNode getMessageAsJsonNode() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@
import net.thisptr.jackson.jq.path.Path;

@AutoService(Function.class)
@BuiltinFunction("error/1")
@BuiltinFunction({ "error/0", "error/1" })
public class ErrorFunction implements Function {
@Override
public void apply(final Scope scope, final List<Expression> args, final JsonNode in, final Path ipath, final PathOutput output, final Version version) throws JsonQueryException {
args.get(0).apply(scope, in, (out) -> {
if (out.isNull())
if (args.size() == 0) {
if (in.isNull())
return;
throw new JsonQueryUserException(out);
});
throw new JsonQueryUserException(in);
} else {
args.get(0).apply(scope, in, (out) -> {
if (out.isNull())
return;
throw new JsonQueryUserException(out);
});
}
}
}
21 changes: 20 additions & 1 deletion jackson-jq/src/test/resources/tests/functions/error.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
- {"v":"[1.5,1.5]","q":"error(empty)","in":null,"out":[]}
- q: 'error'
in: null
out: []
v: "[1.5,1.5]"
- q: 'try error catch .'
in: foo
out:
- foo
- q: 'try error catch .'
in: 0
out:
- 0

- q: 'error(empty)'
in: null
out: []
v: "[1.5,1.5]"
- q: 'try error("a", error("foo")) catch .'
out:
- a
- q: 'try error(error("foo")) catch .'
out:
- foo
- q: 'try error({x:0}) catch .'
out:
- {"x":0}
- q: 'error(null)'
out: []