[SERVER-19720] add a std::error_category for MongoDB error codes Created: 03/Aug/15  Updated: 19/Sep/15  Resolved: 13/Aug/15

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: None
Fix Version/s: 3.1.7

Type: Task Priority: Major - P3
Reporter: Adam Midvidy Assignee: Adam Midvidy
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Sprint: Platform 8 08/28/15
Participants:

 Description   

In NetworkInterfaceASIO, we often need to indicate error conditions by returning an instance of std::error_code. As we have our own error code space (the codes defined in error_codes.err), we should have our own error category so that we don't collide with any system codes.

We will probably also need to generate the code from error_codes.err so that we can convert the codes to their associated strings.



 Comments   
Comment by Githook User [ 14/Aug/15 ]

Author:

{u'username': u'amidvidy', u'name': u'Adam Midvidy', u'email': u'amidvidy@gmail.com'}

Message: SERVER-19720 don't compare error codes against ErrorCodes::OK due to MSVC
Branch: master
https://github.com/mongodb/mongo/commit/292cf22afced2c652c11012be931d99f2acdd485

Comment by Githook User [ 13/Aug/15 ]

Author:

{u'username': u'amidvidy', u'name': u'Adam Midvidy', u'email': u'amidvidy@gmail.com'}

Message: SERVER-19720 harmonize handling of std::error_code and ErrorCodes in NetworkInterfaceASIO
Branch: master
https://github.com/mongodb/mongo/commit/870efac99e9195c03389645df3da5a27a388102d

Comment by Githook User [ 13/Aug/15 ]

Author:

{u'username': u'amidvidy', u'name': u'Adam Midvidy', u'email': u'amidvidy@gmail.com'}

Message: SERVER-19720 add missing BOOST_NOEXCEPT to fix MSVC
Branch: master
https://github.com/mongodb/mongo/commit/7de54a86f4fc04a5ce7d2e9f8ba7ab84fecd318d

Comment by Githook User [ 13/Aug/15 ]

Author:

{u'username': u'amidvidy', u'name': u'Adam Midvidy', u'email': u'amidvidy@gmail.com'}

Message: SERVER-19720 fix lint
Branch: master
https://github.com/mongodb/mongo/commit/9eadefd1f5e9f9a480f14fe5ad0b1b1838e96005

Comment by Githook User [ 13/Aug/15 ]

Author:

{u'username': u'amidvidy', u'name': u'Adam Midvidy', u'email': u'amidvidy@gmail.com'}

Message: SERVER-19720 add a std::error_category() for MongoDB error codes
Branch: master
https://github.com/mongodb/mongo/commit/34618123fd568b02a93c294768cb2b5afc54798b

Comment by Adam Midvidy [ 04/Aug/15 ]

Yeah, you're right that we don't need code generation. However, the <system_error> API added in C++11 is actually a bit complex. Note that the STL provides both std::error_code and std::error_condition, which represent platform specific and platform independent errors, respectively. Since our ErrorCodes are platform independent, we really should be using std::error_condition, but we need to be able to create instances of std::error_code as well to interoperate with ASIO. As such, there are a few things I think we need to do:

1) Add an implementation of mongo_error_category that extends std::error_category. As you noted, most of the virtual functions we need to implement can be implemented using the existing logic in ErrorCodes.

2) Add an overload of make_error_code for ErrorCodes::Error.

3) Add an overload of make_error_condition for ErrorCodes::Error

4) specialize is_error_code_enum for ErrorCodes::Error

5) specialize is_error_condition_enum for ErrorCodes::Error

For reference, here are two blog posts by the author of ASIO, who played a major role in designing the <system_error> API for C++11:

Comment by Andy Schwerin [ 03/Aug/15 ]

Given an error_code with appropriate category, err, I believe we can write ErrorCodes::errorString(static_cast<ErrorCodes::Error>(err.value())). I don't think it needs to be auto-generated. A handful of functions, possibly in status.h, seem appropriate:

std::error_code toStdErrorCode(ErrorCodes::Error);
ErrorCodes::Error toMongoErrorCode(std::error_code);
stdErrorCodeToString(std::error_code); // ?

Generated at Thu Feb 08 03:51:52 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.