Throwing errors in JavaScript is.....interesting...and often misunderstood.
Thread here 👇
JavaScript has an Error object
So you may think that you only throw errors
But you can throw anything
Even this

What the heck would this even mean? (seriously when would you ever do this?)
Throwing things other than a new Error object is actually rather common.

So if you see something besides an error thrown, it's not necessarily bad, but possibly we can improve this?
The built in Error object has 2 properties:

1. name
2. message

The name defaults to "Error". The message is whatever you pass into the constructor, and when the error happens, the console prints out the message.
But we can also create our own custom Error classes by extending Error. This code customizes the default name property, adds a new statusCode property, while keeping the default behavior by passing a message into the super class constructor
This lets us handle specific errors by type:
Lots of cool things you can do with just errors in ye olde JavaScript. What tips do you have that I missed?
You can follow @josepheames.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.