Skip to content

Commit 33da670

Browse files
committed
$match - raise an explicit error if no return value is available, i.e., raise and undefined result that is propagated to the caller
1 parent 33701ef commit 33da670

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/erlang_red_jsonata.erl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ execute(JSONata, Msg) ->
4949
"jsonata unsupported function: ~p", element(3, H)
5050
)
5151
)};
52+
error:undefined:Stacktrace ->
53+
[H | _] = Stacktrace,
54+
{undefined,
55+
list_to_binary(
56+
io_lib:format(
57+
"jsonata undefined result: ~p", element(3, H)
58+
)
59+
)};
5260
E:M:S ->
5361
{exception, {E, M, S}}
5462
end.
@@ -285,7 +293,7 @@ handle_local_function(
285293
Args = [global],
286294
case re:run(any_to_list(Str), R, [{capture, all, binary} | Args]) of
287295
nomatch ->
288-
undefined;
296+
erlang:error(undefined, [{"$match", Args}]);
289297
{match, CaptureBinary} ->
290298
{match, CaptureIdx} =
291299
re:run(any_to_list(Str), R, [{capture, all, index} | Args]),
@@ -313,9 +321,9 @@ handle_local_function(
313321
);
314322
handle_local_function(
315323
jsonata_match,
316-
[_Str, _RegExp, MatchLimit]
324+
[_Str, _RegExp, MatchLimit] = Args
317325
) when is_integer(MatchLimit), MatchLimit =:= 0 ->
318-
undefined;
326+
erlang:error(undefined, [{"$match", Args}]);
319327
handle_local_function(
320328
jsonata_match,
321329
[Str, RegExp, MatchLimit]
@@ -421,8 +429,6 @@ match_capture_objects(
421429
match_capture_objects(RestBinary, RestIndicies, [CapHsh | Acc]).
422430
%%
423431
%%
424-
match_capture_limit(undefined, _, _) ->
425-
undefined;
426432
match_capture_limit(_Result, 0, [Hd | []] = _Acc) ->
427433
Hd;
428434
match_capture_limit(_Result, 0, Acc) ->

test/erlang_red_jsonata_test.erl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,17 @@ match_operator_multiple_matches_with_limit_test() ->
119119
)
120120
),
121121

122+
%% undefined return value is generated when there is no match or no
123+
%% no result. The corresponding attribute on the msg object isn't defined.
122124
?assertEqual(
123-
{ok, undefined},
124-
erlang_red_jsonata:execute(
125+
undefined,
126+
element(1, erlang_red_jsonata:execute(
125127
"$match($$.payload, /([0-9]+)([a-z]+)([0-9]+)([a-z]+)/, 0)",
126128
#{
127129
<<"payload">> =>
128130
<<"123abc456def|||789ghi012jkl|||345mno678pqr||">>
129131
}
130-
)
132+
))
131133
),
132134

133135
{exception,
@@ -190,15 +192,17 @@ match_operator_multiple_matches_with_limit_test() ->
190192
}
191193
)
192194
),
195+
%% undefined because no match is found, this will result in the msg variable
196+
%% that should be assigned this result not to be defined.
193197
?assertEqual(
194-
{ok, undefined},
195-
erlang_red_jsonata:execute(
198+
undefined,
199+
element(1, erlang_red_jsonata:execute(
196200
"$match($$.payload, /([0-9]+)([a-z]+)([0-9]+)([a-z]+)/, $$.limit)",
197201
#{
198202
<<"payload">> => <<"3">>,
199203
<<"limit">> => <<"2">>
200204
}
201-
)
205+
))
202206
).
203207

204208
flatten_test() ->

0 commit comments

Comments
 (0)