0

In the code below, I'm causing an error (for illustration) by attempting to connect to a database that doesn't exist on the SQL Server.

const sql = require("msnodesqlv8");
const express = require("express");
const app = express();

// Start server and listen on http://localhost:8081/
const server = app.listen(8081, function () {
    console.log("Server listening on port: %s", server.address().port)
});

const connectionString = "Server=.;Database=DatabaseThatDoesNotExist;Trusted_Connection=Yes;Driver={SQL Server Native Client 11.0}";
const sql = "SELECT * FROM dbo.Table1";

app.get('/', function (req, res)
{
    sql.query(connectionString, sql, (err, recordset) => {
        if(err)
        {
            console.log(err);
            res.status(500).send(err);
            return;
        }

        // else
        res.json(recordset);
    });
})

The result of console.log(err); lists the error objects with four properties (code, message, sqlstate and stack):

Array(2) [Error: [Microsoft][SQL Server Native Client 11.0][…, Error: [Microsoft][SQL Server Native Client 11.0][…]
codefile.js:33
length:2
__proto__:Array(0) [, …]
0:Error: [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'DOMAIN\USER'.
code:18456
message:"[Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'DOMAIN\USER'."
sqlstate:"28000"
stack:"Error: [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'DOMAIN\USER'."
__proto__:Object {constructor: , name: "Error", message: "", …}
1:Error: [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "MyDatabase" requested by the login. The login failed.
code:4060
message:"[Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "MyDatabase" requested by the login. The login failed."
sqlstate:"42000"
stack:"Error: [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "MyDatabase" requested by the login. The login failed."
__proto__:Object {constructor: , name: "Error", message: "", …}

However, the result of res.status(500).send(err); (seen in the client) only contains two properties (sqlstate and code):

[
    {
        "sqlstate": "28000",
        "code": 18456
    },
    {
        "sqlstate": "42000",
        "code": 4060
    }
]

How do I get express to return the message and stack properties also?

John
  • 1,043
  • 15
  • 20

1 Answers1

0

The problem is that Error does not have enumerable fields and therefore message and stack don't get serialized to JSON.

See this question for ways to serialize the error object.

A quick solution for your case: res.status(500).send({message: err.message, stack: err.stack});

But this is of course not scalable. A better approach would be to configure the json replacer for express:

app.set('json replacer', replaceErrors);

And use the replacer implementation from the linked question excellent answer by @jonathan-lonowski:

function replaceErrors(key, value) {
  if (value instanceof Error) {
    var error = {};

    Object.getOwnPropertyNames(value).forEach(function (key) {
        error[key] = value[key];
    });

    return error;
  }
  return value;
}
daramasala
  • 3,040
  • 2
  • 26
  • 33