-
Notifications
You must be signed in to change notification settings - Fork 27
RSDK-2758 Add an exception system #209
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
65d3303
add exception classes
benjirewis 745e19c
throw exceptions
benjirewis 325d9b9
error codes and no child classes
benjirewis b5d9494
try to fix global qualification issue
benjirewis 1931326
drew comments
benjirewis 7676682
const
benjirewis e63ed4d
more drew comments
benjirewis 901e15a
cleanup after rebase
benjirewis 27121d4
docs
benjirewis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #include <viam/sdk/common/exception.hpp> | ||
|
|
||
| namespace viam { | ||
| namespace sdk { | ||
|
|
||
| Exception::Exception(ErrorCondition condition, const std::string& what) | ||
| : std::runtime_error("viam::sdk::Exception: " + what), condition_(condition){}; | ||
|
|
||
| Exception::Exception(const std::string& what) : Exception(ErrorCondition::k_general, what){}; | ||
|
|
||
| Exception::~Exception() = default; | ||
|
|
||
| const std::error_condition& Exception::condition() const noexcept { | ||
| return condition_; | ||
| }; | ||
|
|
||
| std::error_condition make_error_condition(ErrorCondition e) { | ||
| struct ErrorCategory : std::error_category { | ||
| const char* name() const noexcept override { | ||
| return "viam::sdk"; | ||
| }; | ||
| std::string message(int ev) const override { | ||
| switch (static_cast<ErrorCondition>(ev)) { | ||
| case ErrorCondition::k_general: | ||
| return "general"; | ||
| case ErrorCondition::k_connection: | ||
| return "connection establishment failure"; | ||
| case ErrorCondition::k_duplicate_registration: | ||
| return "duplicate registration"; | ||
| case ErrorCondition::k_duplicate_resource: | ||
| return "duplicate resource"; | ||
| case ErrorCondition::k_grpc: | ||
| return "gRPC"; | ||
| case ErrorCondition::k_not_supported: | ||
| return "not supported"; | ||
| case ErrorCondition::k_resource_not_found: | ||
| return "resource not found"; | ||
| default: | ||
| return "unknown"; | ||
| } | ||
| }; | ||
| }; | ||
|
|
||
| static const ErrorCategory errorCategory{}; | ||
| return {static_cast<int>(e), errorCategory}; | ||
| } | ||
|
|
||
| GRPCException::GRPCException(grpc::Status status) | ||
| : Exception(ErrorCondition::k_grpc, status.error_message()), status_(std::move(status)){}; | ||
|
|
||
| const grpc::Status& GRPCException::status() const noexcept { | ||
| return status_; | ||
| } | ||
|
|
||
| } // namespace sdk | ||
| } // namespace viam |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /// @file common/exception.hpp | ||
| /// | ||
| /// @brief Defines custom exceptions for the SDK. | ||
| #pragma once | ||
| #include <grpcpp/support/status.h> | ||
benjirewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #include <stdexcept> | ||
| #include <string> | ||
|
|
||
| #include <viam/sdk/resource/resource_api.hpp> | ||
|
|
||
| namespace viam { | ||
| namespace sdk { | ||
|
|
||
| /// @defgroup Exception Classes related to SDK exceptions. | ||
|
|
||
| /// @class ErrorCondition | ||
| /// @brief Defines a set of a error conditions to be used in conjunction with | ||
| /// Exception. | ||
| /// @ingroup Exception | ||
| enum class ErrorCondition : uint8_t { | ||
| k_general = 0, // Default condition | ||
| k_connection = 1, // Issue during connection establishment | ||
| k_duplicate_registration = 2, // API or API/Model pair has already been registered | ||
| k_duplicate_resource = 3, // Resource has already been added | ||
| k_grpc = 4, // gRPC error from remote machine | ||
| k_not_supported = 5, // Behavior not supported by the SDK | ||
| k_resource_not_found = 6 // Resource does not exist | ||
| }; | ||
|
|
||
| std::error_condition make_error_condition(ErrorCondition e); | ||
|
|
||
| /// @class Exception | ||
| /// @brief Defines an exception type for the SDK. | ||
| /// @ingroup Exception | ||
| class Exception : public std::runtime_error { | ||
| public: | ||
| explicit Exception(ErrorCondition condition, const std::string& what); | ||
| explicit Exception(const std::string& what); | ||
| virtual ~Exception(); | ||
|
|
||
| const std::error_condition& condition() const noexcept; | ||
|
|
||
| private: | ||
| std::error_condition condition_; | ||
| }; | ||
|
|
||
| /// @class GRPCException | ||
| /// @brief Defines an exception from a gRPC interaction. | ||
| /// @ingroup Exception | ||
| class GRPCException : public Exception { | ||
| public: | ||
| explicit GRPCException(grpc::Status status); | ||
|
|
||
| const grpc::Status& status() const noexcept; | ||
|
|
||
| private: | ||
| grpc::Status status_; | ||
| }; | ||
|
|
||
| } // namespace sdk | ||
| } // namespace viam | ||
|
|
||
| namespace std { | ||
| template <> | ||
| struct is_error_condition_enum<viam::sdk::ErrorCondition> : true_type {}; | ||
| } // namespace std | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.