couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jch...@apache.org
Subject svn commit: r801345 - in /couchdb/trunk: share/server/render.js share/www/script/test/show_documents.js src/couchdb/couch_httpd.erl
Date Wed, 05 Aug 2009 18:11:45 GMT
Author: jchris
Date: Wed Aug  5 18:11:45 2009
New Revision: 801345

URL: http://svn.apache.org/viewvc?rev=801345&view=rev
Log:
proper handling of 406 Not Acceptable errors in list and show functions

Modified:
    couchdb/trunk/share/server/render.js
    couchdb/trunk/share/www/script/test/show_documents.js
    couchdb/trunk/src/couchdb/couch_httpd.erl

Modified: couchdb/trunk/share/server/render.js
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/server/render.js?rev=801345&r1=801344&r2=801345&view=diff
==============================================================================
--- couchdb/trunk/share/server/render.js (original)
+++ couchdb/trunk/share/server/render.js Wed Aug  5 18:11:45 2009
@@ -140,18 +140,21 @@
     responseContentType = mimesByKey[bestKey][0];
   }
   
-  for (var i=0; i < mimeFuns.length; i++) {
-    if (mimeFuns[i][0] == bestKey) {
-      bestFun = mimeFuns[i][1];
-      break;
-    }
+  if (bestKey) {
+    for (var i=0; i < mimeFuns.length; i++) {
+      if (mimeFuns[i][0] == bestKey) {
+        bestFun = mimeFuns[i][1];
+        break;
+      }
+    };
   };
-
+  
   if (bestFun) {
     // log("responding with: "+bestKey);
     return bestFun();
   } else {
-    throw({code:406, body:"Not Acceptable: "+accept||bestKey});
+    var supportedTypes = mimeFuns.map(function(mf) {return mimesByKey[mf[0]].join(', ') ||
mf[0]});
+    throw({error:"not_acceptable", reason:"Content-Type "+(accept||bestKey)+" not supported,
try one of: "+supportedTypes.join(', ')});
   }
 };
 
@@ -248,13 +251,15 @@
 }
 
 function respondError(e, funSrc, htmlErrors) {
-  var logMessage = "function raised error: "+e.toString();
-  log(logMessage);
-  log("stacktrace: "+e.stack);
-  var errorMessage = htmlErrors ? htmlRenderError(e, funSrc) : logMessage;
-  respond({
-    error:"render_error",
-    reason:errorMessage});
+  if (e.error && e.reason) {
+    respond(e);
+  } else {
+    var logMessage = "function raised error: "+e.toString();
+    log(logMessage);
+    log("stacktrace: "+e.stack);
+    var errorMessage = htmlErrors ? htmlRenderError(e, funSrc) : logMessage;
+    renderError(errorMessage);
+  }
 }
 
 function escapeHTML(string) {

Modified: couchdb/trunk/share/www/script/test/show_documents.js
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/www/script/test/show_documents.js?rev=801345&r1=801344&r2=801345&view=diff
==============================================================================
--- couchdb/trunk/share/www/script/test/show_documents.js (original)
+++ couchdb/trunk/share/www/script/test/show_documents.js Wed Aug  5 18:11:45 2009
@@ -318,13 +318,12 @@
   // test the provides mime matcher without a match
   xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/provides/"+docid, {
    headers: {
-     "Accept": 'text/html,application/atom+xml; q=0.9'
+     "Accept": 'text/monkeys'
    }
   });
-  var ct = xhr.getResponseHeader("Content-Type");
-  T(/charset=utf-8/.test(ct))
-  T(/text\/html/.test(ct))
-  T(xhr.responseText == "Ha ha, you said \"plankton\".");
+  var rs = JSON.parse(xhr.responseText);
+  T(rs.error == "not_acceptable")
+
 
   // should fallback on the first one
   xhr = CouchDB.request("GET", "/test_suite_db/_design/template/_show/provides/"+docid, {

Modified: couchdb/trunk/src/couchdb/couch_httpd.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_httpd.erl?rev=801345&r1=801344&r2=801345&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_httpd.erl (original)
+++ couchdb/trunk/src/couchdb/couch_httpd.erl Wed Aug  5 18:11:45 2009
@@ -452,6 +452,8 @@
     {404, <<"not_found">>, <<"missing">>};
 error_info({not_found, Reason}) ->
     {404, <<"not_found">>, Reason};
+error_info({not_acceptable, Reason}) ->
+    {406, <<"not_acceptable">>, Reason};
 error_info(conflict) ->
     {409, <<"conflict">>, <<"Document update conflict.">>};
 error_info({forbidden, Msg}) ->



Mime
View raw message