-
Notifications
You must be signed in to change notification settings - Fork 27
RSDK-2758 Consistent error handling & exception scheme #100
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
29f9123
Add Custom Exceptions
zaporter-work 9ae7de9
Additional throws
zaporter-work 7d57297
Pr nits
zaporter-work 3591a8e
Add clarifying comment
zaporter-work b835954
Merge branch 'main' into zp/exception
zaporter-work d95ac27
Merge branch 'main' into zp/exception
zaporter-work bc83d60
Merge branch 'main' into zp/exception
stuqdog 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,68 @@ | ||
| #include <grpc/status.h> | ||
| #include <viam/sdk/common/exception.hpp> | ||
| namespace viam { | ||
| namespace sdk { | ||
|
|
||
| ViamException::ViamException(const std::string& what) | ||
| : std::runtime_error("ViamException: " + what), error_code_(ViamErrorCode::unknown){}; | ||
|
|
||
| ViamException::ViamException(const std::string& type, | ||
| const ViamErrorCode& code, | ||
| const std::string& what) | ||
| : std::runtime_error("ViamException(" + type + "): " + what), error_code_(code){}; | ||
|
|
||
| ViamException::~ViamException() = default; | ||
|
|
||
| ViamException::ViamException(const ::grpc::Status& status_code) | ||
| : ViamException(status_code.error_message() + " " + status_code.error_details()) {} | ||
|
|
||
| ViamException ViamException::from_viam_error_code(ViamErrorCode code) { | ||
| switch (code) { | ||
| case ViamErrorCode::ok: { | ||
| return ViamException( | ||
| "Ran ViamException::from_viam_error_code on an \'ok\' value. Check the code value " | ||
| "first."); | ||
| } | ||
| case ViamErrorCode::permission_denied: { | ||
| return PermissionDeniedException(); | ||
| } | ||
| case ViamErrorCode::duplicate_resource: { | ||
| return DuplicateResourceException(); | ||
| } | ||
| case ViamErrorCode::unimplemented: { | ||
| return UnimplementedException(); | ||
| } | ||
| case ViamErrorCode::validation: { | ||
| return ValidationException(); | ||
| } | ||
| case ViamErrorCode::connection: { | ||
| return ConnectionException(); | ||
| } | ||
| case ViamErrorCode::unknown: | ||
| default: { | ||
| return ViamException(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ViamErrorCode ViamException::get_error_code() const noexcept { | ||
| return error_code_; | ||
| } | ||
|
|
||
| PermissionDeniedException::PermissionDeniedException(const std::string& what) | ||
| : ViamException("PermissionDenied", ViamErrorCode::permission_denied, what){}; | ||
|
|
||
| DuplicateResourceException::DuplicateResourceException(const std::string& what) | ||
| : ViamException("DuplicateResource", ViamErrorCode::duplicate_resource, what){}; | ||
|
|
||
| UnimplementedException::UnimplementedException(const std::string& what) | ||
| : ViamException("Unimplemented", ViamErrorCode::unimplemented, what){}; | ||
|
|
||
| ValidationException::ValidationException(const std::string& what) | ||
| : ViamException("Validation", ViamErrorCode::validation, what){}; | ||
|
|
||
| ConnectionException::ConnectionException(const std::string& what) | ||
| : ViamException("Connection", ViamErrorCode::connection, what){}; | ||
|
|
||
| } // 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,69 @@ | ||
| #pragma once | ||
| #include <grpc/status.h> | ||
| #include <grpcpp/impl/codegen/status.h> | ||
| #include <iostream> | ||
| #include <sstream> | ||
| #include <stdexcept> | ||
| #include <string> | ||
|
|
||
| namespace viam { | ||
| namespace sdk { | ||
|
|
||
| enum class ViamErrorCode : uint8_t { | ||
| // Used to indicate no error | ||
| ok = 0, | ||
| // Default error code for ViamException | ||
| unknown = 1, | ||
| permission_denied = 2, | ||
| duplicate_resource = 3, | ||
| unimplemented = 4, | ||
| validation = 5, | ||
| connection = 6 | ||
| }; | ||
|
|
||
| class ViamException : public std::runtime_error { | ||
| public: | ||
| explicit ViamException(const std::string& what = "unknown"); | ||
|
|
||
| explicit ViamException(const std::string& type, | ||
| const ViamErrorCode& code, | ||
| const std::string& what); | ||
|
|
||
| explicit ViamException(const ::grpc::Status& status_code); | ||
|
|
||
| static ViamException from_viam_error_code(ViamErrorCode code); | ||
|
|
||
| virtual ~ViamException(); | ||
|
|
||
| ViamErrorCode get_error_code() const noexcept; | ||
|
|
||
| private: | ||
| ViamErrorCode error_code_; | ||
| }; | ||
|
|
||
| class PermissionDeniedException : public ViamException { | ||
| public: | ||
| explicit PermissionDeniedException(const std::string& what = "unknown"); | ||
| }; | ||
|
|
||
| class DuplicateResourceException : public ViamException { | ||
| public: | ||
| explicit DuplicateResourceException(const std::string& what = "unknown"); | ||
| }; | ||
|
|
||
| class UnimplementedException : public ViamException { | ||
| public: | ||
| explicit UnimplementedException(const std::string& what = "unknown"); | ||
| }; | ||
|
|
||
| class ValidationException : public ViamException { | ||
| public: | ||
| explicit ValidationException(const std::string& what = "unknown"); | ||
| }; | ||
|
|
||
| class ConnectionException : public ViamException { | ||
| public: | ||
| explicit ConnectionException(const std::string& what = "unknown"); | ||
| }; | ||
| } // 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
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.