Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Commit

Permalink
Better error handling for static file delivery (#443)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
koraa authored and trieloff committed Feb 21, 2019
1 parent 7104479 commit 90dd9a2
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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));
}

/**
Expand Down

0 comments on commit 90dd9a2

Please sign in to comment.