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

Base.windowserror function to encapsulate Windows errors in SystemErrors #30613

Merged
merged 5 commits into from
Jan 9, 2019

Conversation

stevengj
Copy link
Member

@stevengj stevengj commented Jan 5, 2019

While working on #30611, I noticed a need for a function like systemerror but for Windows errors that use GetLastError() instead of errno, in order to:

  • Delay the cost of creating the formatted error string (with Libc.FormatMessage) until the error is actually displayed (if ever).
  • Still use a SystemError exception type, so that the exception type is consistent across platforms. (The fact that it is a Windows exception is stored in the extrainfo field.)
  • Encapsulate a common pattern to shorten the code.

Along the way, I found and fixed a bunch of apparent bugs — functions that were calling systemerror after Windows API calls that don't set errno. (These would have thrown a SystemError exception with errorshow function that confusingly prints "no error" or similar as the error message.)

The resulting Base.windowserror function is not exported.

@stevengj stevengj added the system:windows Affects only Windows label Jan 5, 2019
@stevengj
Copy link
Member Author

stevengj commented Jan 8, 2019

Yay, green CI!

@stevengj
Copy link
Member Author

stevengj commented Jan 8, 2019

@tkelman, @StefanKarpinski, good to merge?

@stevengj
Copy link
Member Author

stevengj commented Jan 8, 2019

Note that libuv has a uv_translate_sys_error function that translates a Windows error code like ERROR_NOACCESS into a libuv error code like UV_EACCES. If there is another function to translate libuv error codes into errno error codes, we might use this to set the errno field of the SystemError to a useful value in windowserror, in case any caller wants to make use of this in a catch block.

@StefanKarpinski
Copy link
Sponsor Member

I guess my only question is whether this needs to be a separate function from systemerror or if we can just have a single function that plays both roles by looking at GetLastError and errno but it seems like there may be no way of doing that and one needs to know which one each Windows API call sets and correspondingly which of systemerror and windowserror one needs to call.

@stevengj
Copy link
Member Author

stevengj commented Jan 9, 2019

I think they need to be separate functions, because whether you call GetLastError or errno depends on the preceding ccall. (Windows API calls generally need GetLastError and POSIX calls need errno.)

@stevengj stevengj merged commit 111a38f into JuliaLang:master Jan 9, 2019
@stevengj stevengj deleted the windowserror branch January 9, 2019 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
system:windows Affects only Windows
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants