Skip to content

Commit

Permalink
exception: add AccessException & make use of it
Browse files Browse the repository at this point in the history
  • Loading branch information
bkueng committed Feb 2, 2024
1 parent 9bc8368 commit 2bf8310
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
8 changes: 8 additions & 0 deletions ulog_cpp/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 12 additions & 12 deletions ulog_cpp/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ void Field::resolveDefinition(int offset)
std::shared_ptr<MessageFormat> 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;
}

std::shared_ptr<Field> 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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -235,7 +235,7 @@ Value::NativeTypeVariant Value::asNativeTypeVariant() const
return deserialize<char>(_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
Expand Down Expand Up @@ -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<uint8_t>(_backing_ref_begin, _backing_ref_end, _field_ref.offsetInMessage(),
Expand All @@ -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);
Expand All @@ -312,10 +312,10 @@ Value Value::operator[](const std::shared_ptr<Field>& 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);
Expand All @@ -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<size_t>(_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);
}
Expand Down
6 changes: 3 additions & 3 deletions ulog_cpp/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<NativeType>::value) {
// this is natively a vector
Expand All @@ -369,7 +369,7 @@ class Value {
if (arg.size() > 0) {
res = staticCastEnsureUnsignedChar<ReturnType>(arg[0]);
} else {
throw ParsingException("Cannot convert empty vector to non-vector type");
throw AccessException("Cannot convert empty vector to non-vector type");
}
}
} else {
Expand Down Expand Up @@ -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<int64_t>(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<uint8_t*>(&v));
Expand Down
4 changes: 2 additions & 2 deletions ulog_cpp/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 2bf8310

Please sign in to comment.