var PSQL = require('cartodb-psql');

function AnalysisStatusBackend() {
}

module.exports = AnalysisStatusBackend;


AnalysisStatusBackend.prototype.getNodeStatus = function (params, callback) {
    var nodeId = params.nodeId;

    var statusQuery = 'SELECT node_id, status, updated_at FROM cdb_analysis_catalog where node_id = \'' + nodeId + '\'';

    var pg = new PSQL(dbParamsFromReqParams(params));
    pg.query(statusQuery, function(err, result) {
        if (err) {
            return callback(err, result);
        }

        result = result || {};

        var rows = result.rows || [];

        return callback(null, rows[0] || {
            node_id: nodeId,
            status: 'unknown'
        });
    }, true); // use read-only transaction
};

function dbParamsFromReqParams(params) {
    var dbParams = {};
    if ( params.dbuser ) {
        dbParams.user = params.dbuser;
    }
    if ( params.dbpassword ) {
        dbParams.pass = params.dbpassword;
    }
    if ( params.dbhost ) {
        dbParams.host = params.dbhost;
    }
    if ( params.dbport ) {
        dbParams.port = params.dbport;
    }
    if ( params.dbname ) {
        dbParams.dbname = params.dbname;
    }
    return dbParams;
}

@@ -242,16 +242,26 @@
 sQuery =
+ %5B%0A       
  'SELECT
@@ -288,17 +288,65 @@
 dated_at
- 
+, last_error_message as error_message',%0A        '
 FROM cdb
@@ -398,16 +398,32 @@
 d + '%5C''
+%0A    %5D.join(' ')
 ;%0A%0A    v
@@ -676,29 +676,28 @@
     
-return callback(null,
+var statusResponse =
  row
@@ -765,16 +765,16 @@
 nknown'%0A
-
         
@@ -774,16 +774,173 @@
        %7D
+;%0A%0A        if (statusResponse.status !== 'failed') %7B%0A            delete statusResponse.error_message;%0A        %7D%0A%0A        return callback(null, statusResponse
 );%0A    %7D

Include error message in node status endpoint