From 169ee1be21f39c1a59db49af619e5b26c9ace0c9 Mon Sep 17 00:00:00 2001 From: Timo Saikkonen Date: Thu, 25 Dec 2014 13:21:20 +0800 Subject: [PATCH] #4: Return a 404 if file doesn't exist Signed-off-by: Timo Saikkonen --- lib/storage-handler.js | 7 +++++++ lib/storage-service.js | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/lib/storage-handler.js b/lib/storage-handler.js index f56c669..5a75437 100644 --- a/lib/storage-handler.js +++ b/lib/storage-handler.js @@ -118,9 +118,16 @@ exports.download = function (provider, req, res, container, file, cb) { container: container || req && req.params.container, remote: file || req && req.params.file }); + res.type(file); reader.pipe(res); reader.on('error', function (err) { + if (err.code === 'ENOENT') { + res.type('application/json'); + res.send(404, { error: err }); + return; + } + res.type('application/json'); res.send(500, { error: err }); }); diff --git a/lib/storage-service.js b/lib/storage-service.js index 3f4b755..f71a04a 100644 --- a/lib/storage-service.js +++ b/lib/storage-service.js @@ -92,6 +92,10 @@ StorageService.prototype.destroyContainer = function (container, cb) { */ StorageService.prototype.getContainer = function (container, cb) { return this.client.getContainer(container, function (err, container) { + if (err && err.code === 'ENOENT') { + err.statusCode = err.status = 404; + return cb(err); + } return cb(err, map(container)); }); };