diff --git a/components/core/src/clp/clp/FileCompressor.cpp b/components/core/src/clp/clp/FileCompressor.cpp index b685b22ac9..64f9959ee2 100644 --- a/components/core/src/clp/clp/FileCompressor.cpp +++ b/components/core/src/clp/clp/FileCompressor.cpp @@ -451,9 +451,9 @@ bool FileCompressor::compress_ir_stream( ReaderInterface& reader ) { bool uses_four_byte_encoding{false}; - auto ir_error_code = ffi::ir_stream::get_encoding_type(reader, uses_four_byte_encoding); - if (ffi::ir_stream::IRErrorCode_Success != ir_error_code) { - SPDLOG_ERROR("Cannot compress {}, IR error={}", path, static_cast(ir_error_code)); + auto result = ffi::ir_stream::get_encoding_type(reader, uses_four_byte_encoding); + if (result.has_error()) { + SPDLOG_ERROR("Cannot compress {}, IR error={}", path, result.error().message()); return false; } diff --git a/components/core/src/clp/ffi/ir_stream/Deserializer.hpp b/components/core/src/clp/ffi/ir_stream/Deserializer.hpp index f9f212e669..59b1d8163c 100644 --- a/components/core/src/clp/ffi/ir_stream/Deserializer.hpp +++ b/components/core/src/clp/ffi/ir_stream/Deserializer.hpp @@ -239,19 +239,11 @@ auto Deserializer::create_generic( QueryHandlerType query_handler ) -> ystdlib::error_handling::Result { bool is_four_byte_encoded{}; - if (auto const err{get_encoding_type(reader, is_four_byte_encoded)}; - IRErrorCode::IRErrorCode_Success != err) - { - return ir_error_code_to_errc(err); - } + YSTDLIB_ERROR_HANDLING_TRYV(get_encoding_type(reader, is_four_byte_encoded)); std::vector metadata; encoded_tag_t metadata_type{}; - if (auto const err{deserialize_preamble(reader, metadata_type, metadata)}; - IRErrorCode::IRErrorCode_Success != err) - { - return ir_error_code_to_errc(err); - } + YSTDLIB_ERROR_HANDLING_TRYV(deserialize_preamble(reader, metadata_type, metadata)); if (cProtocol::Metadata::EncodingJson != metadata_type) { return std::errc::protocol_not_supported; @@ -293,10 +285,7 @@ auto Deserializer::deserialize_next_ir_unit( return std::errc::operation_not_permitted; } - encoded_tag_t tag{}; - if (auto const err{deserialize_tag(reader, tag)}; IRErrorCode::IRErrorCode_Success != err) { - return ir_error_code_to_errc(err); - } + auto tag{YSTDLIB_ERROR_HANDLING_TRYX(deserialize_tag(reader))}; auto const optional_ir_unit_type{get_ir_unit_type_from_tag(tag)}; if (false == optional_ir_unit_type.has_value()) { diff --git a/components/core/src/clp/ffi/ir_stream/IrDeserializationError.cpp b/components/core/src/clp/ffi/ir_stream/IrDeserializationError.cpp index 21d194ed27..1a53edbdc2 100644 --- a/components/core/src/clp/ffi/ir_stream/IrDeserializationError.cpp +++ b/components/core/src/clp/ffi/ir_stream/IrDeserializationError.cpp @@ -25,8 +25,12 @@ auto IrErrorCategory::message(IrDeserializationErrorEnum error_enum) const -> st return "incomplete IR stream"; case IrDeserializationErrorEnum::InvalidKeyGroupOrdering: return "invalid key-ID-group ordering"; + case IrDeserializationErrorEnum::InvalidMagicNumber: + return "IR stream starts with an invalid magic number"; case IrDeserializationErrorEnum::InvalidTag: return "invalid tag"; + case IrDeserializationErrorEnum::MessageDecodingFailure: + return "message decoding failed"; case IrDeserializationErrorEnum::UnsupportedMetadataFormat: return "IR stream metadata format unsupported"; case IrDeserializationErrorEnum::UnsupportedVersion: diff --git a/components/core/src/clp/ffi/ir_stream/IrDeserializationError.hpp b/components/core/src/clp/ffi/ir_stream/IrDeserializationError.hpp index a15c8bc1d8..f2c2e9e050 100644 --- a/components/core/src/clp/ffi/ir_stream/IrDeserializationError.hpp +++ b/components/core/src/clp/ffi/ir_stream/IrDeserializationError.hpp @@ -15,7 +15,9 @@ enum class IrDeserializationErrorEnum : uint8_t { EndOfStream, IncompleteStream, InvalidKeyGroupOrdering, + InvalidMagicNumber, InvalidTag, + MessageDecodingFailure, UnsupportedMetadataFormat, UnsupportedVersion, UnknownSchemaTreeNodeType, diff --git a/components/core/src/clp/ffi/ir_stream/decoding_methods.cpp b/components/core/src/clp/ffi/ir_stream/decoding_methods.cpp index c05806d532..0133b893f0 100644 --- a/components/core/src/clp/ffi/ir_stream/decoding_methods.cpp +++ b/components/core/src/clp/ffi/ir_stream/decoding_methods.cpp @@ -7,6 +7,7 @@ #include #include +#include #include "../../ir/types.hpp" #include "../EncodedTextAst.hpp" @@ -30,115 +31,98 @@ namespace { * @param reader * @param encoded_tag * @param string_blob The string blob to append the deserialized logtype to. - * @return IRErrorCode_Success on success. - * @return IRErrorCode_Corrupted_IR if the encoded tag is invalid. - * @return IRErrorCode_Incomplete_IR if the reader doesn't contain enough data to deserialize. + * @return A void result on success, or an error code indicating the failure: + * - IrDeserializationErrorEnum::IncompleteStream if the reader doesn't contain enough data to + * deserialize. + * - IrDeserializationErrorEnum::InvalidTag if the encoded tag is invalid. + * - Forwards `deserialize_int`'s return values on failure. */ [[nodiscard]] auto deserialize_and_append_logtype( ReaderInterface& reader, encoded_tag_t encoded_tag, StringBlob& string_blob -) -> IRErrorCode; +) -> ystdlib::error_handling::Result; /** * Deserializes a dictionary variable from the given reader and appends it to the given string blob. * @param reader * @param encoded_tag * @param string_blob The string blob to append the deserialized logtype to. - * @return IRErrorCode_Success on success. - * @return IRErrorCode_Corrupted_IR if the encoded tag is invalid. - * @return IRErrorCode_Incomplete_IR if the reader doesn't contain enough data to deserialize. + * @return A void result on success, or an error code indicating the failure: + * - IrDeserializationErrorEnum::IncompleteStream if the reader doesn't contain enough data to + * deserialize. + * - IrDeserializationErrorEnum::InvalidTag if the encoded tag is invalid. + * - Forwards `deserialize_int`'s return values on failure. */ [[nodiscard]] auto deserialize_and_append_dict_var( ReaderInterface& reader, encoded_tag_t encoded_tag, StringBlob& string_blob -) -> IRErrorCode; +) -> ystdlib::error_handling::Result; auto deserialize_and_append_logtype( ReaderInterface& reader, encoded_tag_t encoded_tag, StringBlob& string_blob -) -> IRErrorCode { +) -> ystdlib::error_handling::Result { size_t logtype_length{}; switch (encoded_tag) { case cProtocol::Payload::LogtypeStrLenUByte: { - uint8_t length{}; - if (false == deserialize_int(reader, length)) { - return IRErrorCode_Incomplete_IR; - } + auto length{YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader))}; logtype_length = length; break; } case cProtocol::Payload::LogtypeStrLenUShort: { - uint16_t length{}; - if (false == deserialize_int(reader, length)) { - return IRErrorCode_Incomplete_IR; - } + auto length{YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader))}; logtype_length = length; break; } case cProtocol::Payload::LogtypeStrLenInt: { // NOTE: Using `int32_t` to match `serialize_logtype`. - int32_t length{}; - if (false == deserialize_int(reader, length)) { - return IRErrorCode_Incomplete_IR; - } + auto length{YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader))}; logtype_length = length; break; } default: - return IRErrorCode_Corrupted_IR; + return IrDeserializationError{IrDeserializationErrorEnum::InvalidTag}; } auto const optional_error_code{string_blob.read_from(reader, logtype_length)}; if (optional_error_code.has_value()) { - return IRErrorCode_Incomplete_IR; + return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; } - return IRErrorCode_Success; + return ystdlib::error_handling::success(); } auto deserialize_and_append_dict_var( ReaderInterface& reader, encoded_tag_t encoded_tag, StringBlob& string_blob -) -> IRErrorCode { +) -> ystdlib::error_handling::Result { size_t dict_var_length{}; switch (encoded_tag) { case cProtocol::Payload::VarStrLenUByte: { - uint8_t length{}; - if (false == deserialize_int(reader, length)) { - return IRErrorCode_Incomplete_IR; - } - dict_var_length = length; + dict_var_length = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); break; } case cProtocol::Payload::VarStrLenUShort: { - uint16_t length{}; - if (false == deserialize_int(reader, length)) { - return IRErrorCode_Incomplete_IR; - } - dict_var_length = length; + dict_var_length = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); break; } case cProtocol::Payload::VarStrLenInt: { // NOTE: Using `int32_t` to match `DictionaryVariableHandler`. - int32_t length{}; - if (false == deserialize_int(reader, length)) { - return IRErrorCode_Incomplete_IR; - } - dict_var_length = length; + dict_var_length = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); break; } default: - return IRErrorCode_Corrupted_IR; + return IrDeserializationError{IrDeserializationErrorEnum::InvalidTag}; } auto const optional_error_code{string_blob.read_from(reader, dict_var_length)}; if (optional_error_code.has_value()) { - return IRErrorCode_Incomplete_IR; + return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; } - return IRErrorCode_Success; + return ystdlib::error_handling::success(); } } // namespace @@ -157,24 +141,28 @@ static bool is_variable_tag(encoded_tag_t tag, bool& is_encoded_var); * @param reader * @param encoded_tag * @param logtype Returns the logtype - * @return IRErrorCode_Success on success - * @return IRErrorCode_Corrupted_IR if reader contains invalid IR - * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize + * @return A void result on success, or an error code indicating the failure: + * - IrDeserializationErrorEnum::IncompleteStream if the reader doesn't contain enough data to + * deserialize. + * - IrDeserializationErrorEnum::InvalidTag if the encoded tag is invalid. + * - Forwards `deserialize_int`'s return values on failure. */ -static IRErrorCode -deserialize_logtype(ReaderInterface& reader, encoded_tag_t encoded_tag, string& logtype); +static auto deserialize_logtype(ReaderInterface& reader, encoded_tag_t encoded_tag, string& logtype) + -> ystdlib::error_handling::Result; /** * Deserializes a dictionary-type variable from the given reader * @param reader * @param encoded_tag - * @param dict_var Returns the dictionary variable - * @return IRErrorCode_Success on success - * @return IRErrorCode_Corrupted_IR if reader contains invalid IR - * @return IRErrorCode_Incomplete_IR if input buffer doesn't contain enough data to deserialize + * @return A result containing the dictionary variable on success, or an error code indicating the + * failure: + * - IrDeserializationErrorEnum::IncompleteStream if the input buffer doesn't contain enough data to + * deserialize. + * - IrDeserializationErrorEnum::InvalidTag if the encoded tag is invalid. + * - Forwards `deserialize_int`'s return values on failure. */ -static IRErrorCode -deserialize_dict_var(ReaderInterface& reader, encoded_tag_t encoded_tag, string& dict_var); +static auto deserialize_dict_var(ReaderInterface& reader, encoded_tag_t encoded_tag) + -> ystdlib::error_handling::Result; /** * Deserializes a timestamp from the given reader @@ -183,13 +171,16 @@ deserialize_dict_var(ReaderInterface& reader, encoded_tag_t encoded_tag, string& * @param encoded_tag * @param ts Returns the timestamp delta if encoded_variable_t == four_byte_encoded_variable_t or * the actual timestamp if encoded_variable_t == eight_byte_encoded_variable_t - * @return IRErrorCode_Success on success - * @return IRErrorCode_Corrupted_IR if reader contains invalid IR - * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize + * @return A void result on success, or an error code indicating the failure: + * - IrDeserializationErrorEnum::IncompleteStream if the reader doesn't contain enough data to + * deserialize. + * - IrDeserializationErrorEnum::InvalidTag if the encoded tag is invalid. + * - Forwards `deserialize_int`'s return values on failure. */ template -static IRErrorCode -deserialize_timestamp(ReaderInterface& reader, encoded_tag_t encoded_tag, epoch_time_ms_t& ts); +static auto +deserialize_timestamp(ReaderInterface& reader, encoded_tag_t encoded_tag, epoch_time_ms_t& ts) + -> ystdlib::error_handling::Result; /** * Deserializes the next log event from the given reader @@ -200,33 +191,34 @@ deserialize_timestamp(ReaderInterface& reader, encoded_tag_t encoded_tag, epoch_ * @param timestamp Returns the timestamp delta if * encoded_variable_t == four_byte_encoded_variable_t or the actual timestamp if * encoded_variable_t == eight_byte_encoded_variable_t - * @return IRErrorCode_Success on success - * @return IRErrorCode_Decode_Error if the log event cannot be properly deserialized - * @return Same as ffi::ir_stream::deserialize_log_event + * @return A void result on success, or an error code indicating the failure: + * - IrDeserializationErrorEnum::MessageDecodingFailure if the log event cannot be properly + * deserialized. + * - Forwards `deserialize_log_event`'s return values on failure. */ template -static IRErrorCode generic_deserialize_log_event( +static auto generic_deserialize_log_event( ReaderInterface& reader, encoded_tag_t encoded_tag, string& message, epoch_time_ms_t& timestamp -); +) -> ystdlib::error_handling::Result; /** * Deserializes metadata from the given reader * @param reader * @param metadata_type Returns the type of the metadata found in the IR - * @param metadata_pos Returns the starting position of the metadata in reader * @param metadata_size Returns the size of the metadata written in the IR - * @return IRErrorCode_Success on success - * @return IRErrorCode_Corrupted_IR if reader contains invalid IR - * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize + * @return A void result on success, or an error code indicating the failure: + * - IrDeserializationErrorEnum::IncompleteStream if reader doesn't contain enough data to + * deserialize. + * - IrDeserializationErrorEnum::UnsupportedMetadataFormat if the metadata length format is + * unsupported. + * - Forwards `deserialize_int`'s return values on failure. */ -static IRErrorCode deserialize_metadata( - ReaderInterface& reader, - encoded_tag_t& metadata_type, - uint16_t& metadata_size -); +static auto +deserialize_metadata(ReaderInterface& reader, encoded_tag_t& metadata_type, uint16_t& metadata_size) + -> ystdlib::error_handling::Result; template static bool is_variable_tag(encoded_tag_t tag, bool& is_encoded_var) { @@ -256,74 +248,51 @@ static bool is_variable_tag(encoded_tag_t tag, bool& is_encoded_var) { return false; } -static IRErrorCode -deserialize_logtype(ReaderInterface& reader, encoded_tag_t encoded_tag, string& logtype) { +static auto deserialize_logtype(ReaderInterface& reader, encoded_tag_t encoded_tag, string& logtype) + -> ystdlib::error_handling::Result { size_t logtype_length; if (encoded_tag == cProtocol::Payload::LogtypeStrLenUByte) { - uint8_t length; - if (false == deserialize_int(reader, length)) { - return IRErrorCode_Incomplete_IR; - } - logtype_length = length; + logtype_length = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else if (encoded_tag == cProtocol::Payload::LogtypeStrLenUShort) { - uint16_t length; - if (false == deserialize_int(reader, length)) { - return IRErrorCode_Incomplete_IR; - } - logtype_length = length; + logtype_length = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else if (encoded_tag == cProtocol::Payload::LogtypeStrLenInt) { - int32_t length; - if (false == deserialize_int(reader, length)) { - return IRErrorCode_Incomplete_IR; - } - logtype_length = length; + logtype_length = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else { - return IRErrorCode_Corrupted_IR; + return IrDeserializationError{IrDeserializationErrorEnum::InvalidTag}; } if (ErrorCode_Success != reader.try_read_string(logtype_length, logtype)) { - return IRErrorCode_Incomplete_IR; + return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; } - return IRErrorCode_Success; + return ystdlib::error_handling::success(); } -static IRErrorCode -deserialize_dict_var(ReaderInterface& reader, encoded_tag_t encoded_tag, string& dict_var) { +static auto deserialize_dict_var(ReaderInterface& reader, encoded_tag_t encoded_tag) + -> ystdlib::error_handling::Result { // Deserialize variable's length size_t var_length; if (cProtocol::Payload::VarStrLenUByte == encoded_tag) { - uint8_t length; - if (false == deserialize_int(reader, length)) { - return IRErrorCode_Incomplete_IR; - } - var_length = length; + var_length = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else if (cProtocol::Payload::VarStrLenUShort == encoded_tag) { - uint16_t length; - if (false == deserialize_int(reader, length)) { - return IRErrorCode_Incomplete_IR; - } - var_length = length; + var_length = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else if (cProtocol::Payload::VarStrLenInt == encoded_tag) { - int32_t length; - if (false == deserialize_int(reader, length)) { - return IRErrorCode_Incomplete_IR; - } - var_length = length; + var_length = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else { - return IRErrorCode_Corrupted_IR; + return IrDeserializationError{IrDeserializationErrorEnum::InvalidTag}; } // Read the dictionary variable + string dict_var; if (ErrorCode_Success != reader.try_read_string(var_length, dict_var)) { - return IRErrorCode_Incomplete_IR; + return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; } - - return IRErrorCode_Success; + return dict_var; } template -static IRErrorCode -deserialize_timestamp(ReaderInterface& reader, encoded_tag_t encoded_tag, epoch_time_ms_t& ts) { +static auto +deserialize_timestamp(ReaderInterface& reader, encoded_tag_t encoded_tag, epoch_time_ms_t& ts) + -> ystdlib::error_handling::Result { static_assert( is_same_v || is_same_v @@ -331,61 +300,40 @@ deserialize_timestamp(ReaderInterface& reader, encoded_tag_t encoded_tag, epoch_ if constexpr (is_same_v) { if (cProtocol::Payload::TimestampVal != encoded_tag) { - return IRErrorCode_Corrupted_IR; - } - if (false == deserialize_int(reader, ts)) { - return IRErrorCode_Incomplete_IR; + return IrDeserializationError{IrDeserializationErrorEnum::InvalidTag}; } + ts = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else { if (cProtocol::Payload::TimestampDeltaByte == encoded_tag) { - int8_t ts_delta; - if (false == deserialize_int(reader, ts_delta)) { - return IRErrorCode_Incomplete_IR; - } - ts = ts_delta; + ts = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else if (cProtocol::Payload::TimestampDeltaShort == encoded_tag) { - int16_t ts_delta; - if (false == deserialize_int(reader, ts_delta)) { - return IRErrorCode_Incomplete_IR; - } - ts = ts_delta; + ts = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else if (cProtocol::Payload::TimestampDeltaInt == encoded_tag) { - int32_t ts_delta; - if (false == deserialize_int(reader, ts_delta)) { - return IRErrorCode_Incomplete_IR; - } - ts = ts_delta; + ts = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else if (cProtocol::Payload::TimestampDeltaLong == encoded_tag) { - int64_t ts_delta; - if (false == deserialize_int(reader, ts_delta)) { - return IRErrorCode_Incomplete_IR; - } - ts = ts_delta; + ts = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else { - return IRErrorCode_Corrupted_IR; + return IrDeserializationError{IrDeserializationErrorEnum::InvalidTag}; } } - return IRErrorCode_Success; + return ystdlib::error_handling::success(); } template -static IRErrorCode generic_deserialize_log_event( +static auto generic_deserialize_log_event( ReaderInterface& reader, encoded_tag_t encoded_tag, string& message, epoch_time_ms_t& timestamp -) { +) -> ystdlib::error_handling::Result { message.clear(); vector encoded_vars; vector dict_vars; string logtype; - if (auto error_code - = deserialize_log_event(reader, encoded_tag, logtype, encoded_vars, dict_vars, timestamp); - IRErrorCode_Success != error_code) - { - return error_code; - } + YSTDLIB_ERROR_HANDLING_TRYV( + deserialize_log_event(reader, encoded_tag, logtype, encoded_vars, dict_vars, timestamp) + ); auto constant_handler = [&](string const& value, size_t begin_pos, size_t length) { message.append(value, begin_pos, length); @@ -411,44 +359,34 @@ static IRErrorCode generic_deserialize_log_event( dict_var_handler ); } catch (DecodingException const& e) { - return IRErrorCode_Decode_Error; + return IrDeserializationError{IrDeserializationErrorEnum::MessageDecodingFailure}; } - return IRErrorCode_Success; + return ystdlib::error_handling::success(); } -static IRErrorCode deserialize_metadata( - ReaderInterface& reader, - encoded_tag_t& metadata_type, - uint16_t& metadata_size -) { +static auto +deserialize_metadata(ReaderInterface& reader, encoded_tag_t& metadata_type, uint16_t& metadata_size) + -> ystdlib::error_handling::Result { if (ErrorCode_Success != reader.try_read_numeric_value(metadata_type)) { - return IRErrorCode_Incomplete_IR; + return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; } // Read metadata length encoded_tag_t encoded_tag; if (ErrorCode_Success != reader.try_read_numeric_value(encoded_tag)) { - return IRErrorCode_Incomplete_IR; + return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; } switch (encoded_tag) { case cProtocol::Metadata::LengthUByte: - uint8_t ubyte_res; - if (false == deserialize_int(reader, ubyte_res)) { - return IRErrorCode_Incomplete_IR; - } - metadata_size = ubyte_res; + metadata_size = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); break; case cProtocol::Metadata::LengthUShort: - uint16_t ushort_res; - if (false == deserialize_int(reader, ushort_res)) { - return IRErrorCode_Incomplete_IR; - } - metadata_size = ushort_res; + metadata_size = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); break; default: - return IRErrorCode_Corrupted_IR; + return IrDeserializationError{IrDeserializationErrorEnum::UnsupportedMetadataFormat}; } - return IRErrorCode_Success; + return ystdlib::error_handling::success(); } template @@ -459,29 +397,25 @@ auto deserialize_log_event( vector& encoded_vars, vector& dict_vars, epoch_time_ms_t& timestamp_or_timestamp_delta -) -> IRErrorCode { - if (auto const err - = deserialize_encoded_text_ast(reader, encoded_tag, logtype, encoded_vars, dict_vars); - IRErrorCode_Success != err) - { - return err; - } +) -> ystdlib::error_handling::Result { + YSTDLIB_ERROR_HANDLING_TRYV( + deserialize_encoded_text_ast(reader, encoded_tag, logtype, encoded_vars, dict_vars) + ); // NOTE: for the eight-byte encoding, the timestamp is the actual timestamp; for the four-byte // encoding, the timestamp is a timestamp delta if (ErrorCode_Success != reader.try_read_numeric_value(encoded_tag)) { - return IRErrorCode_Incomplete_IR; - } - if (auto error_code = deserialize_timestamp( - reader, - encoded_tag, - timestamp_or_timestamp_delta - ); - IRErrorCode_Success != error_code) - { - return error_code; + return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; } - return IRErrorCode_Success; + + YSTDLIB_ERROR_HANDLING_TRYV( + deserialize_timestamp( + reader, + encoded_tag, + timestamp_or_timestamp_delta + ) + ); + return ystdlib::error_handling::success(); } template @@ -491,38 +425,28 @@ auto deserialize_encoded_text_ast( std::string& logtype, std::vector& encoded_vars, std::vector& dict_vars -) -> IRErrorCode { +) -> ystdlib::error_handling::Result { // Handle variables - string var_str; bool is_encoded_var{false}; while (is_variable_tag(encoded_tag, is_encoded_var)) { if (is_encoded_var) { - encoded_variable_t encoded_variable; - if (false == deserialize_int(reader, encoded_variable)) { - return IRErrorCode_Incomplete_IR; - } + auto encoded_variable{ + YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)) + }; encoded_vars.push_back(encoded_variable); } else { - if (auto error_code = deserialize_dict_var(reader, encoded_tag, var_str); - IRErrorCode_Success != error_code) - { - return error_code; - } + auto var_str{YSTDLIB_ERROR_HANDLING_TRYX(deserialize_dict_var(reader, encoded_tag))}; dict_vars.emplace_back(var_str); } if (ErrorCode_Success != reader.try_read_numeric_value(encoded_tag)) { - return IRErrorCode_Incomplete_IR; + return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; } } // Handle logtype - if (auto error_code = deserialize_logtype(reader, encoded_tag, logtype); - IRErrorCode_Success != error_code) - { - return error_code; - } + YSTDLIB_ERROR_HANDLING_TRYV(deserialize_logtype(reader, encoded_tag, logtype)); - return IRErrorCode_Success; + return ystdlib::error_handling::success(); } template @@ -533,34 +457,21 @@ template bool is_encoded_var{}; while (is_variable_tag(encoded_tag, is_encoded_var)) { if (is_encoded_var) { - encoded_variable_t encoded_variable{}; - if (false == deserialize_int(reader, encoded_variable)) { - return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; - } + auto encoded_variable{ + YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)) + }; encoded_vars.push_back(encoded_variable); } else { - if (auto const error_code{ - deserialize_and_append_dict_var(reader, encoded_tag, string_blob) - }; - IRErrorCode_Success != error_code) - { - return IrDeserializationError{ - IrDeserializationErrorEnum::EncodedTextAstDeserializationFailure - }; - } + YSTDLIB_ERROR_HANDLING_TRYV( + deserialize_and_append_dict_var(reader, encoded_tag, string_blob) + ); } if (ErrorCode_Success != reader.try_read_numeric_value(encoded_tag)) { return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; } } - if (auto const error_code{deserialize_and_append_logtype(reader, encoded_tag, string_blob)}; - IRErrorCode_Success != error_code) - { - return IrDeserializationError{ - IrDeserializationErrorEnum::EncodedTextAstDeserializationFailure - }; - } + YSTDLIB_ERROR_HANDLING_TRYV(deserialize_and_append_logtype(reader, encoded_tag, string_blob)); return EncodedTextAst::create( std::move(encoded_vars), @@ -568,11 +479,12 @@ template ); } -IRErrorCode get_encoding_type(ReaderInterface& reader, bool& is_four_bytes_encoding) { +auto get_encoding_type(ReaderInterface& reader, bool& is_four_bytes_encoding) + -> ystdlib::error_handling::Result { char buffer[cProtocol::MagicNumberLength]; auto error_code = reader.try_read_exact_length(buffer, cProtocol::MagicNumberLength); if (error_code != ErrorCode_Success) { - return IRErrorCode_Incomplete_IR; + return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; } if (0 == memcmp(buffer, cProtocol::FourByteEncodingMagicNumber, cProtocol::MagicNumberLength)) { is_four_bytes_encoding = true; @@ -585,16 +497,9 @@ IRErrorCode get_encoding_type(ReaderInterface& reader, bool& is_four_bytes_encod { is_four_bytes_encoding = false; } else { - return IRErrorCode_Corrupted_IR; - } - return IRErrorCode_Success; -} - -IRErrorCode deserialize_tag(ReaderInterface& reader, encoded_tag_t& tag) { - if (ErrorCode_Success != reader.try_read_numeric_value(tag)) { - return IRErrorCode_Incomplete_IR; + return IrDeserializationError{IrDeserializationErrorEnum::InvalidMagicNumber}; } - return IRErrorCode_Success; + return ystdlib::error_handling::success(); } auto deserialize_tag(ReaderInterface& reader) -> ystdlib::error_handling::Result { @@ -605,35 +510,27 @@ auto deserialize_tag(ReaderInterface& reader) -> ystdlib::error_handling::Result return tag; } -IRErrorCode deserialize_preamble( +auto deserialize_preamble( ReaderInterface& reader, encoded_tag_t& metadata_type, size_t& metadata_pos, uint16_t& metadata_size -) { - if (auto error_code = deserialize_metadata(reader, metadata_type, metadata_size); - error_code != IRErrorCode_Success) - { - return error_code; - } +) -> ystdlib::error_handling::Result { + YSTDLIB_ERROR_HANDLING_TRYV(deserialize_metadata(reader, metadata_type, metadata_size)); metadata_pos = reader.get_pos(); if (ErrorCode_Success != reader.try_seek_from_begin(metadata_pos + metadata_size)) { - return IRErrorCode_Incomplete_IR; + return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; } - return IRErrorCode_Success; + return ystdlib::error_handling::success(); } -IRErrorCode deserialize_preamble( +auto deserialize_preamble( ReaderInterface& reader, encoded_tag_t& metadata_type, std::vector& metadata -) { +) -> ystdlib::error_handling::Result { uint16_t metadata_size{0}; - if (auto error_code = deserialize_metadata(reader, metadata_type, metadata_size); - error_code != IRErrorCode_Success) - { - return error_code; - } + YSTDLIB_ERROR_HANDLING_TRYV(deserialize_metadata(reader, metadata_type, metadata_size)); metadata.resize(metadata_size); if (ErrorCode_Success != reader.try_read_exact_length( @@ -641,9 +538,9 @@ IRErrorCode deserialize_preamble( metadata_size )) { - return IRErrorCode_Incomplete_IR; + return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; } - return IRErrorCode_Success; + return ystdlib::error_handling::success(); } auto validate_protocol_version(std::string_view protocol_version) -> IRProtocolErrorCode { @@ -682,31 +579,19 @@ auto validate_protocol_version(std::string_view protocol_version) -> IRProtocolE return IRProtocolErrorCode::Unsupported; } -IRErrorCode deserialize_utc_offset_change(ReaderInterface& reader, UtcOffset& utc_offset) { - int64_t serialized_utc_offset{}; - if (false == deserialize_int(reader, serialized_utc_offset)) { - return IRErrorCode_Incomplete_IR; - } - utc_offset = UtcOffset{serialized_utc_offset}; - return IRErrorCode_Success; -} - auto deserialize_utc_offset_change(ReaderInterface& reader) -> ystdlib::error_handling::Result { - int64_t serialized_utc_offset{}; - if (false == deserialize_int(reader, serialized_utc_offset)) { - return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; - } + auto serialized_utc_offset{YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader))}; return UtcOffset{serialized_utc_offset}; } namespace four_byte_encoding { -IRErrorCode deserialize_log_event( +auto deserialize_log_event( ReaderInterface& reader, encoded_tag_t encoded_tag, string& message, epoch_time_ms_t& timestamp_delta -) { +) -> ystdlib::error_handling::Result { return generic_deserialize_log_event( reader, encoded_tag, @@ -717,12 +602,12 @@ IRErrorCode deserialize_log_event( } // namespace four_byte_encoding namespace eight_byte_encoding { -IRErrorCode deserialize_log_event( +auto deserialize_log_event( ReaderInterface& reader, encoded_tag_t encoded_tag, string& message, epoch_time_ms_t& timestamp -) { +) -> ystdlib::error_handling::Result { return generic_deserialize_log_event( reader, encoded_tag, @@ -740,7 +625,7 @@ template auto deserialize_log_event( vector& encoded_vars, vector& dict_vars, epoch_time_ms_t& timestamp_or_timestamp_delta -) -> IRErrorCode; +) -> ystdlib::error_handling::Result; template auto deserialize_log_event( ReaderInterface& reader, @@ -749,7 +634,7 @@ template auto deserialize_log_event( vector& encoded_vars, vector& dict_vars, epoch_time_ms_t& timestamp_or_timestamp_delta -) -> IRErrorCode; +) -> ystdlib::error_handling::Result; template auto deserialize_encoded_text_ast( ReaderInterface& reader, @@ -757,7 +642,7 @@ template auto deserialize_encoded_text_ast( std::string& logtype, std::vector& encoded_vars, std::vector& dict_vars -) -> IRErrorCode; +) -> ystdlib::error_handling::Result; template auto deserialize_encoded_text_ast( ReaderInterface& reader, @@ -765,7 +650,7 @@ template auto deserialize_encoded_text_ast( std::string& logtype, std::vector& encoded_vars, std::vector& dict_vars -) -> IRErrorCode; +) -> ystdlib::error_handling::Result; template auto deserialize_encoded_text_ast( ReaderInterface& reader, diff --git a/components/core/src/clp/ffi/ir_stream/decoding_methods.hpp b/components/core/src/clp/ffi/ir_stream/decoding_methods.hpp index 86ca4170ba..7cb7aa4aa7 100644 --- a/components/core/src/clp/ffi/ir_stream/decoding_methods.hpp +++ b/components/core/src/clp/ffi/ir_stream/decoding_methods.hpp @@ -54,20 +54,13 @@ class DecodingException : public TraceableException { * Deserializes the IR stream's encoding type * @param reader * @param is_four_bytes_encoding Returns the encoding type - * @return ErrorCode_Success on success - * @return ErrorCode_Corrupted_IR if reader contains invalid IR - * @return ErrorCode_Incomplete_IR if reader doesn't contain enough data to decode - */ -IRErrorCode get_encoding_type(ReaderInterface& reader, bool& is_four_bytes_encoding); - -/** - * Deserializes the tag for the next packet. - * @param reader - * @param tag Returns the tag of the next packet. - * @return IRErrorCode_Success on success - * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize + * @return A void result on success, or an error code indicating the failure: + * - IrDeserializationErrorEnum::IncompleteStream if reader doesn't contain enough data to + * deserialize. + * - IrDeserializationErrorEnum::InvalidMagicNumber if reader contains an invalid magic number. */ -[[nodiscard]] IRErrorCode deserialize_tag(ReaderInterface& reader, encoded_tag_t& tag); +auto get_encoding_type(ReaderInterface& reader, bool& is_four_bytes_encoding) + -> ystdlib::error_handling::Result; /** * Deserializes the tag for the next packet. @@ -90,9 +83,11 @@ IRErrorCode get_encoding_type(ReaderInterface& reader, bool& is_four_bytes_encod * @param dict_vars Returns the dictionary variables * @param timestamp_or_timestamp_delta Returns the timestamp (in the eight-byte encoding case) or * the timestamp delta (in the four-byte encoding case) - * @return IRErrorCode_Success on success - * @return IRErrorCode_Corrupted_IR if reader contains invalid IR - * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data + * @return A void result on success, or an error code indicating the failure: + * - IrDeserializationErrorEnum::IncompleteStream if the reader doesn't contain enough data to + * deserialize. + * - Forwards `deserialize_encoded_text_ast`'s return values on failure. + * - Forwards `deserialize_timestamp`'s return values on failure. */ template auto deserialize_log_event( @@ -102,7 +97,7 @@ auto deserialize_log_event( std::vector& encoded_vars, std::vector& dict_vars, ir::epoch_time_ms_t& timestamp_or_timestamp_delta -) -> IRErrorCode; +) -> ystdlib::error_handling::Result; /** * Deserializes an encoded text AST from the given stream @@ -112,9 +107,12 @@ auto deserialize_log_event( * @param logtype Returns the logtype * @param encoded_vars Returns the encoded variables * @param dict_vars Returns the dictionary variables - * @return IRErrorCode_Success on success - * @return IRErrorCode_Corrupted_IR if `reader` contains invalid IR - * @return IRErrorCode_Incomplete_IR if `reader` doesn't contain enough data + * @return A void result on success, or an error code indicating the failure: + * - IrDeserializationErrorEnum::IncompleteStream if the reader doesn't contain enough data to + * deserialize. + * - Forwards `deserialize_int`'s return values on failure. + * - Forwards `deserialize_dict_var`'s return values on failure. + * - Forwards `deserialize_logtype`'s return values on failure. */ template auto deserialize_encoded_text_ast( @@ -123,7 +121,7 @@ auto deserialize_encoded_text_ast( std::string& logtype, std::vector& encoded_vars, std::vector& dict_vars -) -> IRErrorCode; +) -> ystdlib::error_handling::Result; /** * Deserializes an encoded text AST from the given reader. @@ -132,9 +130,12 @@ auto deserialize_encoded_text_ast( * @param encoded_tag * @return A result containing the deserialized encoded text AST on success, or an error code * indicating the failure: - * - IrDeserializationErrorEnum::EncodedTextAstDeserializationFailure if the encoded text AST - * cannot be deserialized. - * - IrDeserializationErrorEnum::IncompleteStream if the IR stream is incomplete. + * - IrDeserializationErrorEnum::IncompleteStream if the reader doesn't contain enough data to + * deserialize. + * - Forwards `deserialize_int`'s return values on failure. + * - Forwards `deserialize_and_append_dict_var`'s return values on failure. + * - Forwards `deserialize_and_append_logtype`'s return values on failure. + * - Forwards `EncodedTextAst::create`'s return values on failure. */ template [[nodiscard]] auto deserialize_encoded_text_ast(ReaderInterface& reader, encoded_tag_t encoded_tag) @@ -186,48 +187,40 @@ void generic_decode_message( * @param metadata_type Returns the type of the metadata deserialized from the IR * @param metadata_pos Returns the starting position of the metadata in reader * @param metadata_size Returns the size of the metadata deserialized from the IR - * @return IRErrorCode_Success on success - * @return IRErrorCode_Corrupted_IR if reader contains invalid IR - * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize + * @return A void result on success, or an error code indicating the failure: + * - IrDeserializationErrorEnum::IncompleteStream if reader doesn't contain enough data to + * deserialize. + * - Forwards `deserialize_metadata`'s return values. */ -IRErrorCode deserialize_preamble( +auto deserialize_preamble( ReaderInterface& reader, encoded_tag_t& metadata_type, size_t& metadata_pos, uint16_t& metadata_size -); +) -> ystdlib::error_handling::Result; /** * Deserializes the preamble for an IR stream. * @param reader * @param metadata_type Returns the type of the metadata deserialized from the IR * @param metadata Returns the metadata in the given vector - * @return IRErrorCode_Success on success - * @return IRErrorCode_Corrupted_IR if reader contains invalid IR - * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize + * @return A void result on success, or an error code indicating the failure: + * - IrDeserializationErrorEnum::IncompleteStream if reader doesn't contain enough data to + * deserialize. + * - Forwards `deserialize_metadata`'s return values. */ -IRErrorCode deserialize_preamble( +auto deserialize_preamble( ReaderInterface& reader, encoded_tag_t& metadata_type, std::vector& metadata -); - -/** - * Deserializes a UTC offset change packet. - * @param reader - * @param utc_offset The deserialized UTC offset. - * @return IRErrorCode_Success on success - * @return IRErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize - */ -IRErrorCode deserialize_utc_offset_change(ReaderInterface& reader, UtcOffset& utc_offset); +) -> ystdlib::error_handling::Result; /** * Deserializes a UTC offset change packet. * @param reader * @return A result containing the deserialized UTC offset on success, or an error code indicating * the failure: - * - IrDeserializationErrorEnum::IncompleteStream if reader doesn't contain enough data to - * deserialize. + * - Forwards `deserialize_int`'s return values on failure. */ [[nodiscard]] auto deserialize_utc_offset_change(ReaderInterface& reader) -> ystdlib::error_handling::Result; @@ -256,17 +249,15 @@ namespace eight_byte_encoding { * @param encoded_tag * @param message Returns the deserialized message * @param timestamp Returns the deserialized timestamp - * @return ErrorCode_Success on success - * @return ErrorCode_Corrupted_IR if reader contains invalid IR - * @return ErrorCode_Decode_Error if the log event cannot be properly deserialized - * @return ErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize + * @return A void result on success, or an error code indicating the failure: + * - Forwards `generic_deserialize_log_event`'s return values on failure. */ -IRErrorCode deserialize_log_event( +auto deserialize_log_event( ReaderInterface& reader, encoded_tag_t encoded_tag, std::string& message, ir::epoch_time_ms_t& timestamp -); +) -> ystdlib::error_handling::Result; } // namespace eight_byte_encoding namespace four_byte_encoding { @@ -276,17 +267,15 @@ namespace four_byte_encoding { * @param encoded_tag * @param message Returns the deserialized message * @param timestamp_delta Returns the deserialized timestamp delta - * @return ErrorCode_Success on success - * @return ErrorCode_Corrupted_IR if reader contains invalid IR - * @return ErrorCode_Decode_Error if the log event cannot be properly deserialized - * @return ErrorCode_Incomplete_IR if reader doesn't contain enough data to deserialize + * @return A void result on success, or an error code indicating the failure: + * - Forwards `generic_deserialize_log_event`'s return values on failure. */ -IRErrorCode deserialize_log_event( +auto deserialize_log_event( ReaderInterface& reader, encoded_tag_t encoded_tag, std::string& message, ir::epoch_time_ms_t& timestamp_delta -); +) -> ystdlib::error_handling::Result; } // namespace four_byte_encoding } // namespace clp::ffi::ir_stream diff --git a/components/core/src/clp/ffi/ir_stream/ir_unit_deserialization_methods.cpp b/components/core/src/clp/ffi/ir_stream/ir_unit_deserialization_methods.cpp index 279b67dff4..2247eb9ead 100644 --- a/components/core/src/clp/ffi/ir_stream/ir_unit_deserialization_methods.cpp +++ b/components/core/src/clp/ffi/ir_stream/ir_unit_deserialization_methods.cpp @@ -228,29 +228,13 @@ auto deserialize_schema_tree_node_key_name(ReaderInterface& reader, std::string& auto deserialize_int_val(ReaderInterface& reader, encoded_tag_t tag, value_int_t& val) -> ystdlib::error_handling::Result { if (cProtocol::Payload::ValueInt8 == tag) { - int8_t deserialized_val{}; - if (false == deserialize_int(reader, deserialized_val)) { - return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; - } - val = deserialized_val; + val = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else if (cProtocol::Payload::ValueInt16 == tag) { - int16_t deserialized_val{}; - if (false == deserialize_int(reader, deserialized_val)) { - return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; - } - val = deserialized_val; + val = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else if (cProtocol::Payload::ValueInt32 == tag) { - int32_t deserialized_val{}; - if (false == deserialize_int(reader, deserialized_val)) { - return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; - } - val = deserialized_val; + val = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else if (cProtocol::Payload::ValueInt64 == tag) { - int64_t deserialized_val{}; - if (false == deserialize_int(reader, deserialized_val)) { - return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; - } - val = deserialized_val; + val = YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)); } else { return IrDeserializationError{IrDeserializationErrorEnum::InvalidTag}; } @@ -261,22 +245,13 @@ auto deserialize_string(ReaderInterface& reader, encoded_tag_t tag, std::string& -> ystdlib::error_handling::Result { size_t str_length{}; if (cProtocol::Payload::StrLenUByte == tag) { - uint8_t length{}; - if (false == deserialize_int(reader, length)) { - return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; - } + auto length{YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader))}; str_length = static_cast(length); } else if (cProtocol::Payload::StrLenUShort == tag) { - uint16_t length{}; - if (false == deserialize_int(reader, length)) { - return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; - } + auto length{YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader))}; str_length = static_cast(length); } else if (cProtocol::Payload::StrLenUInt == tag) { - uint32_t length{}; - if (false == deserialize_int(reader, length)) { - return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; - } + auto length{YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader))}; str_length = static_cast(length); } else { return IrDeserializationError{IrDeserializationErrorEnum::InvalidTag}; @@ -369,10 +344,7 @@ auto deserialize_value_and_insert_to_node_id_value_pairs( break; } case cProtocol::Payload::ValueFloat: { - uint64_t val{}; - if (false == deserialize_int(reader, val)) { - return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; - } + auto val{YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader))}; node_id_value_pairs.emplace(node_id, Value{bit_cast(val)}); break; } diff --git a/components/core/src/clp/ffi/ir_stream/utils.hpp b/components/core/src/clp/ffi/ir_stream/utils.hpp index 1f17d88b16..e338e8e235 100644 --- a/components/core/src/clp/ffi/ir_stream/utils.hpp +++ b/components/core/src/clp/ffi/ir_stream/utils.hpp @@ -47,11 +47,14 @@ auto serialize_int(integer_t value, std::vector& output_buf) -> void; * Deserializes an integer from the given reader * @tparam integer_t Type of the integer to deserialize * @param reader - * @param value Returns the deserialized integer - * @return Whether the reader contained enough data to deserialize. + * @return A result containing the deserialized integer on success, or an error code indicating the + * failure: + * - IrDeserializationErrorEnum::IncompleteStream if the reader doesn't contain enough data to + * deserialize. */ template -[[nodiscard]] auto deserialize_int(ReaderInterface& reader, integer_t& value) -> bool; +[[nodiscard]] auto deserialize_int(ReaderInterface& reader) + -> ystdlib::error_handling::Result; /** * Serializes a string using CLP's encoding for unstructured text. @@ -156,12 +159,13 @@ auto serialize_int(integer_t value, std::vector& output_buf) -> void { } template -auto deserialize_int(ReaderInterface& reader, integer_t& value) -> bool { +auto deserialize_int(ReaderInterface& reader) -> ystdlib::error_handling::Result { integer_t value_little_endian; if (reader.try_read_numeric_value(value_little_endian) != clp::ErrorCode_Success) { - return false; + return IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; } + integer_t value; constexpr auto cReadSize = sizeof(integer_t); if constexpr (cReadSize == 1) { value = value_little_endian; @@ -172,7 +176,7 @@ auto deserialize_int(ReaderInterface& reader, integer_t& value) -> bool { } else if constexpr (cReadSize == 8) { value = bswap_64(value_little_endian); } - return true; + return value; } template @@ -251,10 +255,9 @@ auto deserialize_and_decode_schema_tree_node_id( auto size_dependent_deserialize_and_decode_schema_tree_node_id = [&reader]() -> ystdlib::error_handling::Result> { - encoded_node_id_t encoded_node_id{}; - if (false == deserialize_int(reader, encoded_node_id)) { - return std::errc::result_out_of_range; - } + auto encoded_node_id{ + YSTDLIB_ERROR_HANDLING_TRYX(deserialize_int(reader)) + }; if (0 > encoded_node_id) { return {true, static_cast(get_ones_complement(encoded_node_id))}; diff --git a/components/core/src/clp/ir/LogEventDeserializer.cpp b/components/core/src/clp/ir/LogEventDeserializer.cpp index 42f229eb6e..d042cc9d52 100644 --- a/components/core/src/clp/ir/LogEventDeserializer.cpp +++ b/components/core/src/clp/ir/LogEventDeserializer.cpp @@ -17,16 +17,9 @@ auto LogEventDeserializer::create(ReaderInterface& reader) -> ystdlib::error_handling::Result> { ffi::ir_stream::encoded_tag_t metadata_type{0}; std::vector metadata; - auto ir_error_code = ffi::ir_stream::deserialize_preamble(reader, metadata_type, metadata); - if (ffi::ir_stream::IRErrorCode_Success != ir_error_code) { - switch (ir_error_code) { - case ffi::ir_stream::IRErrorCode_Incomplete_IR: - return std::errc::result_out_of_range; - case ffi::ir_stream::IRErrorCode_Corrupted_IR: - default: - return std::errc::protocol_error; - } - } + YSTDLIB_ERROR_HANDLING_TRYV( + ffi::ir_stream::deserialize_preamble(reader, metadata_type, metadata) + ); if (ffi::ir_stream::cProtocol::Metadata::EncodingJson != metadata_type) { return std::errc::protocol_not_supported; @@ -74,20 +67,15 @@ auto LogEventDeserializer::deserialize_log_event() // Process any packets before the log event ffi::ir_stream::encoded_tag_t tag{}; while (true) { - auto ir_error_code = ffi::ir_stream::deserialize_tag(m_reader, tag); - if (ffi::ir_stream::IRErrorCode_Incomplete_IR == ir_error_code) { - return std::errc::result_out_of_range; - } - + tag = YSTDLIB_ERROR_HANDLING_TRYX(ffi::ir_stream::deserialize_tag(m_reader)); if (ffi::ir_stream::cProtocol::Eof == tag) { return std::errc::no_message; } if (ffi::ir_stream::cProtocol::Payload::UtcOffsetChange == tag) { - ir_error_code = ffi::ir_stream::deserialize_utc_offset_change(m_reader, m_utc_offset); - if (ffi::ir_stream::IRErrorCode_Incomplete_IR == ir_error_code) { - return std::errc::result_out_of_range; - } + m_utc_offset = YSTDLIB_ERROR_HANDLING_TRYX( + ffi::ir_stream::deserialize_utc_offset_change(m_reader) + ); } else { // Packet must be a log event break; @@ -99,23 +87,16 @@ auto LogEventDeserializer::deserialize_log_event() std::vector dict_vars; std::vector encoded_vars; - auto ir_error_code = ffi::ir_stream::deserialize_log_event( - m_reader, - tag, - logtype, - encoded_vars, - dict_vars, - timestamp_or_timestamp_delta + YSTDLIB_ERROR_HANDLING_TRYV( + ffi::ir_stream::deserialize_log_event( + m_reader, + tag, + logtype, + encoded_vars, + dict_vars, + timestamp_or_timestamp_delta + ) ); - if (ffi::ir_stream::IRErrorCode_Success != ir_error_code) { - switch (ir_error_code) { - case ffi::ir_stream::IRErrorCode_Incomplete_IR: - return std::errc::result_out_of_range; - case ffi::ir_stream::IRErrorCode_Corrupted_IR: - default: - return std::errc::protocol_error; - } - } epoch_time_ms_t timestamp{}; if constexpr (std::is_same_v) { diff --git a/components/core/src/clp/ir/utils.cpp b/components/core/src/clp/ir/utils.cpp index 7cc3ca6f0e..855cc0b14f 100644 --- a/components/core/src/clp/ir/utils.cpp +++ b/components/core/src/clp/ir/utils.cpp @@ -7,7 +7,6 @@ namespace clp::ir { auto has_ir_stream_magic_number(std::string_view buf) -> bool { BufferReader buf_reader{buf.data(), buf.size()}; bool is_four_bytes_encoded{false}; - return ffi::ir_stream::IRErrorCode_Success - == ffi::ir_stream::get_encoding_type(buf_reader, is_four_bytes_encoded); + return ffi::ir_stream::get_encoding_type(buf_reader, is_four_bytes_encoded).has_value(); } } // namespace clp::ir diff --git a/components/core/tests/test-ir_encoding_methods.cpp b/components/core/tests/test-ir_encoding_methods.cpp index 3276904ce7..e0186d475d 100644 --- a/components/core/tests/test-ir_encoding_methods.cpp +++ b/components/core/tests/test-ir_encoding_methods.cpp @@ -24,6 +24,7 @@ #include "../src/clp/ffi/ir_stream/decoding_methods.hpp" #include "../src/clp/ffi/ir_stream/Deserializer.hpp" #include "../src/clp/ffi/ir_stream/encoding_methods.hpp" +#include "../src/clp/ffi/ir_stream/IrDeserializationError.hpp" #include "../src/clp/ffi/ir_stream/IrUnitType.hpp" #include "../src/clp/ffi/ir_stream/protocol_constants.hpp" #include "../src/clp/ffi/ir_stream/search/test/utils.hpp" @@ -51,6 +52,8 @@ using clp::ffi::ir_stream::deserialize_utc_offset_change; using clp::ffi::ir_stream::Deserializer; using clp::ffi::ir_stream::encoded_tag_t; using clp::ffi::ir_stream::get_encoding_type; +using clp::ffi::ir_stream::IrDeserializationError; +using clp::ffi::ir_stream::IrDeserializationErrorEnum; using clp::ffi::ir_stream::IRErrorCode; using clp::ffi::ir_stream::search::test::unpack_and_serialize_msgpack_bytes; using clp::ffi::ir_stream::serialize_utc_offset_change; @@ -469,19 +472,19 @@ bool serialize_preamble( * @param tag * @param message * @param decoded_ts Returns the decoded timestamp - * @return IRErrorCode_Success on success - * @return Same as the clp::ffi::ir_stream::eight_byte_encoding::deserialize_log_event when - * encoded_variable_t == eight_byte_encoded_variable_t - * @return Same as the clp::ffi::ir_stream::four_byte_encoding::deserialize_log_event when - * encoded_variable_t == four_byte_encoded_variable_t + * @return A void result on success, or an error code indicating the failure: + * - Forwards `clp::ffi::ir_stream::eight_byte_encoding::deserialize_log_event`'s return values on + * failure when `encoded_variable_t == eight_byte_encoded_variable_t` + * - Forwards `clp::ffi::ir_stream::four_byte_encoding::deserialize_log_event`'s return values on + * failure when `encoded_variable_t == four_byte_encoded_variable_t` */ template -IRErrorCode deserialize_log_event( +auto deserialize_log_event( BufferReader& reader, encoded_tag_t tag, string& message, epoch_time_ms_t& decoded_ts -); +) -> ystdlib::error_handling::Result; /** * Struct to hold the timestamp info from the IR stream's metadata @@ -565,12 +568,12 @@ bool serialize_preamble( } template -IRErrorCode deserialize_log_event( +auto deserialize_log_event( BufferReader& reader, encoded_tag_t tag, string& message, epoch_time_ms_t& decoded_ts -) { +) -> ystdlib::error_handling::Result { static_assert( is_same_v || is_same_v @@ -615,8 +618,7 @@ TEST_CASE("get_encoding_type", "[ffi][get_encoding_type]") { size_checked_pointer_cast(eight_byte_encoding_vec.data()), eight_byte_encoding_vec.size() }; - REQUIRE(get_encoding_type(eight_byte_ir_buffer, is_four_bytes_encoding) - == IRErrorCode::IRErrorCode_Success); + REQUIRE_FALSE(get_encoding_type(eight_byte_ir_buffer, is_four_bytes_encoding).has_error()); REQUIRE(match_encoding_type(is_four_bytes_encoding)); // Test four-byte encoding @@ -629,8 +631,7 @@ TEST_CASE("get_encoding_type", "[ffi][get_encoding_type]") { size_checked_pointer_cast(four_byte_encoding_vec.data()), four_byte_encoding_vec.size() }; - REQUIRE(get_encoding_type(four_byte_ir_buffer, is_four_bytes_encoding) - == IRErrorCode::IRErrorCode_Success); + REQUIRE_FALSE(get_encoding_type(four_byte_ir_buffer, is_four_bytes_encoding).has_error()); REQUIRE(match_encoding_type(is_four_bytes_encoding)); // Test error on empty and incomplete ir_buffer @@ -638,15 +639,15 @@ TEST_CASE("get_encoding_type", "[ffi][get_encoding_type]") { size_checked_pointer_cast(four_byte_encoding_vec.data()), 0 ); - REQUIRE(get_encoding_type(empty_ir_buffer, is_four_bytes_encoding) - == IRErrorCode::IRErrorCode_Incomplete_IR); + REQUIRE(get_encoding_type(empty_ir_buffer, is_four_bytes_encoding).error() + == IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}); BufferReader incomplete_buffer{ size_checked_pointer_cast(four_byte_encoding_vec.data()), four_byte_encoding_vec.size() - 1 }; - REQUIRE(get_encoding_type(incomplete_buffer, is_four_bytes_encoding) - == IRErrorCode::IRErrorCode_Incomplete_IR); + REQUIRE(get_encoding_type(incomplete_buffer, is_four_bytes_encoding).error() + == IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}); // Test error on invalid encoding vector const invalid_ir_vec{0x02, 0x43, 0x24, 0x34}; @@ -654,8 +655,8 @@ TEST_CASE("get_encoding_type", "[ffi][get_encoding_type]") { size_checked_pointer_cast(invalid_ir_vec.data()), invalid_ir_vec.size() }; - REQUIRE(get_encoding_type(invalid_ir_buffer, is_four_bytes_encoding) - == IRErrorCode::IRErrorCode_Corrupted_IR); + REQUIRE(get_encoding_type(invalid_ir_buffer, is_four_bytes_encoding).error() + == IrDeserializationError{IrDeserializationErrorEnum::InvalidMagicNumber}); } TEMPLATE_TEST_CASE( @@ -681,8 +682,7 @@ TEMPLATE_TEST_CASE( // Check if encoding type is properly read BufferReader ir_buffer{size_checked_pointer_cast(ir_buf.data()), ir_buf.size()}; bool is_four_bytes_encoding; - REQUIRE(get_encoding_type(ir_buffer, is_four_bytes_encoding) - == IRErrorCode::IRErrorCode_Success); + REQUIRE_FALSE(get_encoding_type(ir_buffer, is_four_bytes_encoding).has_error()); REQUIRE(match_encoding_type(is_four_bytes_encoding)); REQUIRE(MagicNumberLength == ir_buffer.get_pos()); @@ -691,8 +691,9 @@ TEMPLATE_TEST_CASE( encoded_tag_t metadata_type{0}; size_t metadata_pos{0}; uint16_t metadata_size{0}; - REQUIRE(deserialize_preamble(ir_buffer, metadata_type, metadata_pos, metadata_size) - == IRErrorCode::IRErrorCode_Success); + REQUIRE_FALSE( + deserialize_preamble(ir_buffer, metadata_type, metadata_pos, metadata_size).has_error() + ); REQUIRE(encoded_preamble_end_pos == ir_buffer.get_pos()); char* metadata_ptr{size_checked_pointer_cast(ir_buf.data()) + metadata_pos}; @@ -722,8 +723,7 @@ TEMPLATE_TEST_CASE( // Test if preamble can be decoded by the string copy method std::vector json_metadata_vec; ir_buffer.seek_from_begin(MagicNumberLength); - REQUIRE(deserialize_preamble(ir_buffer, metadata_type, json_metadata_vec) - == IRErrorCode::IRErrorCode_Success); + REQUIRE_FALSE(deserialize_preamble(ir_buffer, metadata_type, json_metadata_vec).has_error()); string_view json_metadata_copied{ size_checked_pointer_cast(json_metadata_vec.data()), json_metadata_vec.size() @@ -738,13 +738,15 @@ TEMPLATE_TEST_CASE( ir_buf.size() }; incomplete_preamble_buffer.seek_from_begin(MagicNumberLength); - REQUIRE(deserialize_preamble( - incomplete_preamble_buffer, - metadata_type, - metadata_pos, - metadata_size - ) - == IRErrorCode::IRErrorCode_Incomplete_IR); + auto incomplete_preamble_result = deserialize_preamble( + incomplete_preamble_buffer, + metadata_type, + metadata_pos, + metadata_size + ); + REQUIRE(incomplete_preamble_result.has_error()); + REQUIRE(IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream} + == incomplete_preamble_result.error()); // Test if corrupted IR can be detected ir_buf[MagicNumberLength] = 0x23; @@ -752,13 +754,15 @@ TEMPLATE_TEST_CASE( size_checked_pointer_cast(ir_buf.data()), ir_buf.size() }; - REQUIRE(deserialize_preamble( - corrupted_preamble_buffer, - metadata_type, - metadata_pos, - metadata_size - ) - == IRErrorCode::IRErrorCode_Corrupted_IR); + auto corrupted_preamble_result = deserialize_preamble( + corrupted_preamble_buffer, + metadata_type, + metadata_pos, + metadata_size + ); + REQUIRE(corrupted_preamble_result.has_error()); + REQUIRE(IrDeserializationError{IrDeserializationErrorEnum::UnsupportedMetadataFormat} + == corrupted_preamble_result.error()); } TEMPLATE_TEST_CASE( @@ -781,21 +785,27 @@ TEMPLATE_TEST_CASE( BufferReader ir_buffer{size_checked_pointer_cast(ir_buf.data()), ir_buf.size()}; string decoded_message; epoch_time_ms_t timestamp; - encoded_tag_t tag; // Test if message can be decoded properly - REQUIRE(IRErrorCode::IRErrorCode_Success == deserialize_tag(ir_buffer, tag)); - REQUIRE(IRErrorCode::IRErrorCode_Success - == deserialize_log_event(ir_buffer, tag, decoded_message, timestamp)); + auto result = deserialize_tag(ir_buffer); + REQUIRE_FALSE(result.has_error()); + auto tag = result.value(); + REQUIRE_FALSE( + deserialize_log_event(ir_buffer, tag, decoded_message, timestamp).has_error() + ); REQUIRE(message == decoded_message); REQUIRE(timestamp == reference_timestamp); REQUIRE(ir_buffer.get_pos() == encoded_message_end_pos); // Test corrupted IR ir_buffer.seek_from_begin(encoded_message_start_pos + 1); - REQUIRE(IRErrorCode::IRErrorCode_Success == deserialize_tag(ir_buffer, tag)); - REQUIRE(IRErrorCode::IRErrorCode_Corrupted_IR - == deserialize_log_event(ir_buffer, tag, message, timestamp)); + result = deserialize_tag(ir_buffer); + REQUIRE_FALSE(result.has_error()); + tag = result.value(); + auto corrupted_result = deserialize_log_event(ir_buffer, tag, message, timestamp); + REQUIRE(corrupted_result.has_error()); + REQUIRE(IrDeserializationError{IrDeserializationErrorEnum::InvalidTag} + == corrupted_result.error()); // Test incomplete IR ir_buf.resize(encoded_message_end_pos - 4); @@ -803,11 +813,14 @@ TEMPLATE_TEST_CASE( size_checked_pointer_cast(ir_buf.data()), ir_buf.size() }; - REQUIRE(IRErrorCode::IRErrorCode_Success == deserialize_tag(incomplete_preamble_buffer, tag)); - REQUIRE( - IRErrorCode::IRErrorCode_Incomplete_IR - == deserialize_log_event(incomplete_preamble_buffer, tag, message, timestamp) - ); + result = deserialize_tag(incomplete_preamble_buffer); + REQUIRE_FALSE(result.has_error()); + tag = result.value(); + auto incomplete_result + = deserialize_log_event(incomplete_preamble_buffer, tag, message, timestamp); + REQUIRE(incomplete_result.has_error()); + REQUIRE(IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream} + == incomplete_result.error()); } // NOTE: This test only tests eight_byte_encoded_variable_t because we trigger @@ -840,21 +853,24 @@ TEST_CASE("message_decode_error", "[ffi][deserialize_log_event]") { // Test if a trailing escape triggers a decoder error auto ir_with_extra_escape{ir_buf}; - encoded_tag_t tag; ir_with_extra_escape.at(logtype_end_pos - 1) = enum_to_underlying_type(VariablePlaceholder::Escape); BufferReader ir_with_extra_escape_buffer{ size_checked_pointer_cast(ir_with_extra_escape.data()), ir_with_extra_escape.size() }; - REQUIRE(IRErrorCode::IRErrorCode_Success == deserialize_tag(ir_with_extra_escape_buffer, tag)); - REQUIRE(IRErrorCode::IRErrorCode_Decode_Error - == deserialize_log_event( - ir_with_extra_escape_buffer, - tag, - decoded_message, - timestamp - )); + auto result = deserialize_tag(ir_with_extra_escape_buffer); + REQUIRE_FALSE(result.has_error()); + auto tag = result.value(); + auto extra_escape_result = deserialize_log_event( + ir_with_extra_escape_buffer, + tag, + decoded_message, + timestamp + ); + REQUIRE(extra_escape_result.has_error()); + REQUIRE(IrDeserializationError{IrDeserializationErrorEnum::MessageDecodingFailure} + == extra_escape_result.error()); // Test if an extra placeholder triggers a decoder error auto ir_with_extra_placeholder{ir_buf}; @@ -864,15 +880,18 @@ TEST_CASE("message_decode_error", "[ffi][deserialize_log_event]") { size_checked_pointer_cast(ir_with_extra_placeholder.data()), ir_with_extra_placeholder.size() }; - REQUIRE(IRErrorCode::IRErrorCode_Success - == deserialize_tag(ir_with_extra_placeholder_buffer, tag)); - REQUIRE(IRErrorCode::IRErrorCode_Decode_Error - == deserialize_log_event( - ir_with_extra_placeholder_buffer, - tag, - decoded_message, - timestamp - )); + result = deserialize_tag(ir_with_extra_placeholder_buffer); + REQUIRE_FALSE(result.has_error()); + tag = result.value(); + auto extra_placeholder_result = deserialize_log_event( + ir_with_extra_placeholder_buffer, + tag, + decoded_message, + timestamp + ); + REQUIRE(extra_placeholder_result.has_error()); + REQUIRE(IrDeserializationError{IrDeserializationErrorEnum::MessageDecodingFailure} + == extra_placeholder_result.error()); } TEST_CASE("decode_next_message_four_byte_timestamp_delta", "[ffi][deserialize_log_event]") { @@ -901,16 +920,19 @@ TEST_CASE("decode_next_message_four_byte_timestamp_delta", "[ffi][deserialize_lo BufferReader ir_buffer{size_checked_pointer_cast(ir_buf.data()), ir_buf.size()}; string decoded_message; - encoded_tag_t tag; epoch_time_ms_t decoded_delta_ts{}; - REQUIRE(IRErrorCode::IRErrorCode_Success == deserialize_tag(ir_buffer, tag)); - REQUIRE(IRErrorCode::IRErrorCode_Success - == deserialize_log_event( + auto result = deserialize_tag(ir_buffer); + REQUIRE_FALSE(result.has_error()); + auto tag = result.value(); + REQUIRE_FALSE( + deserialize_log_event( ir_buffer, tag, decoded_message, decoded_delta_ts - )); + ) + .has_error() + ); REQUIRE(message == decoded_message); REQUIRE(decoded_delta_ts == ts_delta); } @@ -1006,8 +1028,7 @@ TEMPLATE_TEST_CASE( }; bool is_four_bytes_encoding; - REQUIRE(get_encoding_type(complete_ir_buffer, is_four_bytes_encoding) - == IRErrorCode::IRErrorCode_Success); + REQUIRE_FALSE(get_encoding_type(complete_ir_buffer, is_four_bytes_encoding).has_error()); REQUIRE(match_encoding_type(is_four_bytes_encoding)); // Test if preamble can be properly decoded @@ -1015,8 +1036,10 @@ TEMPLATE_TEST_CASE( encoded_tag_t metadata_type; size_t metadata_pos; uint16_t metadata_size; - REQUIRE(deserialize_preamble(complete_ir_buffer, metadata_type, metadata_pos, metadata_size) - == IRErrorCode::IRErrorCode_Success); + REQUIRE_FALSE( + deserialize_preamble(complete_ir_buffer, metadata_type, metadata_pos, metadata_size) + .has_error() + ); REQUIRE(encoded_preamble_end_pos == complete_ir_buffer.get_pos()); auto* json_metadata_ptr{size_checked_pointer_cast(ir_buf.data() + metadata_pos)}; @@ -1034,23 +1057,30 @@ TEMPLATE_TEST_CASE( string decoded_message; epoch_time_ms_t ts_or_ts_delta{}; UtcOffset utc_offset{0}; - encoded_tag_t tag; epoch_time_ms_t prev_ts{preamble_ts}; + encoded_tag_t tag; for (auto const& log_event : test_log_events) { - REQUIRE(IRErrorCode::IRErrorCode_Success == deserialize_tag(complete_ir_buffer, tag)); + auto tag_result = deserialize_tag(complete_ir_buffer); + REQUIRE_FALSE(tag_result.has_error()); + tag = tag_result.value(); if (clp::ffi::ir_stream::cProtocol::Payload::UtcOffsetChange == tag) { - REQUIRE(IRErrorCode::IRErrorCode_Success - == deserialize_utc_offset_change(complete_ir_buffer, utc_offset)); - REQUIRE(IRErrorCode::IRErrorCode_Success == deserialize_tag(complete_ir_buffer, tag)); + auto utc_offset_result = deserialize_utc_offset_change(complete_ir_buffer); + REQUIRE_FALSE(utc_offset_result.has_error()); + utc_offset = utc_offset_result.value(); + tag_result = deserialize_tag(complete_ir_buffer); + REQUIRE_FALSE(tag_result.has_error()); + tag = tag_result.value(); } - REQUIRE(IRErrorCode::IRErrorCode_Success - == deserialize_log_event( + REQUIRE_FALSE( + deserialize_log_event( complete_ir_buffer, tag, decoded_message, ts_or_ts_delta - )); + ) + .has_error() + ); auto timestamp{ts_or_ts_delta}; if constexpr (is_same_v) { timestamp += prev_ts; @@ -1060,7 +1090,9 @@ TEMPLATE_TEST_CASE( REQUIRE(log_event.get_timestamp() == timestamp); REQUIRE(log_event.get_utc_offset() == utc_offset); } - REQUIRE(IRErrorCode::IRErrorCode_Success == deserialize_tag(complete_ir_buffer, tag)); + auto tag_result = deserialize_tag(complete_ir_buffer); + REQUIRE_FALSE(tag_result.has_error()); + tag = tag_result.value(); REQUIRE(clp::ffi::ir_stream::cProtocol::Eof == tag); REQUIRE(complete_ir_buffer.get_pos() == ir_buf.size()); } @@ -1095,8 +1127,7 @@ TEMPLATE_TEST_CASE( }; bool is_four_bytes_encoding; - REQUIRE(get_encoding_type(complete_ir_buffer, is_four_bytes_encoding) - == IRErrorCode::IRErrorCode_Success); + REQUIRE_FALSE(get_encoding_type(complete_ir_buffer, is_four_bytes_encoding).has_error()); REQUIRE(match_encoding_type(is_four_bytes_encoding)); auto create_result = LogEventDeserializer::create(complete_ir_buffer); @@ -1147,10 +1178,7 @@ TEMPLATE_TEST_CASE( BufferReader buffer_reader{size_checked_pointer_cast(ir_buf.data()), ir_buf.size()}; bool is_four_byte_encoding{}; - REQUIRE( - (IRErrorCode::IRErrorCode_Success - == get_encoding_type(buffer_reader, is_four_byte_encoding)) - ); + REQUIRE_FALSE(get_encoding_type(buffer_reader, is_four_byte_encoding).has_error()); if constexpr (std::is_same_v) { REQUIRE(is_four_byte_encoding); } else { @@ -1159,10 +1187,7 @@ TEMPLATE_TEST_CASE( encoded_tag_t metadata_type{}; vector metadata_bytes; - REQUIRE( - (IRErrorCode::IRErrorCode_Success - == deserialize_preamble(buffer_reader, metadata_type, metadata_bytes)) - ); + REQUIRE_FALSE(deserialize_preamble(buffer_reader, metadata_type, metadata_bytes).has_error()); REQUIRE((clp::ffi::ir_stream::cProtocol::Metadata::EncodingJson == metadata_type)); string_view const metadata_view{ size_checked_pointer_cast(metadata_bytes.data()), @@ -1185,17 +1210,20 @@ TEMPLATE_TEST_CASE( ); REQUIRE((expected_metadata == metadata)); - encoded_tag_t encoded_tag{}; - REQUIRE((IRErrorCode::IRErrorCode_Success == deserialize_tag(buffer_reader, encoded_tag))); + auto tag_result = deserialize_tag(buffer_reader); + REQUIRE_FALSE(tag_result.has_error()); + auto encoded_tag = tag_result.value(); REQUIRE((clp::ffi::ir_stream::cProtocol::Payload::UtcOffsetChange == encoded_tag)); + UtcOffset utc_offset_change{0}; - REQUIRE( - (IRErrorCode::IRErrorCode_Success - == deserialize_utc_offset_change(buffer_reader, utc_offset_change)) - ); + auto utc_offset_change_result = deserialize_utc_offset_change(buffer_reader); + REQUIRE_FALSE(utc_offset_change_result.has_error()); + utc_offset_change = utc_offset_change_result.value(); REQUIRE((cBeijingUtcOffset == utc_offset_change)); - REQUIRE((IRErrorCode::IRErrorCode_Success == deserialize_tag(buffer_reader, encoded_tag))); + tag_result = deserialize_tag(buffer_reader); + REQUIRE_FALSE(tag_result.has_error()); + encoded_tag = tag_result.value(); REQUIRE((clp::ffi::ir_stream::cProtocol::Eof == encoded_tag)); char eof{}; @@ -1423,8 +1451,9 @@ TEMPLATE_TEST_CASE( REQUIRE_FALSE(cSerializationMethodToTest(node_id, output_buf).has_error()); BufferReader reader{size_checked_pointer_cast(output_buf.data()), output_buf.size()}; - encoded_tag_t tag{}; - REQUIRE((IRErrorCode::IRErrorCode_Success == deserialize_tag(reader, tag))); + auto tag_result = deserialize_tag(reader); + REQUIRE_FALSE(tag_result.has_error()); + auto tag = tag_result.value(); auto const result{cDeserializationMethodToTest(tag, reader)}; REQUIRE_FALSE(result.has_error()); auto const [is_auto_generated, deserialized_node_id]{result.value()}; diff --git a/components/core/tests/test-ir_serializer.cpp b/components/core/tests/test-ir_serializer.cpp index 17ed18f42e..b4f89ab409 100644 --- a/components/core/tests/test-ir_serializer.cpp +++ b/components/core/tests/test-ir_serializer.cpp @@ -15,7 +15,6 @@ #include "../src/clp/ir/types.hpp" #include "../src/clp/streaming_compression/zstd/Decompressor.hpp" -using clp::ffi::ir_stream::IRErrorCode::IRErrorCode_Success; using clp::ir::cIrFileExtension; using clp::ir::eight_byte_encoded_variable_t; using clp::ir::epoch_time_ms_t; @@ -91,14 +90,13 @@ TEMPLATE_TEST_CASE( ir_reader.open(ir_test_file); bool uses_four_byte_encoding{false}; - REQUIRE( - (IRErrorCode_Success - == clp::ffi::ir_stream::get_encoding_type(ir_reader, uses_four_byte_encoding)) + REQUIRE_FALSE( + clp::ffi::ir_stream::get_encoding_type(ir_reader, uses_four_byte_encoding).has_error() ); REQUIRE((is_same_v == uses_four_byte_encoding)); auto result = LogEventDeserializer::create(ir_reader); - REQUIRE((false == result.has_error())); + REQUIRE_FALSE(result.has_error()); auto& deserializer = result.value(); // Decode and deserialize all expected log events