From 39ace2d0fa0d3700566bbc453b6518b0a6ff83c3 Mon Sep 17 00:00:00 2001 From: John Christopher Anderson Date: Wed, 5 Aug 2009 18:11:45 +0000 Subject: proper handling of 406 Not Acceptable errors in list and show functions git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@801345 13f79535-47bb-0310-9956-ffa450edef68 --- share/server/render.js | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'share/server') diff --git a/share/server/render.js b/share/server/render.js index 51472672..1a9fc5a5 100644 --- a/share/server/render.js +++ b/share/server/render.js @@ -140,18 +140,21 @@ function runProvides(req) { 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 renderError(m) { } 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) { -- cgit v1.2.3