-
Notifications
You must be signed in to change notification settings - Fork 21
Use std::error_code with custom error category #95
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
Conversation
Co-authored-by: Jimmy Song <55897143+jimson-msft@users.noreply.github.com> Cherry-picked from #89
sdk-cpp/include/do_errors.h
Outdated
|
|
||
| private: | ||
| int32_t _code; | ||
| //error_category _category; |
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.
IIRC, this should be a reference. Commonly, a reference to a static instance. See the constructor here: https://en.cppreference.com/w/cpp/error/error_code/error_code
and cpprest's category here:
https://github.com/microsoft/cpprestsdk/blob/master/Release/src/utilities/asyncrt_utils.cpp
#Resolved
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.
Now that we have a better understanding of this - I believe that all we need is our own error_category instances and not a new error_code class.
We should reuse the std::error_code class and provide our own error_category instance where relevant (i.e.,, when we store a DO/boost/libcurl error within).
We can have our own errc enum and still reuse std::error_code.
Let me know if you want to discuss this over a call.
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.
Hmm agreed, rather than construct std::error_code with that extra param, what do you think of iter 15 where we use our own error_code class to supply the do_category via overloading category()
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.
Thanks. Been thinking some more about this -
DU actually adds their own 'facility' bits to the error code to identify that the error came from DO.
So as a c++ library author, we should not worry about how our callers identify the source of errors in their telemetry. Each caller might want a different method.
Considering that, and the fact that we are trying to follow c++ standards, I think we should use std::error_code with our own error_category instances (more than one if needed; for now keep what we have).
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.
You can leave the insertion of DO facility bits into the error codes as-is for now. Just remove the custom error_code class.
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.
sounds good to me
sdk-cpp/include/do_error_macros.h
Outdated
| #endif | ||
| #define DO_OK error_code(0) | ||
|
|
||
| #define DO_SUCCEEDED(code) (((int32_t)(code.value())) < 0) |
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.
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.
Read this comment after my other one in do_errors.h about using std::error_code.
sdk-cpp/include/do_error_macros.h
Outdated
| #endif | ||
|
|
||
| //TODO(jimson) Looks like these conversions are causing test issues with tests | ||
| //TODO(jimson): Do we really need these after switching to error code class? The error_category should help us differentiate what type of error it is |
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.
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.
Actually, wait, we need to think about this because DU client can only report an integer error code so the error_category information gets lost there.
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.
Yeah that's a good point - so I'm not sure if using only the error_category is sufficient - unless we ask them to perform the bit operations to add our facility code based on the error category in returned error_code
Can't we expose error code with the facility bits already added to the internal value? Would this make the error category redundant if the error code itself contains the 'category' via the facility bits?
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.
Let's still apply the facility code where needed for DU purposes, and make use of error_category
IMO this is redundant since caller can get it from std::error_code. We should remove this and rename get_error_code() to error_code(). In reply to: 942778026 In reply to: 942778026 In reply to: 942778026 In reply to: 942778026 In reply to: 942778026 In reply to: 942778026 In reply to: 942778026 Refers to: sdk-cpp/include/do_errors.h:84 in efea6d9. [](commit_id = efea6d9, deletion_comment = False) |
shishirb-MSFT
left a comment
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.
![]()
|
There are some failing tests with these changes, looking into fixing now |
shishirb-MSFT
left a comment
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.
![]()
Resolved |
|
|
||
| std::error_code make_error_code(std::errc e) | ||
| { | ||
| return std::error_code(DO_ERROR_FROM_STD_ERROR(e), do_category()); |
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.
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.
I'll sign off.
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.
Hmm I'm not sure I follow - I think we do need to do this in order to make use of the same error code macros/convention. the std::errc enum are a set of positive integer values, and so need to call DO_ERROR_FROM_STD_ERROR to make them hresult-like.
shishirb-MSFT
left a comment
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.
![]()
No description provided.