Skip to content

Commit

Permalink
Refactoring errHandler() to be in lexical scope with new arity as `…
Browse files Browse the repository at this point in the history
…utility.error()`
  • Loading branch information
avoidwork committed Dec 16, 2017
1 parent d6a6d0a commit 1231898
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
23 changes: 3 additions & 20 deletions lib/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,23 +381,6 @@ class Tenso extends HTTPMethods {
compression = "",
header, compressionMethod;

const errHandler = e => {
try {
if (this.config.http2 === false) {
res.statusCode = 500;
res.writeHead(500, headers || rheaders);
res.end(http.STATUS_CODES[500]);
} else {
req.server.router.http2Send(req, res, http.STATUS_CODES[500], 500, headers || rheaders);
}
} catch (err) {
void 0;
}

this.log(e.stack, "warn");
reject(e);
};

if (res.headersSent === false) {
res.statusCode = status;

Expand Down Expand Up @@ -443,11 +426,11 @@ class Tenso extends HTTPMethods {
headers["transfer-encoding"] = "chunked";
delete headers["content-length"];
res.writeHead(status, headers);
body.pipe(zlib[compressionMethod]()).on("error", errHandler).on("close", () => resolve(true)).pipe(res);
body.pipe(zlib[compressionMethod]()).on("error", e => utility.error(req, res, this, resolve, reject, e, headers || rheaders)).on("close", () => resolve(true)).pipe(res);
} else {
zlib[compressionMethod.replace("create", "").toLowerCase()](body, (e, data) => {
if (e !== null) {
errHandler(e);
utility.error(req, res, this, resolve, reject, e, headers || rheaders);
} else {
headers["content-length"] = data.length;

Expand Down Expand Up @@ -482,7 +465,7 @@ class Tenso extends HTTPMethods {
res.writeHead(status, headers);

if (pipe === true) {
body.on("error", errHandler).on("close", () => resolve(true)).pipe(res);
body.on("error", e => utility.error(req, res, this, resolve, reject, e, headers || rheaders)).on("close", () => resolve(true)).pipe(res);
} else {
if (req.range !== void 0) {
res.end(Buffer.from(body.toString()).slice(req.range.start, req.range.end + 1).toString());
Expand Down
19 changes: 19 additions & 0 deletions lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,24 @@ function bootstrap (obj) {
return obj;
}

function error (req, res, obj, resolve, reject, e, headers) {
try {
if (obj.config.http2 === false) {
res.statusCode = 500;
res.writeHead(500, headers);
res.end(http.STATUS_CODES[500]);
} else {
obj.router.http2Send(req, res, http.STATUS_CODES[500], 500, headers);
}

obj.log(e.stack, "warn");
} catch (err) {
void 0;
}

reject(e);
}

function hypermedia (server, req, rep, headers) {
const collection = req.parsed.pathname,
links = [];
Expand Down Expand Up @@ -805,6 +823,7 @@ module.exports = {
bootstrap: bootstrap,
capitalize: capitalize,
clone: clone,
error: error,
explode: explode,
hypermedia: hypermedia
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tenso",
"description": "Tensō is an elastic REST API gateway for node.js",
"version": "6.2.4",
"version": "6.2.5",
"homepage": "http://avoidwork.github.io/tenso",
"author": "Jason Mulligan <[email protected]>",
"repository": {
Expand Down Expand Up @@ -29,7 +29,7 @@
"express-session": "^1.15.6",
"keysort": "^1.0.2",
"lusca": "^1.5.2",
"mimetype": "0.0.8",
"mimetype": "^0.0.8",
"passport": "^0.4.0",
"passport-facebook": "^2.1.1",
"passport-google": "^0.3.0",
Expand All @@ -40,7 +40,7 @@
"passport-local": "^1.0.0",
"passport-oauth2": "^1.4.0",
"passport-saml": "^0.31.0",
"passport-slack": "0.0.7",
"passport-slack": "^0.0.7",
"passport-twitter": "^1.0.4",
"precise": "^1.1.0",
"retsu": "^3.0.1",
Expand Down

0 comments on commit 1231898

Please sign in to comment.