-
Notifications
You must be signed in to change notification settings - Fork 8
Use StringError exception and errors #7
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
Conversation
@nalimilan Bring out the bricks! Let me know what you think.
src/iconv.jl
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave strerror here, as you're using it.
codecov-io
commented
Jan 13, 2016
Current coverage is 88.88%
Merging #7 into master will increase coverage by +0.46% as of
72ba299
@@ master #7 diff @@ ====================================== Files 1 1 Stmts 95 108 +13 Branches 0 0 Methods 0 0 ====================================== + Hit 84 96 +12 Partial 0 0 - Missed 11 12 +1
Review entire Coverage Diff as of
72ba299Powered by Codecov. Updated on successful CI builds.
src/iconv.jl
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't make this the same exception type as other errors. It will only happen when loading the package, I can be a standard ErrorException. Ideally, BinDeps would provide a standardize way of checking this, with a specific error.
58c4d45 to
ee8e9fa
Compare
@nalimilan Hopefully this addresses everything from your review (except possibly using separate exception types, which I could do in a follow up PR as soon as you've gotten some feedback, or decided yourself what you prefer)
I've just posted a message on julia-users: https://groups.google.com/forum/#!topic/julia-users/yMMj5GQi-hY
Ah, I hadn't been thinking of making StringEncodingError an abstract type, and then the other specific types as concrete, I'd thought you were talking about a number of concrete types.
I really like that approach (in addition, in that case, I'd want to lobby for an abstract StringError put in Base, and have my UnicodeError changed to be a concrete subtype of that), as well as new concrete exceptions for iconv.jl (which more and more seems maybe should be StringEncoders.jl or StringEncodings.jl)
Good idea! 👍
As a separate commit, I added support for my understanding of your suggestion on julia-users.
Let me know if that's what you wanted, I do think it made things cleaner!
src/iconv.jl
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the point of this GeneralError type. The idea behind using specific exception types was precisely that they can be adapted to the specific information you need to convey. In my message to julia-users, I simplified the typology a bit, but the other constants from above should all be replaced with an exception type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of a GeneralError type was to be able (in the future) to have the actual messages table driven, based on the locale, so a French locale could display error strings in French, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Translating error messages is another big issue that should be tackled in Julia more broadly. Let's keep that out of scope for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, but this does nothing more than what UnicodeError already does in Base, to allow for the format strings to be constant - which is what allows the error handling to be done without forcing the entire function to always push a GC frame (I know that Yuyichao has talked about changing things so that that is not so bad, by only doing that along branches that do the creating, but I'm not sure what the status of the in v0.4.x or master).
Yes, the whole locale business is something to think about for the future, but I think having GeneralError which greatly simplifies making the specific exception types, is well worth having here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to have all StringEncodingError subtypes contain those fields instead, and get rid of GeneraError. That way, you can make args a tuple of the right length for each type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But wouldn't that mean adding a lot of code to have specific show methods for every single type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because you should be able to write a generic function for StringEncodingError.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, OK, I'll try to figure out how to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, wasn't as painful as I thought it would be, but does mean instead of a single value for each subtype of StringEncodingError, they need to copy the three that GeneralError had.
Let me know what you think of this commit, thanks!
Hope I didn't miss anything, and that you think this PR is a net improvement. Thanks again for spending the time to improve strings on Julia & being patient with these PRs to your package!
src/iconv.jl
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, this looks good, but I've come to think that instead of storing a msg field which would be identical for all instances; it would be better to have message(::Type{InvalidEncodingError}), etc., functions which will return the message template. Then it should be OK.
That should also allow you to get rid of inner constructors (or of most of them).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hehe, no problem at all, sounds like a good idea! Just hope you are OK with all the time on this, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I've been changing it to use message, I had a thought, that maybe this loses some important flexibility. If you use constructors, it you can have different messages, based on the number or types of the arguments to the constructors, but with this scheme, how would you manage that?
For this particular package, at least now, it probably doesn't matter, but I still am thinking about what we'd want for a much more flexible and efficient general error scheme for Julia.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could still define the method to return the msg field if it's actually there for the considered type. So I don't think we'd lose in generality at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another issue, is that I think it adds a dynamic dispatch when you actually do call show, whereas before, that was not necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll try that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried a slightly different method that looked a bit cleaner - let me know what you think, I added a subtype for StringEncodingErrors that don't take any arguments, makes the show() method for thos just a simple one-liner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than an abstract type, I'd prefer if you used a type union for show. Also put the two show methods at the same place for clarity. That way, the type tree won't reflect implementation details: subtypes(StringEncodingError) will only return interesting information for the user.
After that, we should be done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, done!
c419535 to
7444825
Compare
src/iconv.jl
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, no need to use the longer parametric function syntax here, just put the Union after the argument (also, putting the function on two lines would be better).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, that's just how I've usually seen it in the code in Base - will change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually turned out to be a few characters longer - before I had message(T), now it is message(typeof(exc)), but that's fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you wish, but it would be nice to make the line shorter by splitting it.
7444825 to
6f85c20
Compare
@nalimilan Does this look OK now?
src/iconv.jl
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InvalidSequenceError would be more precise and consistent with the other one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason this was separate, was that this one didn't have the byte sequence available.
Do you have a good solution for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. Then just remove that branch, as I don't think it can happen (the call doesn't pass any input data...). Anyway we'll get a generic error if it happens.
Thanks. Please squash the PR into a single commit.
Implement Milan's idea for abstract StringEncodingError Changes per Milan's review: generalerror -> iconv_error Removal of iconv: from most error messages Addition of more specific exceptions: IConvError, OutputBufferError, InvalidBytesError Eliminate GeneralError, per review Remove iconv_error, modulename, clean up show, per review
6f85c20 to
49f1370
Compare
@nalimilan Anything else? I added one extra commit to add some more tests and make codecov-io stop complaining, I can squash that in as well.
OK, looks good, thanks!
Use StringError exception and errors
Thank you! I enjoyed getting all your input!
This is intended to make error handling a bit cleaner, and also to potentially improve performance in functions where before it would have had to create a GC frame (because of creating a string to pass to error()).