summaryrefslogtreecommitdiff
path: root/share
diff options
context:
space:
mode:
Diffstat (limited to 'share')
-rw-r--r--share/server/main.js45
-rw-r--r--share/www/script/couch_tests.js15
2 files changed, 40 insertions, 20 deletions
diff --git a/share/server/main.js b/share/server/main.js
index 8e1f91cd..286fd9c0 100644
--- a/share/server/main.js
+++ b/share/server/main.js
@@ -29,7 +29,12 @@ sum = function(values) {
}
log = function(message) {
- print(toJSON({log: toJSON(message)}));
+ if (typeof message == "undefined") {
+ message = "Error: attempting to log message of 'undefined'.";
+ } else if (typeof message != "string") {
+ message = toJSON(message);
+ }
+ print(toJSON({log: message}));
}
// mimeparse.js
@@ -135,7 +140,7 @@ var Mimeparse = (function() {
// this function provides a shortcut for managing responses by Accept header
respondWith = function(req, responders) {
- var accept = req.headers["Accept"];
+ var bestKey = null, accept = req.headers["Accept"];
if (accept) {
var provides = [];
for (key in responders) {
@@ -144,19 +149,17 @@ respondWith = function(req, responders) {
}
}
var bestMime = Mimeparse.bestMatch(provides, accept);
- var bestKey = keysByMime[bestMime];
- var rFunc = responders[bestKey];
- if (rFunc) {
- var resp = maybeWrapResponse(rFunc());
- resp["headers"] = resp["headers"] || {};
- resp["headers"]["Content-Type"] = bestMime;
- return resp;
- }
+ bestKey = keysByMime[bestMime];
+ }
+ var rFunc = responders[bestKey || responders.fallback || "html"];
+ if (rFunc) {
+ var resp = maybeWrapResponse(rFunc());
+ resp["headers"] = resp["headers"] || {};
+ resp["headers"]["Content-Type"] = bestMime;
+ respond(resp);
+ } else {
+ throw({code:406, body:"Not Acceptable: "+accept});
}
- if (responders.fallback) {
- return responders[responders.fallback]();
- }
- throw({code:406, body:"Not Acceptable: "+accept});
}
// whoever registers last wins.
@@ -378,20 +381,30 @@ function maybeWrapResponse(resp) {
}
};
+var responseSent;
function runRenderFunction(renderFun, args) {
+ responseSent = false;
try {
var resp = renderFun.apply(null, args);
- respond(maybeWrapResponse(resp));
+ if (!responseSent) {
+ if (resp) {
+ respond(maybeWrapResponse(resp));
+ } else {
+ respond({error:"render_error",reason:"undefined response from render function"});
+ }
+ }
} catch(e) {
log("function raised error: "+e.toString());
log("stacktrace: "+e.stack);
+ respond({error:"render_error",reason:e});
}
};
// prints the object as JSON, and rescues and logs any toJSON() related errors
function respond(obj) {
+ responseSent = true;
try {
- print(toJSON(obj));
+ print(toJSON(obj));
} catch(e) {
log("Error converting object to JSON: " + e.toString());
}
diff --git a/share/www/script/couch_tests.js b/share/www/script/couch_tests.js
index 01f94927..8f76ff06 100644
--- a/share/www/script/couch_tests.js
+++ b/share/www/script/couch_tests.js
@@ -2307,10 +2307,12 @@ var tests = {
_id:"_design/template",
language: "javascript",
shows: {
- "hello" : stringFun(function() {
- return {
- body : "Hello World"
- };
+ "hello" : stringFun(function(doc) {
+ if (doc) {
+ return "Hello World";
+ } else {
+ return "Empty World";
+ }
}),
"just-name" : stringFun(function(doc, req) {
return {
@@ -2410,6 +2412,11 @@ var tests = {
// hello template world
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/hello/"+docid);
T(xhr.responseText == "Hello World");
+
+ // hello template world (no docid)
+ xhr = CouchDB.request("GET", "/test_suite_db/_show/template/hello");
+ T(xhr.responseText == "Empty World");
+
// show with doc
xhr = CouchDB.request("GET", "/test_suite_db/_show/template/just-name/"+docid);