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

Add Errno#errno_message getter #6702

Merged
merged 6 commits into from Jan 2, 2019
Merged

Conversation

wooster0
Copy link
Contributor

@wooster0 wooster0 commented Sep 11, 2018

This adds Errno#errno_message getter which returns the last raised errno message.

Usecase:
I want to print the error message from errno in my application just like this: No such file or directory instead of this: Unhandled exception: some_call: No such file or directory (Errno).

I noticed that I override the `message` method in Exception so I needed to rename it.
@wooster0 wooster0 changed the title Add Errno.message Add Errno.description Sep 11, 2018
src/errno.cr Outdated Show resolved Hide resolved
@wooster0 wooster0 changed the title Add Errno.description Add Errno.error Sep 11, 2018
Copy link
Contributor

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not.

src/errno.cr Outdated Show resolved Hide resolved
@RX14
Copy link
Contributor

RX14 commented Sep 12, 2018

Perhaps this should be a method instead of a class method

@asterite
Copy link
Member

Once we make the error hierarchy independent of the OS, will this still be here?

I don't quite understand the use case.

@wooster0 wooster0 changed the title Add Errno.error Add Errno#message Sep 12, 2018
@wooster0
Copy link
Contributor Author

wooster0 commented Sep 12, 2018

@RX14 In the end there isn't really a difference and the other methods are also already class methods.
But if we make self.message an instance method, the others should be instance methods too I think.

@asterite Yes I think it will still be here. The use case is just that when I for example read a file and an error happens, that I can print the reason why the file couldn't be read.
I could right now for example do:

begin
  File.read("a")
rescue ex : Errno
  puts ex
end

and it would print Error opening file 'a' with mode 'r': No such file or directory. But I just want to print the errno message No such file or directory in my program.

@asterite
Copy link
Member

@r00ster91 then let's save @message in an instance variable and expose that as a getter.

@asterite
Copy link
Member

Hm, never mind, the name collides with Exception's message. Maybe @errno_message, though...

@wooster0
Copy link
Contributor Author

wooster0 commented Sep 12, 2018

@asterite Hmm so we always save the last raised errno message in @errno_message. But what if I for example want to get some other errno message with a number instead of just the last one that has been raised? I think we should rather use a method.

@asterite
Copy link
Member

But what if I for example want to get some other errno message with a number instead of just the last one that has been raised?

What would be an example for this? Errno gets re-written each time there's an error, so it only makes sense to reference the last one.

With that you could do:

begin
  File.read("a")
rescue ex : Errno
  puts ex.errno_message
end

But more specifically, why don't you do:

begin
  File.read("a")
rescue ex
  puts "Can't read file a"
end

I don't think Errno as an exception will survive in the future, we'll probably have a different exception hierarchy and exposing errno message will probably don't exist.

@straight-shoota
Copy link
Member

But what if I for example want to get some other errno message with a number

You could always just call LibC.error directly.

@wooster0
Copy link
Contributor Author

But more specifically, why don't you do:

begin
 File.read("a")
rescue ex
 puts "Can't read file a"
end

I prefer to give the user specific information why the program couldn't read the file. Because just this is IMO not very helpful to solve the problem.

But okay I will change it to a getter then. I agree. In the end when really need it, I could just call LibC.strerror myself.

@wooster0 wooster0 changed the title Add Errno#message Add Errno#errno_message getter Sep 13, 2018
@asterite asterite added this to the 0.27.1 milestone Jan 2, 2019
@asterite asterite merged commit 5978883 into crystal-lang:master Jan 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants