From dabf5e1c9a7e53f435b89d9e0fbf8e091e7e40e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Thu, 13 Mar 2014 19:47:44 +0100 Subject: [PATCH] Make 404 responses compatible with CouchDB API The CouchDB REST API returns always `"error": "not_found"` in the body of a 404 response: http://couchdb-13.readthedocs.org/en/latest/api-basics/#http-status-codes The npm client depends on the magic string 'not_found' as can be seen in requestDone() in npm-registry-client/lib/request.js. Before this change, npm install of an unknown package was reporting the Sinopia error string and a stack trace of npm. After this change, npm install of an unknown package returns a nice error saying "the package is not in the npm registry, bug the author" --- lib/index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 96b61171..d4177dc7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -64,7 +64,15 @@ module.exports = function(config_hash) { if (err.status && err.status >= 400 && err.status < 600) { if (calls == 1) { res.status(err.status) - res.send({error: err.msg || err.message || 'unknown error'}) + var body = {error: err.msg || err.message || 'unknown error'}; + + // Make 404 responses compliant with CouchDB REST API + if (err.status == 404) { + body.reason = body.error + body.error = 'not_found' + } + + res.send(body) } } else { Logger.logger.error({err: err}, 'unexpected error: @{!err.message}\n@{err.stack}')