-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optional error information instead of throwing (by default) #45
Conversation
Maybe I'm missing a class of use cases, but it seems to me that type functions should always throw rather than return false. This allows the user to make a determination about whether and how to proceed, with the relevant error message (via a typical try/catch). Of course, if type checking without throwing is a common enough pattern you could add some sugar that wraps the typical type function in a try catch that returns false. I'm trying to imagine why you would need both an exception-less return AND access to the exception, but if that's another pattern to support you could add error saving to the sugary function. At least it would keep it in one place. |
@treygriffith often you'll have functions like If you presumed to always The ambiguity is what I want to remove, settling on one or the other. However, if you never throw, you don't get the reason for why the type was non conformant, and instead you are left with a high level reason which may be misleading. Another problem with throwing is that performance sucks for recursive/deep types, This PR sticks with the |
@dcousens that makes sense given the use case you highlighted (and the performance impact). Might I suggest then keeping the error saving in one place rather than attaching it as an property to the various functions? I'm imagining something like this:
Might not be your style or there might be another reason to do it the way you have it, but feels better to manage the setting and getting of the error information in one place. |
Great point, but how do you support external types? Additionally, what resets the field? Any type that might set it I suppose? |
Yeah, I would think exposing As for reset, I think it makes the most sense to have next type error override it. I would expect consumers of the API to check the return value of the type function and then check for the lastTypeError - it's existence shouldn't mean anything on its own. |
@treygriffith I suspect that |
Aye, but what if they didn't call |
At least some of the benefits for this can be found in #53 |
I suppose the approach taken in #53 might be better. |
@treygriffith indeed, until I find the ideal solution for |
From #42
Type functions should simply always return false on failure rather than returning false OR throwing.
The problem is, without extra information, the error messages would suck, so return false AND add the extra information, could give the best of both.