Skip to content

Commit cf99034

Browse files
committed
Handle another expected error in the scanner
It's the same idea as `SyntaxError` and `TypeErorrs` above. While at it improve the filter test to use individual ddocs for each error. This way one error or crash won't influence the others.
1 parent a7b963e commit cf99034

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,8 @@ expected_error({error, {_, {<<"unnamed_error">>, _}}}, {error, {_, {<<"unnamed_e
544544
true;
545545
expected_error({error, {_, {<<"SyntaxError">>, _}}}, {error, {_, {<<"SyntaxError">>, _}}}) ->
546546
true;
547+
expected_error({error, {_, {<<"ReferenceError">>, _}}}, {error, {_, {<<"ReferenceError">>, _}}}) ->
548+
true;
547549
expected_error(_, _) ->
548550
false.
549551

src/couch_quickjs/test/couch_quickjs_scanner_plugin_tests.erl

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,11 @@ t_filter_only({_, DbName}) ->
235235
end.
236236

237237
t_filter_with_expected_error({_, DbName}) ->
238-
ok = add_doc(DbName, ?DDOC1, ddoc_filter_error(#{})),
238+
% Put filters with errors in different ddocs so than each be tested in isolation
239+
ok = add_doc(DbName, <<"_design/ddoc_custom_error">>, ddoc_filter_custom_error(#{})),
240+
ok = add_doc(DbName, <<"_design/ddoc_type_error">>, ddoc_filter_type_error(#{})),
241+
ok = add_doc(DbName, <<"_design/ddoc_syntax_error">>, ddoc_filter_syntax_error(#{})),
242+
ok = add_doc(DbName, <<"_design/ddoc_reference_error">>, ddoc_filter_reference_error(#{})),
239243
meck:reset(couch_scanner_server),
240244
meck:reset(?PLUGIN),
241245
config:set("couch_scanner_plugins", atom_to_list(?PLUGIN), "true", false),
@@ -245,9 +249,9 @@ t_filter_with_expected_error({_, DbName}) ->
245249
true ->
246250
?assertEqual(1, num_calls(complete, 1)),
247251
?assertEqual(1, num_calls(db, ['_', DbName])),
248-
?assertEqual(1, num_calls(ddoc, ['_', DbName, '_'])),
252+
?assertEqual(4, num_calls(ddoc, ['_', DbName, '_'])),
249253
?assertEqual(0, couch_stats:sample([couchdb, query_server, process_error_exits])),
250-
?assertEqual(10, couch_stats:sample([couchdb, query_server, process_errors])),
254+
?assertEqual(40, couch_stats:sample([couchdb, query_server, process_errors])),
251255
?assertEqual(0, couch_stats:sample([couchdb, query_server, process_exits])),
252256
% start, complete and no warning as we expected the error, so 2 total only
253257
?assertEqual(2, log_calls(warning));
@@ -397,12 +401,17 @@ ddoc_filter(Doc) ->
397401
}
398402
}.
399403

400-
ddoc_filter_error(Doc) ->
401-
Doc#{
402-
filters => #{
403-
f => <<"function(doc, req) {throw(\"foo\");}">>
404-
}
405-
}.
404+
ddoc_filter_custom_error(Doc) ->
405+
Doc#{filters => #{f => <<"function(doc, req) {throw(\"foo\");}">>}}.
406+
407+
ddoc_filter_type_error(Doc) ->
408+
Doc#{filters => #{f => <<"function(doc, req){if(doc.missing.foo){return false;}}">>}}.
409+
410+
ddoc_filter_syntax_error(Doc) ->
411+
Doc#{filters => #{f => <<"function(doc, req){JSON.parse([]); return true;}">>}}.
412+
413+
ddoc_filter_reference_error(Doc) ->
414+
Doc#{filters => #{f => <<"function(doc, req){if(potato){return true;}}">>}}.
406415

407416
ddoc_view(Doc) ->
408417
Doc#{
@@ -461,6 +470,7 @@ ddoc_view(Doc) ->
461470
},
462471
v_type_error => #{map => <<"function(doc){emit(doc.missing.foo,1);}">>},
463472
v_syntax_error => #{map => <<"function(doc){emit(JSON.parse([]),1);}">>},
473+
v_reference_error => #{map => <<"function(doc){emit(potato,1);}">>},
464474
v_expr_fun1 => #{map => <<"(function(doc) {emit(1,2)})\n">>},
465475
v_expr_fun2 => #{map => <<"(function(doc) {emit(3,4)});">>},
466476
v_expr_fun3 => #{map => <<"y=9;\n(function(doc) {emit(5,y)})">>},

0 commit comments

Comments
 (0)