question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throw Error objects instead of strings

See original GitHub issue

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:closed
  • Created 2 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
101arrowzcommented, May 12, 2021

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.

1reaction
101arrowzcommented, May 6, 2021

In TypeScript, there is an enum type that compiles to pure numbers. So it could be done like in lower level languages:

enum InflateError {
  InvalidDistance,
  InvalidLength,
  UnexpectedEOF
}

try {
  inflateSync(something);
} catch (e) {
  if (e == InflateError.InvalidDistance) {
    // handle
  }
}

On reconsideration maybe it’s best just to use the system you suggested. I’ll look into implementing this for v0.7.0.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found