Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of object is node-postgres Error? Why node's console.log and JSON.stringify handle it differently?

console.log outputs it like this,

{ [error: syntax error at or near "step"]
  length: 86,
  name: 'error',
  severity: 'ERROR',
  code: '42601',
  detail: undefined,
  hint: undefined,
  position: '62',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  file: 'scan.l',
  line: '1001',
  routine: 'scanner_yyerror' }

but JSON.stringify doesn't sees the narrative part of an error,

{"length":86,"name":"error","severity":"ERROR","code":"42601","position":"62","file":"scan.l","line":"1001","routine":"scanner_yyerror"}

I can't figure out how to get this "error: column "undefined" does not exist" reading wikies (https://github.com/brianc/node-postgres/wiki/Error-handling, http://nodejs.ru/doc/v0.4.x/stdio.html#console.log)

The code is like this,

   client.query(selstring, function(err, result) {
   if(err){
     res.send(JSON.stringify(err));
     console.log(err);
   }else{

thanks

UPDATE: err.toString() shows error: syntax error at or near "step"

like image 706
fedd Avatar asked Feb 27 '13 13:02

fedd


1 Answers

Because the pg error is a subclass of the standard Js Error() obj and therefore the base error "description" is accessible via err.message...

console.log(new Error("hello world"));
[Error: hello world]
console.log(new Error("hello world").message);
hello world

You can subclass the standard Error object for your own error objs by appending the Error.prototype

See Kevin's post How do I create a custom Error in JavaScript?

like image 161
Paul Coleman Avatar answered Sep 29 '22 21:09

Paul Coleman