std::variant for error types [on hold]
up vote
-2
down vote
favorite
I'm currently in the process of rewritting a http-client class. When it comes to the error-handling I see the three options:
- Throwing an exception
- Passing an error-type by reference
- Returning a
std::variant<SuccessType, ErrorType>
Given the following success / error types
struct HttpSuccess {
int status;
std::string body;
};
struct HttpError {
std::string error_description;
};
The function signatures for each of the three cases would look roughly like the following
HttpSuccess get(const std::string& uri);
(HttpErrror would inerhit from an exception type in this case)
HttpSuccess get(const std::string& uri, HttpError& error)
(alsostd::optional<HttpError>& error
possible)std::variant<HttpSuccess, HttpError> get(const std::string& uri)
Here is my reasoning why I dislike option 1 + 2:
- Since errors are to be expected (timeouts, internet hiccups etc), I don't want to use 1. The errors the client is dealing with are no execptions semantically.
- There is for me no way to ensure that the error-type is properly queried (afaik google uses rules to ensure this behaviour). So users might skip the error and proceed with the garabge return type.
This reasoning alone leaves me with option 3. The upsides that I see are the following:
- I can introduce different error categories easily (failure to contact the a server might be represented by a different error type than a server response error e.g.)
- Enforces error checking (with an admitably somewhat akward use of std::visit) but establishes reasoning about the error behaviour
- Allows for HttpSuccess to be non-default constructible which might be relevant in cases where a complex server response is deserialized before passing it on to the client.
The downsides that I see are mainly:
- More complex code on the library consumer side
- Overhead in the non-error case due to variant wrapper
However, all-in-all the third approach feels quite unidiomatic. Are there any opinions by the experts on whether any of the approaches is superior to the others?
c++ error-handling c++17
New contributor
put on hold as off-topic by Ludisposed, 200_success, Toby Speight, tinstaafl, Incomputable 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Ludisposed, 200_success, Toby Speight, tinstaafl, Incomputable
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-2
down vote
favorite
I'm currently in the process of rewritting a http-client class. When it comes to the error-handling I see the three options:
- Throwing an exception
- Passing an error-type by reference
- Returning a
std::variant<SuccessType, ErrorType>
Given the following success / error types
struct HttpSuccess {
int status;
std::string body;
};
struct HttpError {
std::string error_description;
};
The function signatures for each of the three cases would look roughly like the following
HttpSuccess get(const std::string& uri);
(HttpErrror would inerhit from an exception type in this case)
HttpSuccess get(const std::string& uri, HttpError& error)
(alsostd::optional<HttpError>& error
possible)std::variant<HttpSuccess, HttpError> get(const std::string& uri)
Here is my reasoning why I dislike option 1 + 2:
- Since errors are to be expected (timeouts, internet hiccups etc), I don't want to use 1. The errors the client is dealing with are no execptions semantically.
- There is for me no way to ensure that the error-type is properly queried (afaik google uses rules to ensure this behaviour). So users might skip the error and proceed with the garabge return type.
This reasoning alone leaves me with option 3. The upsides that I see are the following:
- I can introduce different error categories easily (failure to contact the a server might be represented by a different error type than a server response error e.g.)
- Enforces error checking (with an admitably somewhat akward use of std::visit) but establishes reasoning about the error behaviour
- Allows for HttpSuccess to be non-default constructible which might be relevant in cases where a complex server response is deserialized before passing it on to the client.
The downsides that I see are mainly:
- More complex code on the library consumer side
- Overhead in the non-error case due to variant wrapper
However, all-in-all the third approach feels quite unidiomatic. Are there any opinions by the experts on whether any of the approaches is superior to the others?
c++ error-handling c++17
New contributor
put on hold as off-topic by Ludisposed, 200_success, Toby Speight, tinstaafl, Incomputable 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Ludisposed, 200_success, Toby Speight, tinstaafl, Incomputable
If this question can be reworded to fit the rules in the help center, please edit the question.
1
I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR?
– Calak
2 days ago
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I'm currently in the process of rewritting a http-client class. When it comes to the error-handling I see the three options:
- Throwing an exception
- Passing an error-type by reference
- Returning a
std::variant<SuccessType, ErrorType>
Given the following success / error types
struct HttpSuccess {
int status;
std::string body;
};
struct HttpError {
std::string error_description;
};
The function signatures for each of the three cases would look roughly like the following
HttpSuccess get(const std::string& uri);
(HttpErrror would inerhit from an exception type in this case)
HttpSuccess get(const std::string& uri, HttpError& error)
(alsostd::optional<HttpError>& error
possible)std::variant<HttpSuccess, HttpError> get(const std::string& uri)
Here is my reasoning why I dislike option 1 + 2:
- Since errors are to be expected (timeouts, internet hiccups etc), I don't want to use 1. The errors the client is dealing with are no execptions semantically.
- There is for me no way to ensure that the error-type is properly queried (afaik google uses rules to ensure this behaviour). So users might skip the error and proceed with the garabge return type.
This reasoning alone leaves me with option 3. The upsides that I see are the following:
- I can introduce different error categories easily (failure to contact the a server might be represented by a different error type than a server response error e.g.)
- Enforces error checking (with an admitably somewhat akward use of std::visit) but establishes reasoning about the error behaviour
- Allows for HttpSuccess to be non-default constructible which might be relevant in cases where a complex server response is deserialized before passing it on to the client.
The downsides that I see are mainly:
- More complex code on the library consumer side
- Overhead in the non-error case due to variant wrapper
However, all-in-all the third approach feels quite unidiomatic. Are there any opinions by the experts on whether any of the approaches is superior to the others?
c++ error-handling c++17
New contributor
I'm currently in the process of rewritting a http-client class. When it comes to the error-handling I see the three options:
- Throwing an exception
- Passing an error-type by reference
- Returning a
std::variant<SuccessType, ErrorType>
Given the following success / error types
struct HttpSuccess {
int status;
std::string body;
};
struct HttpError {
std::string error_description;
};
The function signatures for each of the three cases would look roughly like the following
HttpSuccess get(const std::string& uri);
(HttpErrror would inerhit from an exception type in this case)
HttpSuccess get(const std::string& uri, HttpError& error)
(alsostd::optional<HttpError>& error
possible)std::variant<HttpSuccess, HttpError> get(const std::string& uri)
Here is my reasoning why I dislike option 1 + 2:
- Since errors are to be expected (timeouts, internet hiccups etc), I don't want to use 1. The errors the client is dealing with are no execptions semantically.
- There is for me no way to ensure that the error-type is properly queried (afaik google uses rules to ensure this behaviour). So users might skip the error and proceed with the garabge return type.
This reasoning alone leaves me with option 3. The upsides that I see are the following:
- I can introduce different error categories easily (failure to contact the a server might be represented by a different error type than a server response error e.g.)
- Enforces error checking (with an admitably somewhat akward use of std::visit) but establishes reasoning about the error behaviour
- Allows for HttpSuccess to be non-default constructible which might be relevant in cases where a complex server response is deserialized before passing it on to the client.
The downsides that I see are mainly:
- More complex code on the library consumer side
- Overhead in the non-error case due to variant wrapper
However, all-in-all the third approach feels quite unidiomatic. Are there any opinions by the experts on whether any of the approaches is superior to the others?
c++ error-handling c++17
c++ error-handling c++17
New contributor
New contributor
edited 2 days ago
Calak
1,42512
1,42512
New contributor
asked 2 days ago
user823255
97
97
New contributor
New contributor
put on hold as off-topic by Ludisposed, 200_success, Toby Speight, tinstaafl, Incomputable 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Ludisposed, 200_success, Toby Speight, tinstaafl, Incomputable
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Ludisposed, 200_success, Toby Speight, tinstaafl, Incomputable 2 days ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Ludisposed, 200_success, Toby Speight, tinstaafl, Incomputable
If this question can be reworded to fit the rules in the help center, please edit the question.
1
I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR?
– Calak
2 days ago
add a comment |
1
I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR?
– Calak
2 days ago
1
1
I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR?
– Calak
2 days ago
I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR?
– Calak
2 days ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
1
I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR?
– Calak
2 days ago