-
Notifications
You must be signed in to change notification settings - Fork 31
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
Error classification #35
Comments
I think it is good to have DataError subclasses, due it is how exceptions in python designed to be. And |
Different error classes are for catching them. Do you have a use case for it? |
Codes and exception hierarchy is good thing. Why not to have it? It will not hurt experience, but can provide more use cases of library. You can safely ignore hierarchy and codes. |
Up to you |
@asvetlov typical use case would be providing structured information (possibly in JSON) to frontend application reporting what validation problems were encountered. Existing solution is somehow usable for English speaking applications, but it is rather limited. Existing exception is good in that it provides nested information about the failure. What is missing is reliable distinction of error types and ability to address multiple errors at once (the later could be probably omited). The concept of solution could be:
Things are best developed under some real scenario, so I would think of taking any of existing exception and expressing them in form of JSON data structure (think of the the fact, keys can be only strings). Taking an example from README.rst:
it could be expressed e.g. as:
This is just very first proposal, there might be better ways for addressing the element. |
Exact JSON schema is up to you, it will not be a part of trafaret. So I vote for exceptions codes and hierarchy. And probably we will need to modify OnError to be able to raise particular exception and code in addition to message. |
I agree on what @asvetlov is talking about. Rather than doing something like this: import trafaret as t
try:
t.Int(gt=5, lt=10).check(some_var)
except t.TooShortError:
message = 'not big enough'
except t.TooBigError:
message = 'way too big'
except t.InvalidTypeError:
message = 'expecting an integer'
except t.DataError: # all other errors I don't care about
message = 'invalid value' I would prefer this way: import trafaret as t
errors = {
'too_short': 'not big enough',
'too_big': 'way too big'
}
try:
t.Int(gt=5, lt=10).check(some_var)
except t.DataError as exc:
message = errors.get(exc.code, 'invalid value') It is much less hassle to deal with codes rather than specific exceptions. I can hardly imagine someone needs those, can you? So if it's not the case, I'm going only for codes. |
Quoting
I would strongly prefer the first way (using Exception classes) for following reasons:
{t.TooShortError: "not big enough",
t.TooBigError: "way too big",
t.InvalidTyeError: "expecting an integer",
t.DataError: "invalid value"} Alternatively there is also To summarize: I would prefer class based solution as code-based one seems to introduce unnecessary complexity without significant benefit. |
@vlcinsky if you want to play with words -- error codes could be the only and obvious way :)
|
@asvetlov I like your call for real use case as this could be the tool to evaluate, which approach works better. Less important notes:
Why I prefer Exceptions over error codesI did some refactoring of error handling in my web application and got a bit unhappy with error codes finding subclassing Exception better:
Important: Searching for use casesThere are always multiple ways to do it, clearly defined use cases could help detecting the most practical approach. I have already mentioned the use case with serializing the exception in such a way, JavaScript could use it for reporting the problem to user. Any more cases? And there are other questions about the exceptions:
|
Exceptions can be used to write trafarets. Anyway I don't see anything harmful in exceptions hierarchy. |
please, don't make PRs for master branch, due I don't want to merge it with version2. |
OK, I'll take some time to think through everything posted here and will make a PR towards version2 branch hopefully soon. |
I'd like to start a discussion about everything that comes with errors.
At the moment we have only one type of error -
DataError
, which is when something is invalid.The problem I see here is when you want to give any extra details about what gone wrong, you can't.
In my application I've come up across this issue and the only way I could deal with it is something like this:
This is ugly and absolutely not reliable. Trafarets tend to have different messages describing such cases, and next time I'm gonna add a trafaret I'm not using just yet, I can't be sure it works like expected. Not speaking about custom extensions...
In my opinion there should be internal error classification and all the trafarets we have so far should stick to it. And honestly, I think we need to drop error messages too because really they're out of scope of this library, it is simply too much specific context that generally lives on app level.
Such system also has to be extendable for specific cases when trafaret's basics aren't enough. I mean you should be able to easily make your own error types and use them inside library ecosystem, not around it.
First approach I'd like to consider is introduce some classes like generic
InvalidError
along with more specificRequiredError
,TooBigError
and so on, which all subclassDataError
. Also it would be handy to add some shortcuts forTrafaret
class as well. And maybe deal withOnError
trafaret in some way to go along with the changes?I'm willing to make a pull request with those improvements, but of course we have to come to terms first about how it should be done.
What do you think?
The text was updated successfully, but these errors were encountered: