1

I'm getting an error object back from my SQLite database. When I display it with console.log(err), I get:

{ records: { Error: SQLITE_ERROR: no such table: showcaseUsers errno: 1, code: 'SQLITE_ERROR' } }

Yet when I display it with JSON.stringify(err), I only get:

{"records":{"errno":1,"code":"SQLITE_ERROR"}}

I want to get the error message no such table: showcaseUsers in a string.

The only way I have found how to do this is with this:

const errorText = console.log(data);

But this also outputs the data to the console, which is undesirable.

How can I either (1) stop console.log from outputting its content to the console, or else (2) get the error message in a string some other way?

NOTE: I am in Node at this point and not in a browser, so it doesn't seem that the answer at Capturing javascript console.log? is helpful.

Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • Possible duplicate of [Capturing javascript console.log?](https://stackoverflow.com/questions/11403107/capturing-javascript-console-log) – Marcin Feb 06 '19 at 10:08
  • 1
    "The only way I have found how to do this is with this: const errorText = console.log(data);" remove the console.log? `const errorText=data;` – Mukyuu Feb 06 '19 at 10:10
  • 1
    Possible duplicate of [JSON.stringify missing properties](https://stackoverflow.com/questions/35487441/json-stringify-missing-properties) I think this should help you ;) This looks exactly like your object! – sjahan Feb 06 '19 at 10:13
  • @Mukyuu I can do `const errorText=data` and then output `console.log(errorText)` but errorText is `typeof` is object, not string, length is undefined. – Edward Tanguay Feb 06 '19 at 10:21
  • you will probably find that the additional error string is being held in a _non-enumerable_ property of the error object. You should be able to extract that. – Alnitak Feb 06 '19 at 10:28
  • which precise sqllite module are you using? – Alnitak Feb 06 '19 at 10:29
  • @Alnitak I'm using the npm package `"sqlite3": "^4.0.2"` – Edward Tanguay Feb 06 '19 at 10:33
  • 1
    @EdwardTanguay you might wish to rephrase the question to reflect what the real problem was (i.e. getting the data out of the error object) as opposed to your intended solution (intercepting the console). Notwithstanding you did ask how you could "get the error message in a string in some other way" the rest of the question as written is a classic "XY problem". – Alnitak Feb 06 '19 at 11:07
  • 1
    Thanks, @Alnitak, I rephrased the title so it better reflects the content and answer. – Edward Tanguay Feb 06 '19 at 12:07
  • p.s. you could also have used `err.toString()` to get "`Error: SQLITE_ERROR: no such table: showcaseUsers `" – Alnitak Feb 06 '19 at 12:19

2 Answers2

2

There's no need to intercept the console at all - you will find the entire error message in err.message, albeit still prefixed with "SQLITE_ERROR:"

It's a non-enumerable property of the Error object hence why it doesn't appear in the JSON output:

let sqlite3 = require('sqlite3');

let db = new sqlite3.Database(':memory:');
db.run("UPDATE foo SET bar = 1", (err, res) => {
  console.log(Object.getOwnPropertyDescriptors(err));
  console.log(err.message);
});

with output:

{ stack:
   { value: 'Error: SQLITE_ERROR: no such table: foo',
     writable: true,
     enumerable: false,
     configurable: true },
  message:
   { value: 'SQLITE_ERROR: no such table: foo',
     writable: true,
     enumerable: false,        <<---------
     configurable: true },
  errno:
   { value: 1, writable: true, enumerable: true, configurable: true },
  code:
   { value: 'SQLITE_ERROR',
     writable: true,
     enumerable: true,
     configurable: true } }
SQLITE_ERROR: no such table: foo
Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • thanks for your info " a non-enumerable property of the Error object " I had stayed up too late to find out what weird thing happened with the message of Error Object Now I have more information to dig in more, thanks a lot – Thinh NV Nov 09 '19 at 02:28
-2

Issue is wrongly formatted JSON, which must be key:value pairs. If value in key:value is a string then it should be enclosed in single or double quotes.

{ records: { Error: SQLITE_ERROR: no such table: showcaseUsers errno: 1, code: 'SQLITE_ERROR' } }

In the absence of proper value (value in your case has datatype String), JSON.stringify(err) tries to coerce it to JSON based on colon : as separator between key and value. Available datatypes in JavaScript

Here is a correction, I have just enclosed it in double quotes, and formatted it (formatting is optional, helpful for legibility).

{ 
    records: { 
        Error: "SQLITE_ERROR: no such table: showcaseUsers errno: 1, code: 'SQLITE_ERROR'" 
    } 
}

Actually it should be formatted (see placement of single quotes) like so

{ 
    records: { 
        Error: 'SQLITE_ERROR: no such table: showcaseUsers',
        errno: 1, 
        code: 'SQLITE_ERROR'
    } 
}

Update: It's imperative to store properly formatted data in database in the first place. If you missed it then you are left with manually parsing data you are getting from SQLite.

Helpful resources

Hamza Rashid
  • 1,329
  • 15
  • 22
  • 4
    Right, but I get this object from the SQLITE database so I have to deal with it as it is. How can I access this wrongly formatted portion of the object? – Edward Tanguay Feb 06 '19 at 10:22
  • It's imperative to store properly formatted data in database in the first place. If you missed it then you are left with manually parsing data you are getting from SQLite. – Hamza Rashid Feb 06 '19 at 10:31
  • 1
    This has _nothing whatsoever_ to do with the format of the data in the database. When the OP says he "gets this object from the SQLITE database" he means that it's an object _generated by the database engine_, not one _stored in the database_. – Alnitak Feb 06 '19 at 10:45