Add discrete exception type for gNMI errors

This commit is contained in:
Viacheslav Holovetskyi
2024-06-17 17:16:02 +03:00
committed by Kostiantyn Buravchenko
parent 1b869b9d9d
commit dffe061778
2 changed files with 13 additions and 8 deletions

View File

@@ -197,7 +197,7 @@ std::string gnmi_get(const std::string &yang_path)
if (!status.ok())
{
throw std::runtime_error{
throw gnmi_exception{
"gNMI get operation wasn't successful: "
+ status.error_message() + "; error code "
+ std::to_string(status.error_code())};
@@ -205,25 +205,25 @@ std::string gnmi_get(const std::string &yang_path)
if (gres.notification_size() != 1)
{
throw std::runtime_error{"Unsupported notification size"};
throw gnmi_exception{"Unsupported notification size"};
}
gnmi::Notification notification = gres.notification(0);
if (notification.update_size() != 1)
{
throw std::runtime_error{"Unsupported update size"};
throw gnmi_exception{"Unsupported update size"};
}
gnmi::Update update = notification.update(0);
if (!update.has_val())
{
throw std::runtime_error{"Empty value"};
throw gnmi_exception{"Empty value"};
}
gnmi::TypedValue value = update.val();
if (!value.has_json_ietf_val())
{
throw std::runtime_error{"Empty JSON value"};
throw gnmi_exception{"Empty JSON value"};
}
return value.json_ietf_val();
@@ -259,10 +259,10 @@ void gnmi_operation::execute()
if (!status.ok())
{
throw std::runtime_error{
throw gnmi_exception{
"gNMI set operation wasn't successful: "
+ status.error_message() + "; error code "
+ std::to_string(status.error_code())};
+ status.error_message() + "; error code "
+ std::to_string(status.error_code())};
}
}

View File

@@ -5,6 +5,7 @@
#include <gnmi.pb.h>
#include <stdexcept>
#include <string>
#include <vector>
@@ -15,6 +16,10 @@ split_string(std::string str, const std::string &delimiter);
void convert_yang_path_to_proto(std::string yang_path, gnmi::Path *proto_path);
class gnmi_exception : public std::runtime_error {
using std::runtime_error::runtime_error;
};
/**
* @brief Get value by specified path.
*