From f8a90e743fa92da2dfe8f68a854b0bf920b02bf8 Mon Sep 17 00:00:00 2001 From: itchyny Date: Tue, 22 Nov 2022 20:40:51 +0900 Subject: [PATCH] Implement error/0 and fix try catch against non-string errors --- .../jq/exception/JsonQueryUserException.java | 14 +++++++++++-- .../jq/internal/functions/ErrorFunction.java | 16 +++++++++----- .../test/resources/tests/functions/error.yaml | 21 ++++++++++++++++++- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/jackson-jq/src/main/java/net/thisptr/jackson/jq/exception/JsonQueryUserException.java b/jackson-jq/src/main/java/net/thisptr/jackson/jq/exception/JsonQueryUserException.java index 56e3abf6..de1da0b8 100644 --- a/jackson-jq/src/main/java/net/thisptr/jackson/jq/exception/JsonQueryUserException.java +++ b/jackson-jq/src/main/java/net/thisptr/jackson/jq/exception/JsonQueryUserException.java @@ -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; } } diff --git a/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/functions/ErrorFunction.java b/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/functions/ErrorFunction.java index 515e3a1b..8d9efbad 100644 --- a/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/functions/ErrorFunction.java +++ b/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/functions/ErrorFunction.java @@ -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 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); + }); + } } } diff --git a/jackson-jq/src/test/resources/tests/functions/error.yaml b/jackson-jq/src/test/resources/tests/functions/error.yaml index 28e73022..5c2e3649 100644 --- a/jackson-jq/src/test/resources/tests/functions/error.yaml +++ b/jackson-jq/src/test/resources/tests/functions/error.yaml @@ -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: []