diff --git a/src/viam/examples/modules/example_module.cpp b/src/viam/examples/modules/example_module.cpp index 4770a1c5c..b53db8df4 100644 --- a/src/viam/examples/modules/example_module.cpp +++ b/src/viam/examples/modules/example_module.cpp @@ -78,7 +78,7 @@ int MyModule::which_ = 0; int main(int argc, char** argv) { if (argc < 2) { - throw "need socket path as command line argument"; + throw std::runtime_error("need socket path as command line argument"); } // Use set_logger_severity_from_args to set the boost trivial logger's diff --git a/src/viam/sdk/CMakeLists.txt b/src/viam/sdk/CMakeLists.txt index b46770d83..5a6b79dd0 100644 --- a/src/viam/sdk/CMakeLists.txt +++ b/src/viam/sdk/CMakeLists.txt @@ -43,6 +43,7 @@ target_sources(viamsdk # consider making all necessary runtime values a single `context` that has to # be initialized within main before anything else happens? registry/registry.cpp + common/exception.cpp common/linear_algebra.cpp common/proto_type.cpp common/utils.cpp diff --git a/src/viam/sdk/common/exception.cpp b/src/viam/sdk/common/exception.cpp new file mode 100644 index 000000000..5f45a7f39 --- /dev/null +++ b/src/viam/sdk/common/exception.cpp @@ -0,0 +1,68 @@ +#include +#include +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 diff --git a/src/viam/sdk/common/exception.hpp b/src/viam/sdk/common/exception.hpp new file mode 100644 index 000000000..2fd2f359e --- /dev/null +++ b/src/viam/sdk/common/exception.hpp @@ -0,0 +1,69 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +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 diff --git a/src/viam/sdk/common/proto_type.cpp b/src/viam/sdk/common/proto_type.cpp index 1fa6f0340..2e395d698 100644 --- a/src/viam/sdk/common/proto_type.cpp +++ b/src/viam/sdk/common/proto_type.cpp @@ -10,6 +10,7 @@ #include #include +#include #include namespace viam { @@ -127,7 +128,7 @@ Value ProtoType::proto_value() { break; } default: { - throw std::runtime_error( + throw ViamException( "Invalid proto_value conversion type. This should never happen;\ please file a bug report."); } @@ -190,8 +191,9 @@ bool operator==(const ProtoType& lhs, const ProtoType& rhs) { return std::equal(lhs_vec.begin(), lhs_vec.end(), rhs_vec.begin(), rhs_vec.end(), pred); } default: { - throw "Invalid proto_value conversion type. This should never happen;\ - please file a bug report."; + throw ViamException( + "Invalid proto_value conversion type. This should never happen;\ + please file a bug report."); } } } diff --git a/src/viam/sdk/components/base/base.cpp b/src/viam/sdk/components/base/base.cpp index 59179600d..d025fe2ce 100644 --- a/src/viam/sdk/components/base/base.cpp +++ b/src/viam/sdk/components/base/base.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -32,7 +33,7 @@ std::shared_ptr Base::resource_registration() { const google::protobuf::ServiceDescriptor* sd = p->FindServiceByName(viam::component::base::v1::BaseService::service_full_name()); if (!sd) { - throw std::runtime_error("Unable to get service descriptor for the base service"); + throw ViamException("Unable to get service descriptor for the base service"); } return std::make_shared(sd); } diff --git a/src/viam/sdk/components/base/client.cpp b/src/viam/sdk/components/base/client.cpp index c8f6bd058..27844d0b0 100644 --- a/src/viam/sdk/components/base/client.cpp +++ b/src/viam/sdk/components/base/client.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -35,7 +36,7 @@ void BaseClient::move_straight(int64_t distance_mm, double mm_per_sec) { const grpc::Status status = stub_->MoveStraight(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } } @@ -51,7 +52,7 @@ void BaseClient::spin(double angle_deg, double degs_per_sec) { const grpc::Status status = stub_->Spin(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } } @@ -67,7 +68,7 @@ void BaseClient::set_power(const Vector3& linear, const Vector3& angular) { const grpc::Status status = stub_->SetPower(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } } @@ -83,7 +84,7 @@ void BaseClient::set_velocity(const Vector3& linear, const Vector3& angular) { const grpc::Status status = stub_->SetVelocity(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } } @@ -101,7 +102,7 @@ grpc::StatusCode BaseClient::stop() { const grpc::Status status = stub_->Stop(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return status.error_code(); } @@ -116,7 +117,7 @@ bool BaseClient::is_moving() { const grpc::Status status = stub_->IsMoving(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return response.is_moving(); } @@ -133,7 +134,7 @@ AttributeMap BaseClient::do_command(AttributeMap command) { const grpc::Status status = stub_->DoCommand(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return struct_to_map(response.result()); } diff --git a/src/viam/sdk/components/board/board.cpp b/src/viam/sdk/components/board/board.cpp index fcca9e95f..e5e542687 100644 --- a/src/viam/sdk/components/board/board.cpp +++ b/src/viam/sdk/components/board/board.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -32,7 +33,7 @@ std::shared_ptr Board::resource_registration() { const google::protobuf::ServiceDescriptor* sd = p->FindServiceByName(viam::component::board::v1::BoardService::service_full_name()); if (!sd) { - throw std::runtime_error("Unable to get service descriptor for the board service"); + throw ViamException("Unable to get service descriptor for the board service"); } return std::make_shared(sd); } @@ -74,7 +75,7 @@ Board::power_mode Board::from_proto(viam::component::board::v1::PowerMode proto) } case viam::component::board::v1::POWER_MODE_UNSPECIFIED: default: { - throw std::runtime_error("Invalid proto board power_mode to decode"); + throw ViamException("Invalid proto board power_mode to decode"); } } } @@ -112,7 +113,7 @@ viam::component::board::v1::PowerMode Board::to_proto(Board::power_mode power_mo return viam::component::board::v1::POWER_MODE_OFFLINE_DEEP; } default: { - throw std::runtime_error("Invalid board power_mode to encode"); + throw ViamException("Invalid board power_mode to encode"); } } } diff --git a/src/viam/sdk/components/board/client.cpp b/src/viam/sdk/components/board/client.cpp index 06569cbdc..32f91879b 100644 --- a/src/viam/sdk/components/board/client.cpp +++ b/src/viam/sdk/components/board/client.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -32,7 +33,7 @@ Board::status BoardClient::get_status() { const grpc::Status status = stub_->Status(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return from_proto(response.status()); } @@ -48,7 +49,7 @@ void BoardClient::set_gpio(const std::string& pin, bool high) { request.set_high(high); const grpc::Status status = stub_->SetGPIO(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } } @@ -63,7 +64,7 @@ bool BoardClient::get_gpio(const std::string& pin) { const grpc::Status status = stub_->GetGPIO(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return response.high(); } @@ -79,7 +80,7 @@ double BoardClient::get_pwm_duty_cycle(const std::string& pin) { const grpc::Status status = stub_->PWM(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return response.duty_cycle_pct(); } @@ -96,7 +97,7 @@ void BoardClient::set_pwm_duty_cycle(const std::string& pin, double duty_cycle_p const grpc::Status status = stub_->SetPWM(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } } @@ -111,7 +112,7 @@ uint64_t BoardClient::get_pwm_frequency(const std::string& pin) { const grpc::Status status = stub_->PWMFrequency(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return response.frequency_hz(); } @@ -128,7 +129,7 @@ void BoardClient::set_pwm_frequency(const std::string& pin, uint64_t frequency_h const grpc::Status status = stub_->SetPWMFrequency(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } } @@ -144,7 +145,7 @@ AttributeMap BoardClient::do_command(AttributeMap command) { const grpc::Status status = stub_->DoCommand(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return struct_to_map(response.result()); } @@ -160,7 +161,7 @@ Board::analog_value BoardClient::read_analog(const std::string& analog_reader_na const grpc::Status status = stub_->ReadAnalogReader(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return response.value(); } @@ -177,7 +178,7 @@ Board::digital_value BoardClient::read_digital_interrupt( const grpc::Status status = stub_->GetDigitalInterruptValue(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return response.value(); } @@ -197,7 +198,7 @@ void BoardClient::set_power_mode(power_mode power_mode, const grpc::Status status = stub_->SetPowerMode(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } } diff --git a/src/viam/sdk/components/camera/camera.cpp b/src/viam/sdk/components/camera/camera.cpp index 9c1557c4c..18438c58a 100644 --- a/src/viam/sdk/components/camera/camera.cpp +++ b/src/viam/sdk/components/camera/camera.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -34,7 +35,7 @@ std::shared_ptr Camera::resource_registration() { const google::protobuf::ServiceDescriptor* sd = p->FindServiceByName(viam::component::camera::v1::CameraService::service_full_name()); if (!sd) { - throw std::runtime_error("Unable to get service descriptor for the camera service"); + throw ViamException("Unable to get service descriptor for the camera service"); } return std::make_shared(sd); } diff --git a/src/viam/sdk/components/encoder/client.cpp b/src/viam/sdk/components/encoder/client.cpp index b6213b052..7febf0fe0 100644 --- a/src/viam/sdk/components/encoder/client.cpp +++ b/src/viam/sdk/components/encoder/client.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -33,7 +34,7 @@ Encoder::position EncoderClient::get_position(position_type position_type) { const grpc::Status status = stub_->GetPosition(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return from_proto(response); } @@ -48,7 +49,7 @@ void EncoderClient::reset_position() { const grpc::Status status = stub_->ResetPosition(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } } @@ -62,7 +63,7 @@ Encoder::properties EncoderClient::get_properties() { const grpc::Status status = stub_->GetProperties(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return from_proto(response); } @@ -79,7 +80,7 @@ AttributeMap EncoderClient::do_command(AttributeMap command) { const grpc::Status status = stub_->DoCommand(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return struct_to_map(response.result()); } diff --git a/src/viam/sdk/components/encoder/encoder.cpp b/src/viam/sdk/components/encoder/encoder.cpp index 9e8fdeb14..663234499 100644 --- a/src/viam/sdk/components/encoder/encoder.cpp +++ b/src/viam/sdk/components/encoder/encoder.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -33,7 +34,7 @@ std::shared_ptr Encoder::resource_registration() { const google::protobuf::ServiceDescriptor* sd = p->FindServiceByName(viam::component::encoder::v1::EncoderService::service_full_name()); if (!sd) { - throw std::runtime_error("Unable to get service descriptor for the encoder service"); + throw ViamException("Unable to get service descriptor for the encoder service"); } return std::make_shared(sd); } @@ -58,7 +59,7 @@ Encoder::position_type Encoder::from_proto(viam::component::encoder::v1::Positio return Encoder::position_type::ticks_count; } default: { - throw std::runtime_error("Invalid proto encoder type to decode"); + throw ViamException("Invalid proto encoder type to decode"); } } } @@ -91,7 +92,7 @@ viam::component::encoder::v1::PositionType Encoder::to_proto(position_type posit return viam::component::encoder::v1::POSITION_TYPE_TICKS_COUNT; } default: { - throw std::runtime_error("Invalid proto encoder type to encode"); + throw ViamException("Invalid proto encoder type to encode"); } } } diff --git a/src/viam/sdk/components/generic/generic.cpp b/src/viam/sdk/components/generic/generic.cpp index 791e4751c..41d0b1824 100644 --- a/src/viam/sdk/components/generic/generic.cpp +++ b/src/viam/sdk/components/generic/generic.cpp @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -34,7 +35,7 @@ std::shared_ptr Generic::resource_registration() { const google::protobuf::ServiceDescriptor* sd = p->FindServiceByName(viam::component::generic::v1::GenericService::service_full_name()); if (!sd) { - throw std::runtime_error("Unable to get service descriptor for the generic service"); + throw ViamException("Unable to get service descriptor for the generic service"); } return std::make_shared(sd); } diff --git a/src/viam/sdk/components/motor/client.cpp b/src/viam/sdk/components/motor/client.cpp index f4991fe7b..49b5b310a 100644 --- a/src/viam/sdk/components/motor/client.cpp +++ b/src/viam/sdk/components/motor/client.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -33,7 +34,7 @@ void MotorClient::set_power(double power_pct) { const grpc::Status status = stub_->SetPower(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } } @@ -49,7 +50,7 @@ void MotorClient::go_for(double rpm, double revolutions) { const grpc::Status status = stub_->GoFor(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } } @@ -65,7 +66,7 @@ void MotorClient::go_to(double rpm, double position_revolutions) { const grpc::Status status = stub_->GoTo(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } } @@ -80,7 +81,7 @@ void MotorClient::reset_zero_position(double offset) { const grpc::Status status = stub_->ResetZeroPosition(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } } @@ -94,7 +95,7 @@ Motor::position MotorClient::get_position() { const grpc::Status status = stub_->GetPosition(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return from_proto(response); } @@ -109,7 +110,7 @@ Motor::properties MotorClient::get_properties() { const grpc::Status status = stub_->GetProperties(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return from_proto(response); } @@ -136,7 +137,7 @@ Motor::power_status MotorClient::get_power_status() { const grpc::Status status = stub_->IsPowered(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return from_proto(response); } @@ -151,7 +152,7 @@ bool MotorClient::is_moving() { const grpc::Status status = stub_->IsMoving(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return response.is_moving(); } @@ -168,7 +169,7 @@ AttributeMap MotorClient::do_command(AttributeMap command) { const grpc::Status status = stub_->DoCommand(&ctx, request, &response); if (!status.ok()) { - throw std::runtime_error(status.error_message()); + throw ViamException(status); } return struct_to_map(response.result()); } diff --git a/src/viam/sdk/components/motor/motor.cpp b/src/viam/sdk/components/motor/motor.cpp index e4c319102..7e285ff08 100644 --- a/src/viam/sdk/components/motor/motor.cpp +++ b/src/viam/sdk/components/motor/motor.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -31,7 +32,7 @@ std::shared_ptr Motor::resource_registration() { const google::protobuf::ServiceDescriptor* sd = p->FindServiceByName(viam::component::motor::v1::MotorService::service_full_name()); if (!sd) { - throw std::runtime_error("Unable to get service descriptor for the motor service"); + throw ViamException("Unable to get service descriptor for the motor service"); } return std::make_shared(sd); } diff --git a/src/viam/sdk/components/motor/motor.hpp b/src/viam/sdk/components/motor/motor.hpp index f9593c8e7..df116f864 100644 --- a/src/viam/sdk/components/motor/motor.hpp +++ b/src/viam/sdk/components/motor/motor.hpp @@ -94,23 +94,23 @@ class Motor : public Component { /// `revolutions` == 0, this will run the motor at `rpm` indefinetely. If `revolutions` != 0, /// this will block until the number of revolutions has been completed or another operation /// comes in. - /// @throws runtime_error if position reporting is not supported + /// @throws `ViamException` if position reporting is not supported virtual void go_for(double rpm, double revolutions) = 0; /// @brief Move the motor to a specific position that is relative to its /// home position at a specified speed which is expressed in RPM. /// @param rpm Speed of motor travel in rotations per minute /// @param position_revolutions Number of revolutions relative to motor's home home/zero - /// @throws runtime_error if position reporting is not supported + /// @throws `ViamException` if position reporting is not supported virtual void go_to(double rpm, double position_revolutions) = 0; /// @brief Sets the current position of the motor as the new zero position. /// @param offset Motor position - /// @throws runtime_error if position reporting is not supported + /// @throws `ViamException` if position reporting is not supported virtual void reset_zero_position(double offset) = 0; /// @brief Reports the position of the robot's motor relative to its zero position. - /// @throws runtime_error if position reporting is not supported + /// @throws `ViamException` if position reporting is not supported virtual position get_position() = 0; /// @brief Returns the properties of the motor which comprises the booleans indicating diff --git a/src/viam/sdk/config/resource.cpp b/src/viam/sdk/config/resource.cpp index cd021e683..cdf02d5cf 100644 --- a/src/viam/sdk/config/resource.cpp +++ b/src/viam/sdk/config/resource.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -18,11 +19,7 @@ namespace viam { namespace sdk { Name ResourceConfig::resource_name() { - try { - this->fix_api(); - } catch (std::string err) { // NOLINT - throw err; - } + this->fix_api(); std::vector remotes; // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) boost::split(remotes, this->name_, boost::is_any_of(":")); @@ -88,7 +85,7 @@ void ResourceConfig::fix_api() { // config structs if (this->api_.type_namespace() != this->namespace__ || this->api_.resource_subtype() != this->type_) { - throw "component namespace and/or type do not match component api field"; + throw ViamException("component namespace and/or type do not match component api field"); } } @@ -104,11 +101,7 @@ ResourceConfig ResourceConfig::from_proto(viam::app::v1::ComponentConfig proto_c } resource.model_ = Model::from_str(proto_cfg.model()); - try { - resource.fix_api(); - } catch (std::string err) { // NOLINT - throw err; - } + resource.fix_api(); if (proto_cfg.has_frame()) { resource.frame_ = LinkConfig::from_proto(proto_cfg.frame()); diff --git a/src/viam/sdk/registry/registry.cpp b/src/viam/sdk/registry/registry.cpp index ac15e142a..df1d7d021 100644 --- a/src/viam/sdk/registry/registry.cpp +++ b/src/viam/sdk/registry/registry.cpp @@ -12,6 +12,7 @@ #include +#include #include #include #include @@ -40,7 +41,7 @@ void Registry::register_model(std::shared_ptr resource) { const std::string reg_key = resource->api().to_string() + "/" + resource->model().to_string(); if (resources_.find(reg_key) != resources_.end()) { const std::string err = "Cannot add resource with name " + reg_key + "as it already exists"; - throw std::runtime_error(err); + throw DuplicateResourceException(err); } resources_.emplace(reg_key, resource); @@ -49,7 +50,7 @@ void Registry::register_model(std::shared_ptr resource) { void Registry::register_resource(API api, std::shared_ptr resource_registration) { if (apis_.find(api) != apis_.end()) { - throw std::runtime_error("Cannot add api " + api.to_string() + " as it already exists"); + throw ViamException("Cannot add api " + api.to_string() + " as it already exists"); } apis_.emplace(std::move(api), std::move(resource_registration)); diff --git a/src/viam/sdk/registry/registry.hpp b/src/viam/sdk/registry/registry.hpp index 9c99db5bf..cd815faa9 100644 --- a/src/viam/sdk/registry/registry.hpp +++ b/src/viam/sdk/registry/registry.hpp @@ -120,7 +120,7 @@ class Registry { public: /// @brief Registers a resource with the Registry. /// @param resource An object containing resource registration information. - /// @throws `std::runtime_error` if the resource has already been registered. + /// @throws `DuplicateResourceException` if the resource has already been registered. static void register_model(std::shared_ptr resource); /// @brief Lookup a given registered resource. diff --git a/src/viam/sdk/resource/resource_api.cpp b/src/viam/sdk/resource/resource_api.cpp index 782b64149..2350a4eee 100644 --- a/src/viam/sdk/resource/resource_api.cpp +++ b/src/viam/sdk/resource/resource_api.cpp @@ -10,6 +10,7 @@ #include +#include #include #include @@ -66,7 +67,7 @@ API API::from_string(std::string api) { boost::split(api_parts, api, boost::is_any_of(":")); return {api_parts.at(0), api_parts.at(1), api_parts.at(2)}; } - throw "string " + api + " is not a valid api name"; + throw ViamException("string " + api + " is not a valid api name"); } API::API(APIType type, std::string resource_subtype) @@ -121,7 +122,7 @@ viam::common::v1::ResourceName Name::to_proto() const { Name Name::from_string(std::string name) { if (!std::regex_match(name, NAME_REGEX)) { - throw "Received invalid Name string: " + name; + throw ViamException("Received invalid Name string: " + name); } std::vector slash_splits; boost::split(slash_splits, name, boost::is_any_of("/")); @@ -201,7 +202,7 @@ Model Model::from_str(std::string model) { if (std::regex_match(model, SINGLE_FIELD_REGEX)) { return {"rdk", "builtin", model}; } - throw std::runtime_error("string " + model + " is not a valid model name"); + throw ViamException("string " + model + " is not a valid model name"); } std::string ModelFamily::to_string() const { diff --git a/src/viam/sdk/resource/resource_api.hpp b/src/viam/sdk/resource/resource_api.hpp index e8df2397a..24214622f 100644 --- a/src/viam/sdk/resource/resource_api.hpp +++ b/src/viam/sdk/resource/resource_api.hpp @@ -116,7 +116,7 @@ class Model { /// @brief Parses a single model string into a Model, using default values for namespace and /// family if not provided. /// - /// @throws `std::runtime_error` if given an invalid model (i.e., one with non-word characters). + /// @throws `ViamException` if given an invalid model (i.e., one with non-word characters). static Model from_str(std::string model); friend bool operator==(const Model& lhs, const Model& rhs); diff --git a/src/viam/sdk/resource/resource_manager.cpp b/src/viam/sdk/resource/resource_manager.cpp index 201b82856..036b90fdf 100644 --- a/src/viam/sdk/resource/resource_manager.cpp +++ b/src/viam/sdk/resource/resource_manager.cpp @@ -14,6 +14,7 @@ #include +#include #include #include #include @@ -67,7 +68,7 @@ std::string get_shortcut_name(const std::string& name) { void ResourceManager::do_add(const Name& name, const std::shared_ptr& resource) { if (name.name().empty()) { - throw "Empty name used for resource: " + name.to_string(); + throw ViamException("Empty name used for resource: " + name.to_string()); } const std::string short_name = name.short_name(); @@ -76,7 +77,7 @@ void ResourceManager::do_add(const Name& name, const std::shared_ptr& void ResourceManager::do_add(const std::string& name, const std::shared_ptr& resource) { if (resources_.find(name) != resources_.end()) { - throw "Attempted to add resource that already existed: " + name; + throw DuplicateResourceException("Attempted to add resource that already existed: " + name); } resources_.emplace(name, resource); @@ -103,7 +104,8 @@ void ResourceManager::add(const Name& name, const std::shared_ptr& res void ResourceManager::do_remove(const Name& name) { const std::string short_name = name.short_name(); if (resources_.find(short_name) == resources_.end()) { - throw "attempted to remove resource " + name.to_string() + " but it didn't exist!"; + throw ViamException("attempted to remove resource " + name.to_string() + + " but it didn't exist!"); } resources_.erase(short_name); diff --git a/src/viam/sdk/resource/resource_manager.hpp b/src/viam/sdk/resource/resource_manager.hpp index 26929b5e0..f23835a18 100644 --- a/src/viam/sdk/resource/resource_manager.hpp +++ b/src/viam/sdk/resource/resource_manager.hpp @@ -24,7 +24,7 @@ class ResourceManager { public: /// @brief Returns a resource. /// @param name the name of the desired resource. - /// @throws `std::runtime_error` if the desired resource does not exist. + /// @throws `ViamException` if the desired resource does not exist. std::shared_ptr resource(const std::string& name); /// @brief Replaces all resources in the manager. diff --git a/src/viam/sdk/robot/client.hpp b/src/viam/sdk/robot/client.hpp index fed6f2658..135988f20 100644 --- a/src/viam/sdk/robot/client.hpp +++ b/src/viam/sdk/robot/client.hpp @@ -72,7 +72,7 @@ class RobotClient { /// @brief Lookup and return a `shared_ptr` to a resource. /// @param name The `ResourceName` of the resource. - /// @throws `std::runtime_error` if the requested resource doesn't exist or is the wrong type. + /// @throws `ViamException` if the requested resource doesn't exist or is the wrong type. /// @return a `shared_ptr` to the requested resource as an uncasted `Resource`. /// /// This method should not be called directly except in specific cases. The @@ -86,7 +86,7 @@ class RobotClient { template /// @brief Lookup and return a `shared_ptr` to a resource of the requested type. /// @param name The ordinary name of the resource. - /// @throws `std::runtime_error` if the requested resource doesn't exist or is the wrong type. + /// @throws `ViamException` if the requested resource doesn't exist or is the wrong type. /// @return a `shared_ptr` to the requested resource. std::shared_ptr resource_by_name(std::string name) { ResourceName r; diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index 02a68cb46..b08476460 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -12,6 +12,8 @@ #include #include +#include + extern "C" void* init_rust_runtime(); extern "C" int free_rust_runtime(void* ptr); extern "C" void free_string(const char* s); @@ -66,9 +68,7 @@ std::shared_ptr ViamChannel::dial(const char* uri, char* socket_path = ::dial(uri, payload, opts.allows_insecure_downgrade(), ptr); if (socket_path == NULL) { free_rust_runtime(ptr); - // TODO(RSDK-1742) Replace throwing of strings with throwing of - // runtime_error - throw "Unable to establish connecting path!"; + throw ViamException("Unable to establish connecting path!"); } std::string address("unix://"); diff --git a/src/viam/sdk/rpc/server.cpp b/src/viam/sdk/rpc/server.cpp index 60954bf2d..35548698c 100644 --- a/src/viam/sdk/rpc/server.cpp +++ b/src/viam/sdk/rpc/server.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -14,7 +15,7 @@ Server::~Server() { void Server::register_service(grpc::Service* service) { if (!builder_) { - throw "Cannot register a new service after the server has started"; + throw ViamException("Cannot register a new service after the server has started"); } builder_->RegisterService(service); @@ -22,7 +23,7 @@ void Server::register_service(grpc::Service* service) { void Server::start() { if (server_) { - throw std::runtime_error("Attempted to start server that was already running"); + throw ViamException("Attempted to start server that was already running"); } grpc::reflection::InitProtoReflectionServerBuilderPlugin(); @@ -33,7 +34,7 @@ void Server::start() { void Server::add_listening_port(std::string address, std::shared_ptr creds) { if (!builder_) { - throw "Cannot add a listening port after server has started"; + throw ViamException("Cannot add a listening port after server has started"); } if (!creds) { diff --git a/src/viam/sdk/rpc/server.hpp b/src/viam/sdk/rpc/server.hpp index eb86d44d9..fed3cb6ea 100644 --- a/src/viam/sdk/rpc/server.hpp +++ b/src/viam/sdk/rpc/server.hpp @@ -18,7 +18,7 @@ class Server { ~Server(); /// @brief Starts the grpc server. Can only be called once. - /// @throws `std::runtime_error` if the server was already `start`ed. + /// @throws `ViamException` if the server was already `start`ed. /// repeated calls. void start(); @@ -26,13 +26,13 @@ class Server { // grpc service type, and convert under the hood /// @brief Registers a gRPC service. /// @param service The gRPC service to be registered. - /// @throws `std::runtime_error` if called after the server has been `start`ed. + /// @throws `ViamException` if called after the server has been `start`ed. void register_service(grpc::Service* service); /// @brief Adds a listening port to the server. /// @param address The address to listen at. /// @param creds The server credentials; defaults to a insecure server credentials. - /// @throws `std::runtime_error` if called after the server has been `start`ed. + /// @throws `ViamException` if called after the server has been `start`ed. void add_listening_port(std::string address, std::shared_ptr creds = nullptr); diff --git a/src/viam/sdk/spatialmath/geometry.cpp b/src/viam/sdk/spatialmath/geometry.cpp index 243cce2c3..819f89e5a 100644 --- a/src/viam/sdk/spatialmath/geometry.cpp +++ b/src/viam/sdk/spatialmath/geometry.cpp @@ -5,6 +5,7 @@ #include +#include #include namespace viam { @@ -68,7 +69,7 @@ GeometryConfig GeometryConfig::from_proto(const viam::common::v1::Geometry& prot } case viam::common::v1::Geometry::GeometryTypeCase::GEOMETRY_TYPE_NOT_SET: default: { - throw "Geometry type is not supported"; + throw UnimplementedException("Geometry type is not supported"); } } } diff --git a/src/viam/sdk/spatialmath/orientation.cpp b/src/viam/sdk/spatialmath/orientation.cpp index 56c814986..29762f3fb 100644 --- a/src/viam/sdk/spatialmath/orientation.cpp +++ b/src/viam/sdk/spatialmath/orientation.cpp @@ -8,6 +8,7 @@ #include +#include #include namespace viam { @@ -81,7 +82,7 @@ OrientationConfig OrientationConfig::from_proto(proto::Orientation proto) { } case proto::Orientation::TypeCase::TYPE_NOT_SET: default: { - throw std::runtime_error("orientation type not known"); + throw ViamException("orientation type not known"); } } return cfg; @@ -141,7 +142,7 @@ proto::Orientation OrientationConfig::to_proto() const { return orientation; }; default: { - throw std::runtime_error("orientation type not known"); + throw ViamException("orientation type not known"); } } } diff --git a/src/viam/sdk/tests/mocks/generic_mocks.hpp b/src/viam/sdk/tests/mocks/generic_mocks.hpp index 1980b9529..8830b2b6e 100644 --- a/src/viam/sdk/tests/mocks/generic_mocks.hpp +++ b/src/viam/sdk/tests/mocks/generic_mocks.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -47,14 +48,14 @@ class MockGenericStub : public viam::component::generic::v1::GenericService::Stu ::grpc::ClientContext* context, const ::viam::common::v1::DoCommandRequest& request, ::grpc::CompletionQueue* cq) override { - throw std::runtime_error("Unimplemented"); + throw UnimplementedException("ClientAsycResponseReader"); }; ::grpc::ClientAsyncResponseReader<::viam::common::v1::DoCommandResponse>* PrepareAsyncDoCommandRaw(::grpc::ClientContext* context, const ::viam::common::v1::DoCommandRequest& request, ::grpc::CompletionQueue* cq) override { - throw std::runtime_error("Unimplemented"); + throw UnimplementedException("PrepareAsyncDoCommandRaw"); }; ::grpc::ClientAsyncResponseReaderInterface<::viam::common::v1::GetGeometriesResponse>* diff --git a/src/viam/sdk/tests/mocks/mock_motor.cpp b/src/viam/sdk/tests/mocks/mock_motor.cpp index 35a3852c0..ce4a1a25e 100644 --- a/src/viam/sdk/tests/mocks/mock_motor.cpp +++ b/src/viam/sdk/tests/mocks/mock_motor.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -24,7 +25,7 @@ void MockMotor::set_power(double power_pct) { void MockMotor::go_for(double rpm, double revolutions) { // This is the actual behavior from rdk:builtin:fake_motor if (rpm == 0.0) { - throw std::runtime_error("Cannot move motor at 0 RPM"); + throw ViamException("Cannot move motor at 0 RPM"); } position_ += revolutions; }; diff --git a/src/viam/sdk/tests/test_motor.cpp b/src/viam/sdk/tests/test_motor.cpp index c2d22c5ff..3110e039c 100644 --- a/src/viam/sdk/tests/test_motor.cpp +++ b/src/viam/sdk/tests/test_motor.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -85,7 +86,7 @@ BOOST_AUTO_TEST_CASE(mock_do_command) { BOOST_AUTO_TEST_CASE(mock_exception_creation) { std::shared_ptr motor = MockMotor::get_mock_motor(); - BOOST_CHECK_THROW(motor->go_for(0.0, 1.0), std::runtime_error); + BOOST_CHECK_THROW(motor->go_for(0.0, 1.0), ViamException); } BOOST_AUTO_TEST_SUITE_END() @@ -207,9 +208,8 @@ BOOST_AUTO_TEST_CASE(test_do_command) { } BOOST_AUTO_TEST_CASE(test_exception_creation) { - server_to_mock_pipeline([](Motor& client) -> void { - BOOST_CHECK_THROW(client.go_for(0.0, 1.0), std::runtime_error); - }); + server_to_mock_pipeline( + [](Motor& client) -> void { BOOST_CHECK_THROW(client.go_for(0.0, 1.0), ViamException); }); } BOOST_AUTO_TEST_SUITE_END()