1

I follow a simple try … catch pattern for my SQLite queries

try {
    … run query and get result …
}
catch (error) {
    console.log(error);
}

On error, I get a nice stacktrace like this

SqliteError: no such column: 
    at getData (/Users/punkish/Projects/zenodeo/bin/facets.js:8:25)
    at Object.<anonymous> (/Users/punkish/Projects/zenodeo/bin/facets.js:23:1)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47

I've written my own small logging routine so I can embellish the messages with colors (using chalk) and some extra info I find helpful, now I have

try {
    … run query and get result …
}
catch (error) {
    mylogger(error);
}

Except, now I just get the following

SqliteError: no such column: 

And no, the mylogger is not eating/chopping away the extra bits. I've added the following to it

if (typeof error === 'object') {
    log('type: object');
    log(`error: ${JSON.stringify(error)}`)
}

and I get

type: object
error: SqliteError: no such column: 

Seems like the stacktrace is streaming, and all of it doesn't go to mylogger. In any case, how can I get all of it?

1 Answer 1

3

I am using Winston and I encountered the same issue before. I'm using .constructor instead of instanceOf to determine the data type which might be the best practice. But my code here might be of some value to you:

winston​.​format​.​printf​(​info​ ​=>​ {
        ​if​ (​info​.​message​.​constructor​ ​===​ ​Object​ ​||​ ​Error​) {
            ​if​ (​info​.​stack​ ​===​ ​undefined​) {
                ​return​ ​`​${​info​.​timestamp​}​ [​${​info​.​label​}​] ​${​info​.​level​}​: ​${​JSON​.​stringify​(​info​.​message​, ​null​, ​1​)​}​`​;
            } ​else​ {
                ​return​ ​`​${​info​.​timestamp​}​ [​${​info​.​label​}​] ​${​info​.​level​}​: ​${​info​.​message​}​ Stack: ​${​info​.​stack​}​`​;
            }
        } ​else​ {
            ​return​ ​`​${​info​.​timestamp​}​ [​${​info​.​label​}​] ​${​info​.​level​}​: ​${​info​.​message​}​`​;
        }

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

2 Comments

your suggestion actually works, thanks! I still don't understand why my way was not working. I mean, why was the error object not printing out fully. Anyway, your way works and I am happy with that.
You're welcome. I think it's because you're using typeof which can be unreliable sometimes.

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.