Home > OS >  How to throw an exception with indent?
How to throw an exception with indent?

Time:09-29

Is there any way to throw javascript errors and force it to be indented? Instead of 'Error' to be ' Error'. If not, what can I use instead, that will surely exit my node.js process. Thanks.

CodePudding user response:

You can't modify the way node displays an uncaught error. But, you can catch the error and choose to display it however you want, before exiting your program.

class SpecialError extends Error {}

function main() {
  // ...
  throw new SpecialError('Whoops!')
}

// This is at the very end, so when the catch finishes,
// there's nothing left to execute, and the program ends.
try {
  main()
} catch (err) {
  if (!(err instanceof SpecialError)) throw err
  console.error([
    `      Error: ${err.message}`, // Lots of indentation
    ...err.stack.split('\n').slice(1)
  ].join('\n'))

  // Makes the program exit with status code 1. See here: https://nodejs.org/api/process.html#process_process_exitcode
  // Uncomment this when you're in node.
  // process.exitCode = 1;
}

CodePudding user response:

I don't know if I realy understood your question, but did you try something like this?

throw new Error('\tSome error message.');

  • Related