The console.error() (or console.log()) statement has some nice features like variable-length argument lists that print out objects (and arrays):
const spaghettio = {sauce: 'arrabiata', cheese: 'gorgonzola'}
console.log(`Uh oh,`, spaghettio)
The object is printed out in a way I can expand the keys, which is very handy for large objects:
But if I want to throw a real error, I don't have those options. So I usually end up doing this:
console.error(`Uh oh`, spaghettio)
throw new Error(`Uh oh: ${spaghettio}`)
The Error version generally does a crappy job of formatting the objects and arrays:
Uh oh: [object Object]. I can do
throw new Error(`Uh oh: ${JSON.stringify(spaghettio)}`)
But no matter how I slice it it's not as nice as what console.error does.
Is there a better way to avoid repeating myself here while still printing out objects to the console?
