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

Print HRESULT description when formatting Error::Hr #718

Merged
merged 4 commits into from
Mar 20, 2020

Conversation

teddemunnik
Copy link
Contributor

When implementing error handling for #712 I thought it'd be nice to add some details to the error HRESULT error messages instead of just error codes.

This change adds a short description from the FormatMessage windows API when printing an Error::Hr, when one is available.

As an example the following messages:

failed to set window style. HRESULT 0x80070585
failed to set window style. HRESULT 0x80070578

are now displayed as:

failed to set window style. HRESULT 0x80070585: Invalid index.
failed to set window style. HRESULT 0x80070578: Invalid window handle.

@teddemunnik teddemunnik force-pushed the windows_format_hresult branch from 9aa9ca7 to 31fe87a Compare March 20, 2020 01:53
The FormatMessageW API expects a LPWSTR* disguising as a LPWSTR, in the
case that the FORMAT_MESSAGE_ALLOCATE_BUFFER flag is present.
Copy link
Member

@xStrom xStrom left a comment

Choose a reason for hiding this comment

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

Nice work! Just one tiny change please.

| FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_MAX_WIDTH_MASK,
null(),
hr as u32,
Copy link
Member

Choose a reason for hiding this comment

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

Let's change this u32 to DWORD (from winapi::shared::minwindef). I know functionally they're the same and the u32 looks nicer but matching the types used in Microsoft docs when calling a winapi function will help whomever is reading this code in the future.

Copy link
Contributor Author

@teddemunnik teddemunnik Mar 20, 2020

Choose a reason for hiding this comment

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

Completely agree, changed it

null(),
hr as u32,
0,
transmute(&mut message_buffer as *const LPWSTR),
Copy link
Member

Choose a reason for hiding this comment

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

I think we can remove the as *const LPWSTR part. Otherwise we might as well just write transmute(&message_buffer).

Probably best to have this instead:

transmute(&mut message_buffer)

The mut part doesn't seem required for the code to work, but it communicates the fact that the function will modify the pointer so we can keep the mut.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I was a bit slow with this comment. I wasn't waiting until you push the DWORD change, I swear! 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems to work without the as *const LPWSTR, but then clippy complains:

warning: transmute from a reference to a pointer
--> druid-shell\src\platform\windows\error.rs:55:13
|
55 | transmute(&mut message_buffer),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: &mut message_buffer as *mut *mut u16 as *mut u16
|
= note: #[warn(clippy::useless_transmute)] on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_transmute

Is that another false positive? I already turned off clippy::crosspointer_transmute for this function in an earlier change.

Copy link
Member

Choose a reason for hiding this comment

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

Clippy seems unhappy indeed. However as *mut LPWSTR seems to satisfy clippy and is more to the truth, so maybe use that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah just listening to clippy apparantly this just works: &mut message_buffer as *const LPWSTR as LPWSTR, didn't know you could do that without transmute. Then I can remove the clippy ignore flags. Does that sound good?

Copy link
Member

Choose a reason for hiding this comment

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

Great find! I was trying to get rid of transmute earlier but didn't know how. It looks like the following works as well:

&mut message_buffer as *mut LPWSTR as LPWSTR

Let's go with that if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good! I just pushed this change :)

@teddemunnik teddemunnik force-pushed the windows_format_hresult branch from 3faa134 to 5771906 Compare March 20, 2020 17:41
Copy link
Member

@xStrom xStrom left a comment

Choose a reason for hiding this comment

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

Great work, this will be very useful.

@xStrom xStrom merged commit 19921e7 into linebender:master Mar 20, 2020
@raphlinus
Copy link
Contributor

I was going to complain about the obviously wrong casting, but that's winapi being winapi, and I agree this is the best way to write that in Rust.

@teddemunnik teddemunnik deleted the windows_format_hresult branch April 9, 2020 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants