javascript - Strange unnamed property in JSON error object -
i'm using pg-promise in node.js console script.
node version: v0.10.38 pg-promise version: 1.1.4
i got connection error due trivial misconfiguration problem doesn't care. want script alert me rich explanation of problem.
if take error object passed error handler callback , send "as is" console via console.log(), ca see text "no pg_hba.conf entry host "127.0.0.1" in manner can see in labelled "output 1".
but if doesn't seem valid json object , don't know how access property has message.
also, if log result of json.stringifgy() on object, information disappears.
is error of pg-promise implementation? or kind of json hidden attributes , can accessed in way?.
below reproduction of issue:
source code:
var pgplib = require('pg-promise'); var pgp = pgplib({}); var db = pgp({ host: "localhost", database: "__db_name__", user: "__db_user__", password: "__db_password__", }); db.query("select 'foo'").then( function(){ console.log ("works!!"); }, function(err){ console.log("error!!", err); // (output 1) // console.log("error!!", json.stringify(err)); // (output 2) } );
output 1:
error!! { [error: no pg_hba.conf entry host "127.0.0.1", user "reports", database "ruminant", ssl off] name: 'error', length: 143, severity: 'fatal', code: '28000', detail: undefined, hint: undefined, position: undefined, internalposition: undefined, internalquery: undefined, where: undefined, schema: undefined, table: undefined, column: undefined, datatype: undefined, constraint: undefined, file: 'auth.c', line: '493', routine: 'clientauthentication' }
output 2:
error!! {"name":"error","length":143,"severity":"fatal","code":"28000","file":"auth.c","line":"493","routine":"clientauthentication"}
edit:
as trott said (thanks), can obtain string using .tostring() method (or forcing string coercion).
but, anyway, below output doesn't seem valid json me.
i figured out achieve same effect overloading .tostring() method in object's prototype, can't:
var foo = function(){}; object.defineproperty(foo.prototype, "tostring", { enumerable: false, value: function() { return "some hidden data"; } }); var bar = new foo(); console.log("1:", bar, bar.tostring()); bar.otherstuff = "foobar"; console.log("2:", bar, bar.tostring());
outputs:
1: {} hidden data 2: { otherstuff: 'foobar' } hidden data
to error message error
object without other stuff, use .tostring()
.
console.log('error! ', err.tostring());
if use string concatenation, should have same effect:
console.log('error! ' + err);
the latter way seems how pg-promise readme example code handles errors.
i've tested both of these pg-promise
, work, print "error: " part of message, don't need include 'error! ' did.
Comments
Post a Comment