9

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"

8
  • Please post the contents of selstring. Commented Feb 27, 2013 at 13:19
  • i dont think it really matters, but here it is: select max(date)::character varying dt from calendar where11 step=0 . my task is to get the error description. the query is intentionally wrong Commented Feb 27, 2013 at 13:24
  • 1
    Oh right sorry, I misunderstood your question :( What does err.toString() return? Commented Feb 27, 2013 at 13:24
  • 1
    Still not quite sure what you're trying to accomplish. If you want the narrative, you can get it with toString (EDIT: err.message is much cleaner) Commented Feb 27, 2013 at 14:21
  • 1
    A class can overload the toJSON method to decide for itself what will be returned when you call JSON.stringify() on its instances. Here's an example: jsfiddle.net/QmWfq Commented Feb 27, 2013 at 14:33

4 Answers 4

6

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?

Sign up to request clarification or add additional context in comments.

Comments

1

I've been struggling to understand the same thing for a long time now.

Looking at the output of JSON.stringify(err), it appears that the object has an array as an unnamed attribute. How is it even possible to create an object with an anonymous attribute? I once messed with this for quite a while but could find no way to even construct any object that could generate a similar structure from JSON.stringify.

I've found err.message actually works to access the string, but I really don't understand why, as 'message' doesn't appear as an attribute in the stringified version. If 'message' is an attribute of err, why doesn't it show up in the output from stringify? If it isnt, then where is it coming from?

1 Comment

I don't know why people are downvoting this answer. It was actually very helpful. Thank you.
-1

First step put console like

console.log(query.text);

Also refer the link may use

http://dailyjs.com/2011/09/26/heroku/

Comments

-1

The easy fix is to add port parameter into your connection, even if it is the default port. I had the exact same issue. I found out about the solution from this github issue.

var pg_conn_conf = {
  username: 'user',
  pass: 'pass',
  port: 5432,
  host: 'localhost',
  db: 'mydb'
};

There is also another stackoverflow answer that suggests extending the Error.prototype, luckily you don't have to deal with that in this case. But it is a good trick to know.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.