diff --git a/ulog_cpp/exception.hpp b/ulog_cpp/exception.hpp index f75cf6d..ddfd05d 100644 --- a/ulog_cpp/exception.hpp +++ b/ulog_cpp/exception.hpp @@ -34,4 +34,12 @@ class UsageException : public ExceptionBase { explicit UsageException(std::string reason) : ExceptionBase(std::move(reason)) {} }; +/** + * Some field/subscription does not exist or index is out of range + */ +class AccessException : public ExceptionBase { + public: + explicit AccessException(std::string reason) : ExceptionBase(std::move(reason)) {} +}; + } // namespace ulog_cpp diff --git a/ulog_cpp/messages.cpp b/ulog_cpp/messages.cpp index ab5d5bf..776b5f0 100644 --- a/ulog_cpp/messages.cpp +++ b/ulog_cpp/messages.cpp @@ -145,7 +145,7 @@ void Field::resolveDefinition(int offset) std::shared_ptr Field::nestedFormat() const { if (_type.type != Field::BasicType::NESTED) { - throw ParsingException("Not a nested type"); + throw AccessException("Not a nested type"); } return _type.nested_message; } @@ -153,7 +153,7 @@ std::shared_ptr Field::nestedFormat() const std::shared_ptr Field::nestedField(const std::string& name) const { if (_type.type != Field::BasicType::NESTED) { - throw ParsingException("Not a nested type"); + throw AccessException("Not a nested type"); } return _type.nested_message->field(name); } @@ -190,7 +190,7 @@ std::string Field::encode() const Value::NativeTypeVariant Value::asNativeTypeVariant() const { if (_array_index >= 0 && _field_ref.arrayLength() < 0) { - throw ParsingException("Can not access array element of non-array field"); + throw AccessException("Can not access array element of non-array field"); } if (_field_ref.arrayLength() == -1 || _array_index >= 0) { @@ -235,7 +235,7 @@ Value::NativeTypeVariant Value::asNativeTypeVariant() const return deserialize(_backing_ref_begin, _backing_ref_end, _field_ref.offsetInMessage(), array_offset); case Field::BasicType::NESTED: - throw ParsingException("Can't get nested field as basic type. Field " + _field_ref.name()); + throw AccessException("Can't get nested field as basic type. Field " + _field_ref.name()); } } else { // decode as an array @@ -276,14 +276,14 @@ Value::NativeTypeVariant Value::asNativeTypeVariant() const case Field::BasicType::CHAR: { auto string_start_iterator = _backing_ref_begin + _field_ref.offsetInMessage(); if (_backing_ref_end - string_start_iterator < _field_ref.arrayLength()) { - throw ParsingException("Decoding fault, memory too short"); + throw AccessException("Decoding fault, memory too short"); } int string_length = strnlen(string_start_iterator.base(), _field_ref.arrayLength()); return std::string(string_start_iterator, string_start_iterator + string_length); } case Field::BasicType::NESTED: - throw ParsingException("Can't get nested field as basic type. Field " + _field_ref.name()); + throw AccessException("Can't get nested field as basic type. Field " + _field_ref.name()); } } return deserialize(_backing_ref_begin, _backing_ref_end, _field_ref.offsetInMessage(), @@ -293,10 +293,10 @@ Value::NativeTypeVariant Value::asNativeTypeVariant() const Value Value::operator[](const Field& field) const { if (_field_ref.type().type != Field::BasicType::NESTED) { - throw ParsingException("Cannot access field of non-nested type"); + throw AccessException("Cannot access field of non-nested type"); } if (!_field_ref.definitionResolved()) { - throw ParsingException("Cannot access field of unresolved type"); + throw AccessException("Cannot access field of unresolved type"); } int submessage_offset = _field_ref.offsetInMessage() + ((_array_index >= 0) ? _field_ref.type().size * _array_index : 0); @@ -312,10 +312,10 @@ Value Value::operator[](const std::shared_ptr& field) const Value Value::operator[](const std::string& field_name) const { if (_field_ref.type().type != Field::BasicType::NESTED) { - throw ParsingException("Cannot access field of non-nested type"); + throw AccessException("Cannot access field of non-nested type"); } if (!_field_ref.definitionResolved()) { - throw ParsingException("Cannot access field of unresolved type"); + throw AccessException("Cannot access field of unresolved type"); } const auto& field = _field_ref.type().nested_message->field(field_name); return operator[](*field); @@ -324,10 +324,10 @@ Value Value::operator[](const std::string& field_name) const Value Value::operator[](size_t index) const { if (_field_ref.arrayLength() < 0) { - throw ParsingException("Cannot access field of non-array type"); + throw AccessException("Cannot access field of non-array type"); } if (index >= static_cast(_field_ref.arrayLength())) { - throw ParsingException("Index out of bounds"); + throw AccessException("Index out of bounds"); } return Value(_field_ref, _backing_ref_begin, _backing_ref_end, index); } diff --git a/ulog_cpp/messages.hpp b/ulog_cpp/messages.hpp index 5c07e19..a2815f7 100644 --- a/ulog_cpp/messages.hpp +++ b/ulog_cpp/messages.hpp @@ -347,7 +347,7 @@ class Value { res = arg; } else { // one is string, the other is not - throw ParsingException("Assign strings and non-string types"); + throw AccessException("Assign strings and non-string types"); } } else if constexpr (is_vector::value) { // this is natively a vector @@ -369,7 +369,7 @@ class Value { if (arg.size() > 0) { res = staticCastEnsureUnsignedChar(arg[0]); } else { - throw ParsingException("Cannot convert empty vector to non-vector type"); + throw AccessException("Cannot convert empty vector to non-vector type"); } } } else { @@ -431,7 +431,7 @@ class Value { int total_offset = offset + array_offset * sizeof(T); if (backing_start > backing_end || backing_end - backing_start - total_offset < static_cast(sizeof(v))) { - throw ParsingException("Unexpected data type size"); + throw AccessException("Unexpected data type size"); } std::copy(backing_start + total_offset, backing_start + total_offset + sizeof(v), reinterpret_cast(&v)); diff --git a/ulog_cpp/writer.cpp b/ulog_cpp/writer.cpp index 4b026d0..6af71fa 100644 --- a/ulog_cpp/writer.cpp +++ b/ulog_cpp/writer.cpp @@ -33,7 +33,7 @@ void Writer::messageInfo(const MessageInfo& message_info) void Writer::messageFormat(const MessageFormat& message_format) { if (_header_complete) { - throw ParsingException("Header completed, cannot write formats"); + throw UsageException("Header completed, cannot write formats"); } message_format.serialize(_data_write_cb); } @@ -48,7 +48,7 @@ void Writer::parameterDefault(const ParameterDefault& parameter_default) void Writer::addLoggedMessage(const AddLoggedMessage& add_logged_message) { if (!_header_complete) { - throw ParsingException("Header not yet completed, cannot write AddLoggedMessage"); + throw UsageException("Header not yet completed, cannot write AddLoggedMessage"); } add_logged_message.serialize(_data_write_cb); }