-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
os: add error_posix() and error_win32() for explicit platform error handling and default behavior #20694
Conversation
Co-authored-by: JalonSolov <JalonSolov@gmail.com>
Hmm... I applaud the new routines, but I'm not a fan of making platform specific functions, as that now forces extra comptime code or other nonsense if you want the code to work on all platforms. I think I'd rather see Param struct to the rescue, 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.
Excellent work.
I am not sure I understand your idea. Note that on windows, some APIs do set I think the solution in this PR, solves it in a much more straightforward way, by introducing a new error, and 2 constructor functions for it, then all callsites can decide for themselves what do they want, and it is easy to check for erroneous usages. |
@[inline] | ||
pub fn error_win32(e SystemError) IError { | ||
$if windows { | ||
code := if e.code == os.error_code_not_set { int(C.GetLastError()) } else { e.code } | ||
message := if e.msg == '' { get_error_msg(code) } else { e.msg } | ||
return error_with_code(message, code) | ||
} $else { | ||
panic('Win32 API not available on this platform.') | ||
} | ||
} |
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.
This specific function could potentially move to vlib/os/os_windows.c.v
so that using it is a comptime error in the future, although that may break bootstrapping 🤔 now.
We could also use $compile_error('...')
here instead of the runtime panic, once that works reliably.
The way I understand @JalonSolov's idea is something like this:
|
Sort of. I was more suggesting that |
I see. I did not change the builtin |
There are about 100 calls to that function in all the main V source, so that's not too terrible. Of the 18 modules I have installed (including all the main V ones, including vsl, vtl, ui, etc.), there is exactly 1 call in the markdown module. So, simple enough to update. |
No, error_with_code is simple, and should not be changed. Another function is ok. |
…andling and default behavior (vlang#20694)
In the os module, in some places, errors are returned with the POSIX error message and C.errno
return error_with_code(posix_get_error_msg(C.errno), C.errno)
In other places, errors are swallowed and the error code is lost (in vfopen())
return error('failed to open file "${path}"')
It is often desirable to override the default error message to provide additional information (like ${path} in the example above).
Therefore, this PR proposes an explicit function
error_posix()
with default behavior oferror_with_code(posix_get_error_msg(C.errno), C.errno)
and the option to override the error message (
msg
) or error code (code
) when desired.Example:
return error_posix(msg: 'failed to open file "${path}"')
In this case, the returned IError would still contain C.errno as its error code.
error_win32
works analogous usingint(C.GetLastError())
andget_error_msg()
to get Win32 API errors.Using
error_posix
anderror_win32
enables consistent default behavior, ensures the API error codes do not get lost, and makes it easier for developers to see that the error handling matches the used API.