Skip to content

Commit

Permalink
Make 404 responses compatible with CouchDB API
Browse files Browse the repository at this point in the history
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"
  • Loading branch information
Miroslav Bajtoš committed Mar 13, 2014
1 parent 234deb4 commit dabf5e1
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down

0 comments on commit dabf5e1

Please sign in to comment.