Throw Error objects instead of strings
See original GitHub issueIssue Description
The problem
I noticed that there are several places where you throw
a string. It’s considered best practice to only throw Error
objects. When listening to an ondata
event, it’s really unexpected to have a variable called err
that is a string instead of an Error
object.
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (7 by maintainers)
Top Results From Across the Web
javascript - Throwing strings instead of Errors - Stack Overflow
A string is not an error object, and does not convey any useful debugging information. Devtools rely on that, such as the file...
Read more >What to throw? Error or bare string dilemma - Medium
Just creating a new Error object has no special effect. To interrupt code execution ... How throwing strings is better than throwing Errors....
Read more >throw - JavaScript - MDN Web Docs - Mozilla
Throw an object You can specify an object when you throw an exception. You can then reference the object's properties in the catch...
Read more >Difference between throw Error('msg') and throw new Error ...
throw Error () is like a Javascript string, a number, a boolean, or an object. It returns specific errors as defined in the...
Read more >Best Practices for exceptions - .NET - Microsoft Learn
Throw exceptions instead of returning an error code ... Exception(String, Exception), which accepts a string message and an inner exception.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve managed to implement errors with codes in a very lightweight fashion; it’s even smaller than before. I will publish this in v0.7.0, hopefully in a few days.
In TypeScript, there is an
enum
type that compiles to pure numbers. So it could be done like in lower level languages:On reconsideration maybe it’s best just to use the system you suggested. I’ll look into implementing this for v0.7.0.