You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2025/02/14 03:58:32 UTC
(couchdb) 01/01: [draft|don't merge] try to bring back emitting errors
This is an automated email from the ASF dual-hosted git repository.
vatamane pushed a commit to branch bring-back-easy-view-debugging
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit f087c11ea9c097baf0f7698aab9653bb19b1ee80
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Thu Feb 13 22:53:14 2025 -0500
[draft|don't merge] try to bring back emitting errors
In Javascript 1.8.5 we were able to just emit an error and it would serialized
to json with a name, message and stacktrace. That can help during debugging.
With later Spidermonkey versions and now QuickJS, the error is serialized to
"{}". That is expected based on [1] as Error fields are not enumerable ("Only
enumerable own properties are visited"). But we could still do it perhaps if we
detect that we have an Error in our emit wrapper and serialize it there to a
string. But somehow `intanceof` doesn't work in the wrapper.
[1]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
---
share/server/views.js | 9 ++++++++
src/couch/src/couch_os_process.erl | 1 +
src/couch/test/eunit/couch_js_tests.erl | 39 +++++++++++++++++++++++++--------
3 files changed, 40 insertions(+), 9 deletions(-)
diff --git a/share/server/views.js b/share/server/views.js
index 4fe3b7528..362b24999 100644
--- a/share/server/views.js
+++ b/share/server/views.js
@@ -84,6 +84,15 @@ var Views = (function() {
return {
// view helper functions
emit : function(key, value) {
+ // If user is debugging they may catch and emit an error object In
+ // Spidermonkey 1.8.5 and below an error object was stringified with a
+ // name and message, but modern engines do not do that any longer and the
+ // result is just "{}". To allow users an easier way to debug views make
+ // sure the stringified error shows name and message
+ if (value instanceof Error) {
+ log('***** emit() emitted an instance of error: '+ value.toString());
+ value = value.toString();
+ };
map_results.push([key, value]);
},
sum : function(values) {
diff --git a/src/couch/src/couch_os_process.erl b/src/couch/src/couch_os_process.erl
index 59ceeca13..42db13329 100644
--- a/src/couch/src/couch_os_process.erl
+++ b/src/couch/src/couch_os_process.erl
@@ -272,4 +272,5 @@ log_level(_) ->
debug.
log(#os_proc{log_level = LogLevel}, Fmt, Args) ->
+ io:format(standard_error, "~n " ++ Fmt ++ "~n", Args),
couch_log:LogLevel(Fmt, Args).
diff --git a/src/couch/test/eunit/couch_js_tests.erl b/src/couch/test/eunit/couch_js_tests.erl
index 764844e63..4e08919cd 100644
--- a/src/couch/test/eunit/couch_js_tests.erl
+++ b/src/couch/test/eunit/couch_js_tests.erl
@@ -21,15 +21,16 @@ couch_js_test_() ->
fun() -> test_util:start_couch([config]) end,
fun test_util:stop_couch/1,
with([
- ?TDEF(should_create_sandbox),
- ?TDEF(should_reset_properly),
- ?TDEF(should_freeze_doc_object),
- ?TDEF(should_roundtrip_utf8),
- ?TDEF(should_roundtrip_modified_utf8),
- ?TDEF(should_replace_broken_utf16),
- ?TDEF(should_allow_js_string_mutations),
- ?TDEF(should_bump_timing_and_call_stats),
- ?TDEF(should_exit_on_internal_error, 60)
+ %% ?TDEF(should_create_sandbox),
+ %% ?TDEF(should_reset_properly),
+ %% ?TDEF(should_freeze_doc_object),
+ ?TDEF(should_emit_error_details)%,
+ %% ?TDEF(should_roundtrip_utf8),
+ %% ?TDEF(should_roundtrip_modified_utf8),
+ %% ?TDEF(should_replace_broken_utf16),
+ %% ?TDEF(should_allow_js_string_mutations),
+ %% ?TDEF(should_bump_timing_and_call_stats),
+ %% ?TDEF(should_exit_on_internal_error, 60)
])
}
}.
@@ -103,6 +104,26 @@ should_freeze_doc_object(_) ->
?assertEqual([[[null, 1041], [null, 1041]]], Result2),
couch_query_servers:ret_os_process(Proc).
+%% erlfmt-ignore
+should_emit_error_details(_) ->
+ Src = <<"
+ function(doc) {
+ try {
+ non_existent.fun_call
+ } catch (e) {
+ log('********* is error instance of error? ' + (e instanceof Error));
+ emit('test_error', e);
+ }
+ }
+ ">>,
+ Proc = couch_query_servers:get_os_process(<<"javascript">>),
+ true = prompt(Proc, [<<"add_fun">>, Src]),
+ Doc = {[{<<"foo">>, 42}]},
+ Result = prompt(Proc, [<<"map_doc">>, Doc]),
+ io:format(standard_error, "~n +++++ ~p:~p@~B ~p~n", [?MODULE, ?FUNCTION_NAME, ?LINE, Result]),
+ ?assertNotEqual([[[<<"test_error">>, {[]}]]], Result),
+ couch_query_servers:ret_os_process(Proc).
+
%% erlfmt-ignore
should_roundtrip_utf8(_) ->
% Try round tripping UTF-8 both directions through