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?