Uploaded image for project: 'CouchDB'
  1. CouchDB
  2. COUCHDB-410

JavaScript errors in validate_doc_update should be handled more gracefully

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 0.9.1, 0.10
    • 1.3
    • Database Core
    • None
    • New Contributors Level (Easy)

    Description

      For example, if I create validate_doc_update:

      function (oldDoc, newDoc, userCtx) {
      doc.foo;
      }

      I am greeted by an "OS process timed out" message. In the logs, all I see is:

      OS Process :: Error converting object to JSON: TypeError: {Array:function (v) {var buf = [];for (var i = 0; i < v.length; i++)

      {buf.push(toJSON(v[i]));}

      return "[" + buf.join(",") + "]";}, Boolean:function (v)

      {return v.toString();}

      , Date:function (v) {var f = function

      {return n < 10 ? "0" + n : n;}

      ;return "\"" + v.getUTCFullYear() + "" + f(v.getUTCMonth() + 1) + "" + f(v.getUTCDate()) + "T" + f(v.getUTCHours()) + ":" + f(v.getUTCMinutes()) + ":" + f(v.getUTCSeconds()) + "Z\"";}, Number:function (v)

      {return isFinite(v) ? v.toString() : "null";}

      , Object:function (v) {if (v === null)

      {return "null";}

      var buf = [];for (var k in v) {if (!v.hasOwnProperty(k) || typeof k !== "string" || v[k] === undefined)

      {continue;}

      buf.push(toJSON(k, val) + ": " + toJSON(v[k]));}return "

      {" + buf.join(",") + "}

      ";}, String:function (v) {if (/["\\\x00-\x1f]/.test(v)) {v = v.replace(/([\x00-\x1f\\"])/g, function (a, b) {var c = subs[b];if (c)

      {return c;}

      c = b.charCodeAt();return "
      u00" + Math.floor(c / 16).toString(16) + (c % 16).toString(16);});}return "\"" + v + "\"";}}[val != null ? val.constructor.name : "Object"] is not a function

      When really the problem is a ReferenceError (I should have used newDoc.foo or oldDoc.foo). The attached patch modifies toJSON() in utils.js so that it converts anything that isn't a String, Array, Date, Object etc. into a String. This makes the error appear in the popup in Futon when saving.

      This isn't necessarily the best thing to do as it modifies the semantics of toJSON(), and also we might want to keep the exception info in the logs, and not propagate it to the user. Perhaps it would be better to modify respond(obj) to produce a better error message in the logs (simply adding something that tried to log obj.toString() would suffice).

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              jasondd Jason Davies
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: