Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
What is the actual output of console.log(error) and how do I get the exact same output as a string?

In a Node.js project using Typescript and targeting ES2020, I'm using a custom Error class like this:

class InvalidParamsError extends Error {
}

try {
  throw new InvalidParamsError();
} catch (error) {
  if (error instanceof Error) {
    console.log(error);
    console.log(error.message); // Different output
    console.log(error.stack); // Different output
    console.log(error.toString()); // Different output
  }
}

The output I get in the console is:

InvalidParamsError
    at REPL11:2:13
    at ...

Error
    at REPL11:2:13
    at ...
Error
undefined

How can I get this very same output as a string without having to print it to the console?

error.message, error.stack and error.toString() all return different strings, and none of them show InvalidParamsError as part of the output, showing a generic Error instead. I know I could fix this manually setting the name of the InvalidParamsError in the constructor, but even then I'm unable to get exactly the same output as console.log(error) as a string.

I tried looking at Node.js' console source code but couldn't find any answer.

I'm targeting ES2020 so I don't think this is related with previous Typescript issues when extending built-ins like Error.

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!