From 90dd9a265548ed8feafffe2bb84dabcf4310ac1d Mon Sep 17 00:00:00 2001 From: Karolin Varner Date: Thu, 21 Feb 2019 07:30:02 -0800 Subject: [PATCH] Better error handling for static file delivery (#443) * Improve error messages * Simplify error handling in deliverPlain() rqerror.message and rqerror.response.body.toString() seem to yield the same data, except that rqerror.message is of type Buffer, so it's contents are printed in the stream returned via html as a list of bytes (which is not easy to read) * increase test coverage * lint --- static.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/static.js b/static.js index 69397e4d..39932534 100644 --- a/static.js +++ b/static.js @@ -18,10 +18,21 @@ const mime = require('mime-types'); // one megabyte openwhisk limit + 20% Base64 inflation + safety padding const REDIRECT_LIMIT = 750000; +function errorCode(code) { + switch (code) { + case 400: + return 404; + default: + return code; + } +} + function error(message, code = 500) { + // treat + const statusCode = errorCode(code); console.error('delivering error', message, code); return { - statusCode: code, + statusCode, headers: { 'Content-Type': 'text/html', 'X-Static': 'Raw/Static', @@ -129,13 +140,7 @@ function deliverPlain(owner, repo, ref, entry, root) { 'X-Static': 'Raw/Static', }, }; - }).catch((rqerror) => { - console.error('REQUEST FAILED', rqerror.response.body.toString()); - if (rqerror.statusCode === 404) { - return error(rqerror.response.body.toString(), 404); - } - return error(rqerror.message, rqerror.statusCode); - }); + }).catch(rqerror => error(rqerror.response.body.toString(), rqerror.statusCode)); } /**