Functions should throw exception on invalid args
See original GitHub issueIn general I believe that it’s better when functions called with invalid argument throw early, because such approach leaves less space for bugs.
Question is how far should we go implementing this, considering that we have no builtin facilities like languages like TypeScript have.
For example see #7125.
- I’m sure that more strict conditions (introduced there) are good. There is no explicit type checking, but code is composed with type-checking in mind, and intentionally fails on many cases of invalid args.
- But should we put that to the test (like done there)? I have some doubts, because that “fails” are not in specs, so they are specific to current implementation. Anyway, it should not be a problem to fix (or even remove) tests when we will need that.
- Still things are not consistent now: not all cases of invalid args are covered. E.g.
DomEvent.off
doesn’t fail if pass invalid object instead ofel
. And (in other functions)event
argument is not validated at all. All such cases could be covered with explicit type checking, but I suppose that would be overkill.
Thoughts?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
java - Throw exception if method arguments are invalid
If you want to throw an error, you have to use the throw keyword, followed by an appropriate Exception . public static int...
Read more >Exceptions
A throw expression accepts an expression (in this case, an unnamed object of the class invalid_argument ), which is passed as an argument...
Read more >How to Throw IllegalArgumentException in Java - Rollbar
The IllegalArgumentException is thrown to indicate an illegal or unsuitable argument passed to a method. Learn how to solve it.
Read more >Should I raise an exception/error when an optional argument ...
But wait, it's worse: if your function throws an exception when the ... Such as an Invalid Combination of Required and Optional Parameters....
Read more >std::invalid_argument - cppreference.com
Defines a type of object to be thrown as exception. It reports errors that arise because an argument value has not been accepted....
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 FreeTop 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
Top GitHub Comments
If you want to implement this yourself then better discuss all details in new PR.
This issue is created to discuss general questions, e.g. how far should we go in arguments validation. Should we cover as more functions/methods/constructors as we can? What of them are error prone (more often misused)? How should we avoid (potential) performance loss?
toPoint()
) but not to constructors (new Point()
), thus leaving space for custom optimization. E.g. when you need to process huge dataset - you may preferPoint
.There is also a problem with Tiles. If the
maxNativeZoom
is set with a string value, the tile-request contains sometimes a high zoom value: https://github.com/Leaflet/Leaflet/blob/d6d6051ac6acbc2c68b6b0f8ef1707cf91054f16/src/layer/tile/TileLayer.js#L209-L220this._tileZoom
/zoom
is then for example"17"
andzoomOffset
which is0
is added to it, which returns170
. If a tile is requested with zoom 170, the browser window breaks / stops running. But I didn’t found out why this happens