-
Notifications
You must be signed in to change notification settings - Fork 18.1k
errors: no obvious way to map error to errno values #29054
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
Comments
Thank you for filing this issue @jech! Perhaps this might just be a docs discovery problem but I think that this can easily be translated today because: a) There are syscall.Errno enumerations of values https://golang.org/pkg/syscall/#Errors Despite it not being encouraged to fiddle around with the syscall package, I'll offer some tips A suggested remedya) Given an error, please check if it is of type syscall.Errno or os.SyscallError which for your code snippet translates then to the following: n, err := conn.WriteToUDP(data, &addr)
if err == nil {
return n // or whatever value you meant to return
}
// Otherwise now translate the error to a C errno value.
var e syscall.Errno = unix.EIO
if os.IsTimeout(err) {
e = unix.EAGAIN
} else if osscerr, ok := err.(*os.SyscallError); ok {
e = osscerr.Err.(syscall.Errno)
} else if scerr, ok := err.(syscall.Errno); ok {
e = scerr
}
C.set_errno(e)
return -1 @jech in the future, it would also be helpful to first discuss this on golang-dev or golang-nuts. I'll also kindly page @ianlancetaylor @rsc, please feel free to chime in and correct me. |
Thanks a lot for your code, that seems to do the trick. Still, would you agree that this is not something you'd expect the average Go programmer to discover on his own? And that this should either be encapsulated in a standard library function, or at least documented? |
Fair point that it would require a little bit more digging than usual, which is why I mentioned this perhaps being a Adding it to the standard library is something to consider carefully because how many people have encountered this issue? It is a cross-package utilitiy i.e. os, syscall, it involves error introspection. But also adding it in means that its signature has to be kept intact. for the lifetime of a GoX series which can be decades |
That makes perfect sense. (Please take it literally -- I'm not sure whether I agree, but your argument definitely makes sense.) (I'm not quite sure whether to close this issue — on the one hand, you've solved my immediate technical problem, for which thanks; on the other hand, I still think there's a documentation problem somewhere.) |
Given that this is an issue that isn't frequently reported and that we have solved it, let's close it. However, as I had suggested in #29054 (comment) whenever you get some free time, please write an "Experience Report" and share it on the wiki, on golang-nuts, social media etc. In this issue I've also paged some interested parties and as we work towards Go2, the feedback will be considered towards the next design. |
I don't see how a SysErr() or Errno() method fits in the scope of either Go 2 Error Values or Error Handling, as it's specific to errors stemming from syscalls. It seems like a method of net.Error, os.PathError, and similar types, or a new interface type. Also the solution above looks like it could be brittle. I'd suggest leaving it open, with title "proposal: Go2: add SysErr interface type" and tag Go2. That's the common approach to gaps in the stdlib. |
I think this issue is reasonably addressed by |
Ah this should work, tho it does push you into the syscall API.
|
@networkimprov: Wanting a C |
go version go1.11.2 linux/amd64
I'm writing some Go code that is called by C code, and I find there's no obvious way to map an error value to a Unix errno value. For example, at one point I write:
While that keeps the code working, it obviously loses a lot of the error granularity. Given that the errno value must be available somewhere under the hood, it's infuriating that it's not exported.
(I'd envision for example an interface with a single method
ToOsInteger
and that is implemented for those errors for which it makes sense to map to a Unix errno.)The text was updated successfully, but these errors were encountered: