9

I want to log that an error occurred without exiting from the program, so i enclose the code that might throw an error in a try-catch block like such

try
{
    let query = parseStatement(input); // might throw an error
    console.log(query);
}
catch (err)
{
    console.error(err); // logs the entire error with stack trace
}
finally
{
    this.prompt();
}

The output I would like to obtain is the error's message without the whole stack trace. Something along the lines of:

Error: Missing semicolon at the end of statement: <statement>

and not

Error: Missing semicolon at the end of statement: <statement>
    at error (/home/nic/dev/nodedb/src/errors.js:5:11)
    at Function.tokenize (/home/nic/dev/nodedb/src/tokens.js:53:13)
    at parseStatement (/home/nic/dev/nodedb/src/sql.js:35:24)
    at Shell.onData (/home/nic/dev/nodedb/src/shell.js:49:25)
    at ReadStream.emit (node:events:394:28)
    at addChunk (node:internal/streams/readable:312:12)
    at readableAddChunk (node:internal/streams/readable:287:9)
    at ReadStream.Readable.push (node:internal/streams/readable:226:10)
    at TTY.onStreamRead (node:internal/stream_base_commons:190:23)
2
  • I’m voting to close this question because it doesn't offer a real solution to the problem of logging without a stack trace; the accepted answer is wrong, and was written by the author himself. Commented Dec 14, 2022 at 8:44
  • I don't know, but to me this seems a bug in Firefox ? -- I sometimes see the stack trace expanded, and can not collapse it. Other times with the same code I see the stack trace collapsed, and can expand it (e.g. after a browser re-start) -- I think console.error should not display the stack trace, but may provide it "collapsed" (i.e. you would see the stack trace when you "expand" it). Commented Nov 29, 2023 at 13:57

3 Answers 3

4

From the documentation:

The Error object has a .message property, so instead of printing the whole error, just

console.error(error.message);
Sign up to request clarification or add additional context in comments.

3 Comments

@LawrenceCherone There was no such question/answer on SO, so i thought someone else might stumble upon the need to log an error without the whole stack trace. I think this question/answer might save someone else the time to read through the documentation
Even a plain text will have stack-trace. Not for itself. But for where you write the console.error().
For me in nodejs this does not work. Even with just error.message it logs the call stack.
2

With Node, you can simply use

process.exit()

If you want to log a message - use console.log/error before exiting the process.

If you just want to remove stacktrace then you may do error.stack = "" before throwing the error like:-

const error = new Error("Your message");
error.stack = ""
throw error

Comments

0

console.error(error.message)

Will log but you might not want runtime to continue

which is why process.exit(1) is important to terminate the current runtime

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.