Skip to content

cppwinrt.h: Improve conversion of error information - #498

Closed
Arthur Araujo (arthuraraujo-msft) wants to merge 3 commits into
microsoft:masterfrom
arthuraraujo-msft:user/arthuraraujo/winrt-logging
Closed

Arthur Araujo (arthuraraujo-msft) wants to merge 3 commits into
microsoft:masterfrom
arthuraraujo-msft:user/arthuraraujo/winrt-logging

Conversation

@arthuraraujo-msft

Copy link
Copy Markdown

Closes #495

When using WIL with C++/WinRT with the cppwinrt.h header, the winrt_to_hresult_handler global handler is hooked from WIL directly into the C++/WinRT codebase.

The handler allows WIL exceptions to be understood by C++/WinRT and their HRESULTs extracted, but messages are lost in the way when going over the API boundaries because only the HRESULT is converted in winrt::to_hresult().

C++/WinRT makes use of Get and SetErrorInfo() to recover the messages when re-creating the exceptions on the caller side. The set is done inside winrt::hresult_error::to_abi() in winrt::to_hresult() on the server side, but only when winrt_to_hresult_handler is not hooked. The retrieval happens when re-creating the winrt::hresult_error on the caller.

If WIL were to call the to_abi() method in the WIL hook for winrt_to_hresult_handler, it would properly trigger the storage of the message and later on it could be retrieved when C++/WinRT creates a new winrt::hresult_error on the caller side.

That's what this PR is doing.
I also added a test to cover the message being passed.

@arthuraraujo-msft

Arthur Araujo (arthuraraujo-msft) commented Jan 15, 2025 •

Copy link
Copy Markdown
Author

I noticed that a few tests in ResultTests.cpp are failing because cppwinrt.h is included in the same compilation unit and affects those tests rather than them testing the normal environment.
For example tests\ResultTests.cpp:336 "Test ResultFromException..." seems like it was written to test the original ResultFromException flow, but it's testing cppwinrt's flow instead because of the override set in details::g_pfnResultFromCaughtException_CppWinRt.

ResultTests seems to be inserted into all test executables. Should it have custom checks depending on where it's inserted?

@dunhor

Copy link
Copy Markdown
Member

Correct me if I'm wrong, but you seem to effectively be using C++/WinRT as a way to call RoOriginateError? There's already a header for that: result_originate.h.

The new test failures seem very suspicious, though I can't quite comment on their cause without digging into it.

@arthuraraujo-msft

Copy link
Copy Markdown
Author

Duncan Horn (@dunhor) I had no idea that existed! I just tested it out and it does lead to the exceptions having the proper messages.
I don't see this in the docs, is there a place where this file is recommended?

@dunhor

Copy link
Copy Markdown
Member

I don't see this in the docs, is there a place where this file is recommended?

I don't see anything mentioning it in the error handling docs, which is where I'd expect to find it if something existed. I can add a pointer somewhere there later if nobody beats me to it.

Now, there's a question that I think this PR raises as to whether or not cppwinrt.h should include result_originate.h by default as I think it's safe to say that if you're using C++/WinRT, you're using WinRT and probably want error origination. Idk if that might have any adverse side effects, though. E.g. idk if that would lead to the same sorts of issues as you're seeing with the test failures with this PR. David Machaj (@dmachaj) might have some insight here as I believe he's more familiar with the error origination logic than I am

@arthuraraujo-msft

Copy link
Copy Markdown
Author

Duncan Horn (@dunhor) Thanks! That's a good point. I agree it could be interesting to include it by default.

@dmachaj

Copy link
Copy Markdown
Collaborator

If it isn't mentioned in the docs then that is definitely a gap that would benefit from being filled. I think the result_originate functionality is super useful and that any binaries using COM extensively should include it.

"Now, there's a question that I think this PR raises as to whether or not cppwinrt.h should include result_originate.h by default as I think it's safe to say that if you're using C++/WinRT, you're using WinRT and probably want error origination."

I don't think cppwinrt headers have any dependency on WIL so that would be the explanation for why this doesn't already happen and would be tricky to add. The cppwinrt error origination already calls RoOriginateError (such as in the winrt::hresult_error constructor). The case it doesn't cover is when WIL is also used for logging and control flow. It is valid to use cppwinrt without using WIL so its not really cppwinrt's job to ensure that you are including all of the best WIL headers.

I think that if we want to improve the default situation then we should consider including <wil/result_originate.h> from some of the other WIL headers such as <wil/cppwinrt.h> or <wil/com.h>.

@dunhor

Copy link
Copy Markdown
Member

I think that if we want to improve the default situation then we should consider including <wil/result_originate.h> from some of the other WIL headers such as <wil/cppwinrt.h> or <wil/com.h>.

Yes, when I said "there's a question that I think this PR raises as to whether or not cppwinrt.h should include result_originate.h by default" I was talking about wil/ cppwinrt.h

@arthuraraujo-msft

Copy link
Copy Markdown
Author

Thanks folks. I'll close this PR since there is an existing flow to produce the same results!

@arthuraraujo-msft
Arthur Araujo (arthuraraujo-msft) deleted the user/arthuraraujo/winrt-logging branch January 16, 2025 13:57
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.

cppwinrt.h: Improve conversion of error information

3 participants