Skip to content
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

Improve Errno error handling #8305

Closed
j8r opened this issue Oct 10, 2019 · 7 comments
Closed

Improve Errno error handling #8305

j8r opened this issue Oct 10, 2019 · 7 comments

Comments

@j8r
Copy link
Contributor

j8r commented Oct 10, 2019

Current issues with having Errno constants are:

  • specific Errno exceptions can't be rescued specifically – that's either all or none.
  • comparing integer Errno#errno is the only way to know the actual error type.

The solution would be to use classes for errno errors to be able to differentiate them, in order to prevent the user to deal with integers.

@asterite
Copy link
Member

Another solution if to have something like rescue ex if ex.something? which will work really well with Errno. See #1124

@asterite
Copy link
Member

So this might be just a duplicate of #1124

@oprypin
Copy link
Member

oprypin commented Oct 10, 2019

Actually is #5003 "Change Errno into a hierarchy of OSError subclasses"

@straight-shoota
Copy link
Member

Thanks @oprypin for referencing your PR. I remembered there had been prior discussions, but couldn't find that specific one.

Currently, Errno simply wraps the error reporting mechanism provided by libc and places it into an exception. It's a legacy and bad design and doesn't represent idiomatic Crystal. This is strictly different for stdlib APIs that don't depend on libc functions. They use to raise specific exception classes depending on the type of error. Regular users shouldn't have to care about errno and how an API is implemented. The user experience should be identical whether a stdlib API is implemented in pure Crystal or based on libc. And it should be portable (see #4835).

@RX14
Copy link
Member

RX14 commented Oct 21, 2019

In my opinion, Errno needs to go. No other language apart from ruby couples their exception hierarchy to a decades old hack around C not being able to return multiple values like this.

My vision for exceptions at 1.0 is that they not only are well-named and not platform-specific, but they contain accessible fields relating to the error. For example, a DNS lookup failure would have a getter for the name which was being resolved. A network connection failure would have the IPAddress attached. errno would still exist, as a property, for the few people who need it, but it'd largely be ignored because it's just not necessary.

There might be technical problems that need to be overcome to achieve a better API but they can be solved like any other.

@rdp
Copy link
Contributor

rdp commented Nov 7, 2019

Dunno if related but here's an example where IO::Error was caught but it allowed Errno to still propagate up through (accidentally): #7136 (comment) FWIW :)

@j8r
Copy link
Contributor Author

j8r commented Mar 24, 2020

Closed with #8885

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants