From 7549258f00b628a1bbc6e017bdfc0c8776e778e9 Mon Sep 17 00:00:00 2001 From: Kamil Cudnik Date: Fri, 12 Jan 2018 20:23:40 +0100 Subject: [PATCH] Change all throws to SWSS_LOG_THROW (#280) --- meta/saiattributelist.cpp | 6 +- meta/saiserialize.cpp | 133 ++++++++++++-------------------------- meta/tests.cpp | 3 +- syncd/syncd.cpp | 6 +- 4 files changed, 49 insertions(+), 99 deletions(-) diff --git a/meta/saiattributelist.cpp b/meta/saiattributelist.cpp index e36a7ec34b7c..89a34da000d6 100644 --- a/meta/saiattributelist.cpp +++ b/meta/saiattributelist.cpp @@ -28,8 +28,7 @@ SaiAttributeList::SaiAttributeList( if (meta == NULL) { - SWSS_LOG_ERROR("FATAL: failed to find metadata for object type %d and attr id %d", object_type, attr.id); - throw std::runtime_error("failed to get metadata"); + SWSS_LOG_THROW("FATAL: failed to find metadata for object type %d and attr id %d", object_type, attr.id); } sai_deserialize_attr_value(str_attr_value, *meta, attr, countOnly); @@ -69,8 +68,7 @@ std::vector SaiAttributeList::serialize_attr_list( if (meta == NULL) { - SWSS_LOG_ERROR("FATAL: failed to find metadata for object type %d and attr id %d", object_type, attr->id); - throw std::runtime_error("failed to get metadata"); + SWSS_LOG_THROW("FATAL: failed to find metadata for object type %d and attr id %d", object_type, attr->id); } std::string str_attr_id = sai_serialize_attr_id(*meta); diff --git a/meta/saiserialize.cpp b/meta/saiserialize.cpp index d6eb626d3a5a..890414403a73 100644 --- a/meta/saiserialize.cpp +++ b/meta/saiserialize.cpp @@ -23,8 +23,7 @@ int char_to_int( if (c >= 'a' && c <= 'f') return c - 'a' + 10; - SWSS_LOG_ERROR("unable to convert char %d to int", c); - throw std::runtime_error("unable to convert char to int"); + SWSS_LOG_THROW("unable to convert char %d to int", c); } template @@ -84,8 +83,7 @@ sai_status_t transfer_list( { if (src_element.list == NULL && src_element.count > 0) { - SWSS_LOG_ERROR("source list is NULL when count is %u, wrong db insert?", src_element.count); - throw std::runtime_error("source list is NULL when count is not zero, wrong db insert?"); + SWSS_LOG_THROW("source list is NULL when count is %u, wrong db insert?", src_element.count); } transfer_primitive(src_element.count, dst_element.count); @@ -389,17 +387,14 @@ sai_status_t transfer_attributes( if (src_attr.id != dst_attr.id) { - SWSS_LOG_ERROR("src vs dst attr id don't match GET mismatch"); - throw std::runtime_error("src vs dst attr id don't match GET mismatch"); + SWSS_LOG_THROW("src vs dst attr id don't match GET mismatch"); } if (meta == NULL) { - SWSS_LOG_ERROR("unable to get metadata for object type %s, attribute %d", + SWSS_LOG_THROW("unable to get metadata for object type %s, attribute %d", sai_serialize_object_type(object_type).c_str(), src_attr.id); - - throw std::runtime_error("unable to get metadata"); } RETURN_ON_ERROR(transfer_attribute(meta->attrvaluetype, src_attr, dst_attr, countOnly)); @@ -427,8 +422,7 @@ uint8_t get_ip_mask( if (zeros && bit) { - SWSS_LOG_ERROR("FATAL: invalid ipv%d mask", ipv6 ? 6 : 4); - throw std::runtime_error("invalid ip mask"); + SWSS_LOG_THROW("FATAL: invalid ipv%d mask", ipv6 ? 6 : 4); } zeros |= !bit; @@ -467,14 +461,12 @@ void sai_populate_ip_mask( if (mask == NULL) { - SWSS_LOG_ERROR("mask pointer is null"); - throw std::runtime_error("mask pointer is null"); + SWSS_LOG_THROW("mask pointer is null"); } if ((ipv6 && (bits > 128)) || (!ipv6 && (bits > 32))) { - SWSS_LOG_ERROR("invalid ip mask bits %u", bits); - throw std::runtime_error("invalid ip mask bits"); + SWSS_LOG_THROW("invalid ip mask bits %u", bits); } // zero all mask @@ -775,8 +767,7 @@ std::string sai_serialize_ipv4( if (inet_ntop(AF_INET, &(sa.sin_addr), buf, INET_ADDRSTRLEN) == NULL) { - SWSS_LOG_ERROR("FATAL: failed to convert IPv4 address, errno: %s", strerror(errno)); - throw std::runtime_error("failed to convert IPv4"); + SWSS_LOG_THROW("FATAL: failed to convert IPv4 address, errno: %s", strerror(errno)); } return buf; @@ -803,8 +794,7 @@ std::string sai_serialize_ipv6( if (inet_ntop(AF_INET6, &(sa6.sin6_addr), buf, INET6_ADDRSTRLEN) == NULL) { - SWSS_LOG_ERROR("FATAL: failed to convert IPv6 address, errno: %s", strerror(errno)); - throw std::runtime_error("failed to convert IPv6"); + SWSS_LOG_THROW("FATAL: failed to convert IPv6 address, errno: %s", strerror(errno)); } return buf; @@ -827,8 +817,7 @@ std::string sai_serialize_ip_address( default: - SWSS_LOG_ERROR("FATAL: invalid ip address family: %d", ipaddress.addr_family); - throw std::runtime_error("invalid ip address family"); + SWSS_LOG_THROW("FATAL: invalid ip address family: %d", ipaddress.addr_family); } } @@ -1079,8 +1068,7 @@ std::string sai_serialize_acl_action( return sai_serialize_oid_list(action.parameter.objlist, countOnly); default: - SWSS_LOG_ERROR("FATAIL: invalid serialization type %d", meta.attrvaluetype); - throw std::runtime_error("serialization type is not supported"); + SWSS_LOG_THROW("FATAIL: invalid serialization type %d", meta.attrvaluetype); } } @@ -1139,8 +1127,7 @@ std::string sai_serialize_acl_field( return sai_serialize_number_list(field.data.u8list, countOnly) + "&mask:" + sai_serialize_number_list(field.mask.u8list, countOnly, true); default: - SWSS_LOG_ERROR("FATAIL: invalid serialization type %d", meta.attrvaluetype); - throw std::runtime_error("serialization type is not supported"); + SWSS_LOG_THROW("FATAIL: invalid serialization type %d", meta.attrvaluetype); } } @@ -1287,8 +1274,7 @@ std::string sai_serialize_attr_value( return sai_serialize_acl_capability(meta, attr.value.aclcapability, countOnly); default: - SWSS_LOG_ERROR("FATAL: invalid serialization type %d", meta.attrvaluetype); - throw std::runtime_error("serialization type is not supported"); + SWSS_LOG_THROW("FATAL: invalid serialization type %d", meta.attrvaluetype); } } @@ -1309,8 +1295,7 @@ std::string sai_serialize_ip_prefix( default: - SWSS_LOG_ERROR("FATAL: invalid ip prefix address family: %d", prefix.addr_family); - throw std::runtime_error("invalid ip address family"); + SWSS_LOG_THROW("FATAL: invalid ip prefix address family: %d", prefix.addr_family); } } @@ -1356,11 +1341,9 @@ json sai_serialize_json_fdb_event_notification_data( if (meta == NULL) { - SWSS_LOG_ERROR("unable to get metadata for object type %s, attribute %d", + SWSS_LOG_THROW("unable to get metadata for object type %s, attribute %d", sai_serialize_object_type(SAI_OBJECT_TYPE_FDB_ENTRY).c_str(), fdb_event.attr[i].id); - - throw std::runtime_error("unable to get metadata"); } json item; @@ -1385,8 +1368,7 @@ std::string sai_serialize_fdb_event_ntf( if (fdb_event == NULL) { - SWSS_LOG_ERROR("fdb_event pointer is null"); - throw std::runtime_error("fdb_event pointer is null"); + SWSS_LOG_THROW("fdb_event pointer is null"); } json j = json::array(); @@ -1410,8 +1392,7 @@ std::string sai_serialize_port_oper_status_ntf( if (port_oper_status == NULL) { - SWSS_LOG_ERROR("port_oper_status pointer is null"); - throw std::runtime_error("port_oper_status pointer is null"); + SWSS_LOG_THROW("port_oper_status pointer is null"); } json j = json::array(); @@ -1438,8 +1419,7 @@ std::string sai_serialize_queue_deadlock_ntf( if (deadlock_data == NULL) { - SWSS_LOG_ERROR("deadlock_data pointer is null"); - throw std::runtime_error("deadlock_data pointer is null"); + SWSS_LOG_THROW("deadlock_data pointer is null"); } json j = json::array(); @@ -1525,9 +1505,7 @@ void sai_deserialize_bool( return; } - SWSS_LOG_ERROR("failed to deserialize '%s' as bool", s.c_str()); - - throw std::runtime_error("bool deserialize failed"); + SWSS_LOG_THROW("failed to deserialize '%s' as bool", s.c_str()); } void sai_deserialize_chardata( @@ -1550,8 +1528,7 @@ void sai_deserialize_chardata( { if (i+1 >= len || ((s[i+1] != '\\') && (s[i+1] != 'x'))) { - SWSS_LOG_ERROR("invalid chardata %s", s.c_str()); - throw std::runtime_error("invalid chardata"); + SWSS_LOG_THROW("invalid chardata %s", s.c_str()); } if (s[i+1] == '\\') @@ -1565,8 +1542,7 @@ void sai_deserialize_chardata( if (i + 2 >= len) { - SWSS_LOG_ERROR("invalid chardata %s", s.c_str()); - throw std::runtime_error("invalid chardata"); + SWSS_LOG_THROW("invalid chardata %s", s.c_str()); } int h = char_to_int(s[i+1]); @@ -1586,8 +1562,7 @@ void sai_deserialize_chardata( if (len > CHAR_LEN) { - SWSS_LOG_ERROR("invalid chardata %s", s.c_str()); - throw std::runtime_error("invalid chardata"); + SWSS_LOG_THROW("invalid chardata %s", s.c_str()); } memcpy(chardata, deserialized.data(), len); @@ -1607,8 +1582,7 @@ void sai_deserialize_number( if (errno != 0 || endptr != s.c_str() + s.length()) { - SWSS_LOG_ERROR("invalid number %s", s.c_str()); - throw std::runtime_error("invalid number"); + SWSS_LOG_THROW("invalid number %s", s.c_str()); } } @@ -1652,8 +1626,7 @@ void sai_deserialize_mac( if (s.length() != (6*2+5)) { - SWSS_LOG_ERROR("invalid mac address %s", s.c_str()); - throw std::runtime_error("invalid mac number"); + SWSS_LOG_THROW("invalid mac address %s", s.c_str()); } int i = 0; @@ -1675,8 +1648,7 @@ void sai_deserialize_object_id( { if (s.find("oid:0x") != 0) { - SWSS_LOG_ERROR("invalid oid %s", s.c_str()); - throw std::runtime_error("invalid oid"); + SWSS_LOG_THROW("invalid oid %s", s.c_str()); } errno = 0; @@ -1687,8 +1659,7 @@ void sai_deserialize_object_id( if (errno != 0 || endptr != s.c_str() + s.length()) { - SWSS_LOG_ERROR("invalid oid %s", s.c_str()); - throw std::runtime_error("invalid oid"); + SWSS_LOG_THROW("invalid oid %s", s.c_str()); } } @@ -1711,8 +1682,7 @@ void sai_deserialize_list( if (pos == std::string::npos) { - SWSS_LOG_ERROR("invalid list %s", s.c_str()); - throw std::runtime_error("invalid list"); + SWSS_LOG_THROW("invalid list %s", s.c_str()); } std::string scount = s.substr(0, pos); @@ -1731,8 +1701,7 @@ void sai_deserialize_list( if (tokens.size() != list.count) { - SWSS_LOG_ERROR("invalid list count %lu != %u", tokens.size(), list.count); - throw std::runtime_error("invalid list count"); + SWSS_LOG_THROW("invalid list count %lu != %u", tokens.size(), list.count); } // list.list = sai_alloc_list(list.count, list); @@ -1838,8 +1807,7 @@ void sai_deserialize_qos_map_list( if (arr.size() != (size_t)qosmap.count) { - SWSS_LOG_ERROR("qos map count mismatch %lu vs %u", arr.size(), qosmap.count); - throw std::runtime_error("qos map count mismatch"); + SWSS_LOG_THROW("qos map count mismatch %lu vs %u", arr.size(), qosmap.count); } qosmap.list = sai_alloc_n_of_ptr_type(qosmap.count, qosmap.list); @@ -1903,8 +1871,7 @@ void sai_deserialize_tunnel_map_list( if (arr.size() != (size_t)tunnelmap.count) { - SWSS_LOG_ERROR("tunnel map count mismatch %lu vs %u", arr.size(), tunnelmap.count); - throw std::runtime_error("tunnel map count mismatch"); + SWSS_LOG_THROW("tunnel map count mismatch %lu vs %u", arr.size(), tunnelmap.count); } tunnelmap.list = sai_alloc_n_of_ptr_type(tunnelmap.count, tunnelmap.list); @@ -1925,8 +1892,7 @@ void sai_deserialize_ipv6( if (inet_pton(AF_INET6, s.c_str(), ipaddr) != 1) { - SWSS_LOG_ERROR("invalid ip address %s", s.c_str()); - throw std::runtime_error("invalid ip addredd"); + SWSS_LOG_THROW("invalid ip address %s", s.c_str()); } } @@ -1938,8 +1904,7 @@ void sai_deserialize_ipv4( if (inet_pton(AF_INET, s.c_str(), &ipaddr) != 1) { - SWSS_LOG_ERROR("invalid ip address %s", s.c_str()); - throw std::runtime_error("invalid ip address"); + SWSS_LOG_THROW("invalid ip address %s", s.c_str()); } } @@ -1970,8 +1935,7 @@ void sai_deserialize_ip_address( return; } - SWSS_LOG_ERROR("invalid ip address %s", s.c_str()); - throw std::runtime_error("invalid ip address"); + SWSS_LOG_THROW("invalid ip address %s", s.c_str()); } template @@ -1985,8 +1949,7 @@ void sai_deserialize_range( if (tokens.size() != 2) { - SWSS_LOG_ERROR("invalid range %s", s.c_str()); - throw std::runtime_error("invalid range"); + SWSS_LOG_THROW("invalid range %s", s.c_str()); } sai_deserialize_number(tokens[0], range.min); @@ -2084,8 +2047,7 @@ void sai_deserialize_acl_field( return sai_deserialize_number_list(field.data.u8list, countOnly) + "&mask:" + sai_deserialize_uint8_hex_list(field.mask.u8list, countOnly); */ default: - SWSS_LOG_ERROR("FATAIL: invalid serialization type %d", meta.attrvaluetype); - throw std::runtime_error("serialization type is not supported"); + SWSS_LOG_THROW("FATAIL: invalid serialization type %d", meta.attrvaluetype); } } @@ -2141,8 +2103,7 @@ void sai_deserialize_acl_action( return sai_deserialize_oid_list(s, action.parameter.objlist, countOnly); default: - SWSS_LOG_ERROR("FATAIL: invalid serialization type %d", meta.attrvaluetype); - throw std::runtime_error("serialization type is not supported"); + SWSS_LOG_THROW("FATAIL: invalid serialization type %d", meta.attrvaluetype); } } @@ -2275,8 +2236,7 @@ void sai_deserialize_attr_value( return sai_deserialize_acl_action(s, meta, attr.value.aclaction, countOnly); default: - SWSS_LOG_ERROR("deserialize type %d is not supportd yet FIXME", meta.attrvaluetype); - throw std::runtime_error("deserialize type is not supported yet FIXME"); + SWSS_LOG_THROW("deserialize type %d is not supportd yet FIXME", meta.attrvaluetype); } } @@ -2290,8 +2250,7 @@ void sai_deserialize_ip_prefix( if (tokens.size() != 2) { - SWSS_LOG_ERROR("invalid ip prefix %s", s.c_str()); - throw std::runtime_error("invalid ip prefix"); + SWSS_LOG_THROW("invalid ip prefix %s", s.c_str()); } uint8_t mask; @@ -2313,8 +2272,7 @@ void sai_deserialize_ip_prefix( } else { - SWSS_LOG_ERROR("invalid ip prefix %s", s.c_str()); - throw std::runtime_error("invalid ip prefix"); + SWSS_LOG_THROW("invalid ip prefix %s", s.c_str()); } } @@ -2456,16 +2414,14 @@ void sai_deserialize_attr_id( if (meta == NULL) { - SWSS_LOG_ERROR("meta pointer is null"); - throw std::runtime_error("meta pointer is null"); + SWSS_LOG_THROW("meta pointer is null"); } auto m = sai_metadata_get_attr_metadata_by_attr_id_name(s.c_str()); if (m == NULL) { - SWSS_LOG_ERROR("invalid attr id: %s", s.c_str()); - throw std::runtime_error("invalid attr id"); + SWSS_LOG_THROW("invalid attr id: %s", s.c_str()); } *meta = m; @@ -2743,8 +2699,7 @@ void sai_deserialize_free_attribute_value( break; default: - SWSS_LOG_ERROR("unsupported type %d on deserialize free, FIXME", type); - throw std::runtime_error("unsupported type on deserialize free, FIXME"); + SWSS_LOG_THROW("unsupported type %d on deserialize free, FIXME", type); } } @@ -2761,11 +2716,9 @@ void sai_deserialize_free_fdb_event( if (meta == NULL) { - SWSS_LOG_ERROR("unable to get metadata for object type %s, attribute %d", + SWSS_LOG_THROW("unable to get metadata for object type %s, attribute %d", sai_serialize_object_type(SAI_OBJECT_TYPE_FDB_ENTRY).c_str(), fdb_event.attr[i].id); - - throw std::runtime_error("unable to get metadata"); } sai_deserialize_free_attribute_value(meta->attrvaluetype, fdb_event.attr[i]); diff --git a/meta/tests.cpp b/meta/tests.cpp index 00d14a3ab8b8..ff1819755ae8 100644 --- a/meta/tests.cpp +++ b/meta/tests.cpp @@ -4081,8 +4081,7 @@ void deserialize_number( if (errno != 0 || endptr != s.c_str() + s.length()) { - SWSS_LOG_ERROR("invalid number %s", s.c_str()); - throw std::runtime_error("invalid number"); + SWSS_LOG_THROW("invalid number %s", s.c_str()); } } diff --git a/syncd/syncd.cpp b/syncd/syncd.cpp index 393bbf2ddfad..3ed64dd03430 100644 --- a/syncd/syncd.cpp +++ b/syncd/syncd.cpp @@ -2107,7 +2107,7 @@ sai_status_t handle_bulk_generic( sai_deserialize_object_id(object_ids[idx], meta_key.objectkey.key.object_id); break; default: - throw std::invalid_argument("object_type"); + SWSS_LOG_THROW("invalid object_type: %s", sai_serialize_object_type(object_type).c_str()); } if (api == (sai_common_api_t)SAI_COMMON_API_BULK_SET) @@ -2201,7 +2201,7 @@ sai_status_t processBulkEvent( if (isInitViewMode()) { - SWSS_LOG_ERROR("bulk api is not supported in init view mode", api); + SWSS_LOG_ERROR("bulk api (%d) is not supported in init view mode", api); exit_and_notify(EXIT_FAILURE); } @@ -2318,7 +2318,7 @@ sai_status_t processEvent( } else { - SWSS_LOG_THROW("api %s is not implemented", op.c_str()); + SWSS_LOG_THROW("api '%s' is not implemented", op.c_str()); } sai_object_type_t object_type;