couchdb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Brian Candler (JIRA)" <j...@apache.org>
Subject [jira] Commented: (COUCHDB-354) Ungraceful behaviour if view returns a function
Date Fri, 22 May 2009 15:40:45 GMT

    [ https://issues.apache.org/jira/browse/COUCHDB-354?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12712141#action_12712141
] 

Brian Candler commented on COUCHDB-354:
---------------------------------------

It's the only version provided in both Ubuntu Hardy and Ubuntu Jaunty. It also works perfectly
fine for me in general, and I don't think the problem is anything to do with the Javascript
engine.

Hidden in the barf you can see what the error is - it's caused by the toJSON function in main.js,
which in skeleton form looks like this: it's a strange sort of case statement.

    return  {'Array':f1, 'Boolean':f2, ...}[val.constructor.name](val);

The problem is that if val is a function, val.constructor.name is 'Function' which is not
defined in the object, so you are essentially calling undefined(val).

You can get a rather better error by catching unexpected values, e.g. like this:

--- main.js.orig	2009-05-22 16:26:22.000000000 +0100
+++ main.js	2009-05-22 16:32:05.000000000 +0100
@@ -306,7 +306,7 @@
   if (typeof(val) == "xml") { // E4X support
     val = val.toXMLString();
   }
-  return {
+  var handlers = {
     "Array": function(v) {
       var buf = [];
       for (var i = 0; i < v.length; i++) {
@@ -351,7 +351,11 @@
       }
       return '"' + v + '"';
     }
-  }[val != null ? val.constructor.name : "Object"](val);
+  }
+  var k = val != null ? val.constructor.name : "Object";
+  var h = handlers[k];
+  if (!h) { throw ("Cannot encode '" + k + "' value as JSON") };
+  return h(val);
 }
 
 function compileFunction(source) {


But I still get a 'badmatch' crash:

** Reason for termination == 
** {{badmatch,<<"Cannot encode 'Function' value as JSON">>},
    [{couch_query_servers,'-rereduce/3-fun-0-',3},
     {lists,zipwith,3},
     {couch_query_servers,rereduce,3},
     {couch_view_group,'-init_group/4-fun-0-',4},
     {couch_btree,'-write_node/3-lc$^0/1-0-',3},
     {couch_btree,write_node,3},
     {couch_btree,complete_root,2},
     {couch_btree,query_modify,4}]}

Given that you've closed this ticket a second time, I'll defer to your judgement that you
do not think this should be fixed, and leave it closed.

> Ungraceful behaviour if view returns a function
> -----------------------------------------------
>
>                 Key: COUCHDB-354
>                 URL: https://issues.apache.org/jira/browse/COUCHDB-354
>             Project: CouchDB
>          Issue Type: Improvement
>          Components: JavaScript View Server
>         Environment: {"couchdb":"Welcome","version":"0.10.0a776990"}
>            Reporter: Brian Candler
>            Priority: Minor
>         Attachments: barf.log, test_reduce_barf.rb
>
>
> If a typo in a view definition causes it to return a function instead of a normal value:
> * the view server crashes
> * you get a huge Erlang barf in the log with worrying messages like "OS process timed
out" and "brutal kill"
> I got all this just by accidentally writing "vs.shift" instead of "vs.shift()"
> The fundamental problem is that a function cannot be serialised in toJSON, but the error
message is not helpful:
> OS Process Log Message: Error converting object to JSON: TypeError: {Array:function (v)
{var ... snip loads ... "Object"] is not a function
> When actually the problem is that the object I was trying to convert *was* a function
:-)
> I will attach some code which replicates this, and the barf generated.
> Of course this is entirely down to user error in an invalid map/reduce function. However
there's already a clean error for 'undefined', maybe this could be done for 'function' too
(or indeed any non-serialisable entity)

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message