From 728b0ab2f3b92b5972a9065c1884ea46322e39f5 Mon Sep 17 00:00:00 2001 From: Brian Reavis Date: Sat, 15 Aug 2015 22:42:09 -0600 Subject: [PATCH] Allow providers to customize status code on error. --- lib/TileRequestHandler.js | 2 +- test/TileRequestHandler.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/TileRequestHandler.js b/lib/TileRequestHandler.js index 3bd6790..7cd2dec 100755 --- a/lib/TileRequestHandler.js +++ b/lib/TileRequestHandler.js @@ -185,7 +185,7 @@ TileRequestHandler.prototype['GET'] = function(server, req, callback) { return done(404, BUFFER_NOPROVIDER, {}); } self.provider.serve(server, req, function(err, buffer, headers) { - if (err) return done(500, new Buffer(err.message || err, 'utf8'), {}); + if (err) return done(err.statusCode || 500, new Buffer(err.message || err, 'utf8'), {}); renderedHeaders = headers || {}; renderedBuffer = buffer; setImmediate(callback); diff --git a/test/TileRequestHandler.js b/test/TileRequestHandler.js index f29ee4d..b5b9e26 100755 --- a/test/TileRequestHandler.js +++ b/test/TileRequestHandler.js @@ -406,6 +406,22 @@ describe('TileRequestHandler', function() { done(); }); }); + it('should return statusCode attached to provider error if available', function(done) { + var mockServer = new TileServer(); + var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png'); + var handler = new TileRequestHandler(); + handler.use({ + serve: function(server, req, callback) { + var err = new Error('My error'); + err.statusCode = 999; + callback(err); + } + }); + handler.GET(mockServer, mockRequest, function(status, buffer, headers) { + assert.equal(status, 999); + done(); + }); + }); it('should skip transforms if provider fails', function(done) { var mockServer = new TileServer(); var mockRequest = TileRequest.parse('/layer/1/2/3/tile.png');