Arlolra has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/252607

Change subject: T115327: Log errors passed along in express
......................................................................

T115327: Log errors passed along in express

 * Use our logger on the chained errors from helpers, like the
   body-parser.

 * Also, prefer res.send when passing data.

 * Maybe the finalhandler deserves a spot in package.json

Change-Id: Ibc870e15c58b45f2cf9428d8d5541480c8b8e469
---
M lib/api/ParsoidService.js
M lib/api/apiUtils.js
M lib/api/routes.js
3 files changed, 21 insertions(+), 22 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid 
refs/changes/07/252607/1

diff --git a/lib/api/ParsoidService.js b/lib/api/ParsoidService.js
index a1c962b..d44cb08 100644
--- a/lib/api/ParsoidService.js
+++ b/lib/api/ParsoidService.js
@@ -6,6 +6,7 @@
 
 // global includes
 var express = require('express');
+var finalHandler = require('express/node_modules/finalhandler');  // Suspect!
 var compression = require('compression');
 var hbs = require('express-handlebars');
 var favicon = require('serve-favicon');
@@ -111,6 +112,22 @@
                next();
        });
 
+       // Log unhandleds errors passed along with our logger.
+       var logError = function(err, req, res) {
+               var logger = res.locals.env ? res.locals.env.logger : 
processLogger;
+               var args = ['warning', req.method, req.originalUrl, err];
+               if (err.type === 'entity.too.large') {
+                       // Add the expected length of the stream.
+                       args.push('expected: ' + err.expected);
+               }
+               logger.log.apply(logger, args);
+       };
+
+       app.use(function(err, req, res, next) {
+               var done = finalHandler(req, res, { onerror: logError });
+               done(err);
+       });
+
        // Catch errors
        app.on('error', function(err) {
                if (err.errno === 'EADDRINUSE') {
diff --git a/lib/api/apiUtils.js b/lib/api/apiUtils.js
index e62b0f7..e39901b 100644
--- a/lib/api/apiUtils.js
+++ b/lib/api/apiUtils.js
@@ -67,24 +67,6 @@
 };
 
 /**
- * End response, but only if response hasn't been sent.
- *
- * @method
- * @param {Response} res The response object from our routing function.
- * @param {MWParserEnvironment} env
- * @param {String|Buffer} [out]
- */
-apiUtils.endResponse = function(res, env, out) {
-       if (env.responseSent) {
-               return;
-       } else {
-               env.responseSent = true;
-               res.end(out);
-               env.log("end/response");
-       }
-};
-
-/**
  * Send response, but only if response hasn't been sent.
  *
  * @method
@@ -495,7 +477,7 @@
                        innerXML: res.locals.bodyOnly && res.locals.apiVersion 
> 2,
                }).str;
                apiUtils.setHeader(res, env, 'content-type', 
apiUtils.HTML_CONTENT_TYPE);
-               apiUtils.endResponse(res, env, output);
+               apiUtils.sendResponse(res, env, output);
        }
 
        if (timer) {
diff --git a/lib/api/routes.js b/lib/api/routes.js
index 087ea3e..0c3e0d3 100644
--- a/lib/api/routes.js
+++ b/lib/api/routes.js
@@ -126,7 +126,7 @@
                                        apiUtils.sendResponse(res, env, 
logData.fullMsg(), logData.flatLogObject().code || 500);
                                }).catch(function(e) {
                                        console.error(e.stack || e);
-                                       res.end(e.stack || e);
+                                       res.end();
                                }).nodify(callback);
                        }
                        return Promise.resolve().nodify(callback);
@@ -173,7 +173,7 @@
 
        // robots.txt: no indexing.
        routes.robots = function(req, res) {
-               res.end("User-agent: *\nDisallow: /\n");
+               res.send("User-agent: *\nDisallow: /\n");
        };
 
        // Return Parsoid version based on package.json + git sha1 if available
@@ -434,7 +434,7 @@
                                        .then(apiUtils.endHtml2wt)
                                        .then(function(output) {
                                apiUtils.setHeader(res, env, 'content-type', 
apiUtils.WIKITEXT_CONTENT_TYPE);
-                               apiUtils.endResponse(res, env, output);
+                               apiUtils.sendResponse(res, env, output);
                        });
                        return apiUtils.cpuTimeout(p, res)
                                .catch(apiUtils.timeoutResp.bind(null, env));

-- 
To view, visit https://gerrit.wikimedia.org/r/252607
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibc870e15c58b45f2cf9428d8d5541480c8b8e469
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Arlolra <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to