Skip to content

feat(kv-ir): Improve error handling in clp::ffi::ir_stream::decoding_methods:#2089

Open
jonathan-imanu wants to merge 2 commits intoy-scope:mainfrom
jonathan-imanu:refactor/kvir-decoding-methods
Open

feat(kv-ir): Improve error handling in clp::ffi::ir_stream::decoding_methods:#2089
jonathan-imanu wants to merge 2 commits intoy-scope:mainfrom
jonathan-imanu:refactor/kvir-decoding-methods

Conversation

@jonathan-imanu
Copy link
Contributor

@jonathan-imanu jonathan-imanu commented Mar 11, 2026

Description

This PR is the second step in improving the error handling in clp::ffi::ir_stream::Deserializer.hpp. Following the suggestion of #904, the end goal is to use the Result<T, IrErrorCode> as the return type instead of using std::errc. This PR builds off of the work done in #2005.

Since this is a larger refactor, this step focuses changing functions in components/core/src/clp/ffi/ir_stream/decoding_methods.cpp to return Result<T, IrErrorCode> instead of the C-style IRErrorCode.

Checklist

  • The PR satisfies the contribution guidelines.
  • This is a breaking change and that has been indicated in the PR title, OR this isn't a
    breaking change.
  • Necessary docs have been updated, OR no docs need to be updated.

Validation performed

  • Workflows
  • Unit tests

Summary by CodeRabbit

  • Bug Fixes

    • Improved detection and reporting for invalid IR stream magic numbers and message decoding failures.
  • Refactor

    • Unified IR deserialization error model: numeric codes replaced by descriptive result-based errors for clearer diagnostics and consistent propagation.
    • Centralized error propagation reduces boilerplate and standardizes failure handling.
  • Tests

    • Updated tests to validate the new result-based error semantics and descriptive error cases.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: bfd3d51e-e518-4187-92e6-71c882571600

📥 Commits

Reviewing files that changed from the base of the PR and between 68eb773 and 262a418.

📒 Files selected for processing (1)
  • components/core/src/clp/ffi/ir_stream/decoding_methods.cpp

Walkthrough

This PR refactors IR stream deserialization error handling from numeric IRErrorCode enums to a typed ystdlib::error_handling::Result<> system, adds two new IrDeserializationError values, and updates call sites to propagate errors via TRY-style macros and Result semantics.

Changes

Cohort / File(s) Summary
Error Type Definitions
components/core/src/clp/ffi/ir_stream/IrDeserializationError.hpp, components/core/src/clp/ffi/ir_stream/IrDeserializationError.cpp
Adds InvalidMagicNumber and MessageDecodingFailure enum values and corresponding message mappings.
Core Deserialization API Refactoring
components/core/src/clp/ffi/ir_stream/decoding_methods.hpp, components/core/src/clp/ffi/ir_stream/decoding_methods.cpp
Replaces many public/internal IRErrorCode returns with ystdlib::error_handling::Result<> (including get_encoding_type, deserialize_tag, deserialize_preamble, deserialize_log_event, deserialize_encoded_text_ast, deserialize_utc_offset_change) and updates docs/signatures accordingly.
Deserializer Interface Changes
components/core/src/clp/ffi/ir_stream/Deserializer.hpp
Replaces explicit IRErrorCode checks with YSTDLIB_ERROR_HANDLING_TRYV/TRYX wrappers to standardize early-return error propagation.
Low-level utils & unit deserialization
components/core/src/clp/ffi/ir_stream/utils.hpp, components/core/src/clp/ffi/ir_stream/ir_unit_deserialization_methods.cpp, components/core/src/clp/ffi/ir_stream/decoding_methods.cpp
deserialize_int now returns Result<integer_t>; many internal deserialization helpers switched to Result-based returns and TRYX-style unwrapping, removing repetitive manual error checks.
Integration / Call sites
components/core/src/clp/FileCompressor.cpp, components/core/src/clp/ir/LogEventDeserializer.cpp, components/core/src/clp/ir/utils.cpp
Call sites updated to consume Result-based APIs: use .has_value()/.has_error() checks or TRY macros instead of comparing numeric IRErrorCode values.
Tests
components/core/tests/test-ir_encoding_methods.cpp, components/core/tests/test-ir_serializer.cpp
Tests updated to include IrDeserializationError.hpp, adapt to Result-based returns and change assertions from exact IRErrorCode checks to .has_error()/result unwrapping semantics.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested reviewers

  • gibber9809
  • LinZhihao-723
  • davidlion
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the primary change: migrating error handling in decoding_methods from IRErrorCode to Result with improved error messages.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@jonathan-imanu jonathan-imanu marked this pull request as ready for review March 12, 2026 03:47
@jonathan-imanu jonathan-imanu requested a review from a team as a code owner March 12, 2026 03:47
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@components/core/tests/test-ir_encoding_methods.cpp`:
- Around line 642-643: The test currently combines failure checking and specific
error matching in one REQUIRE for get_encoding_type(empty_ir_buffer,
is_four_bytes_encoding).error(); change each of these negative-path assertions
(including the instances at the other two locations) to first assert that the
result has_error() (e.g., REQUIRE(result.has_error())), then separately assert
the specific error via result.error() ==
IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; locate the
calls to get_encoding_type and the variables empty_ir_buffer and
is_four_bytes_encoding and replace the single combined REQUIRE with the two-step
has_error() then error() checks.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 16c021a7-c349-4361-83b6-4cf98eb4a90e

📥 Commits

Reviewing files that changed from the base of the PR and between f40c9fe and 68eb773.

📒 Files selected for processing (12)
  • components/core/src/clp/clp/FileCompressor.cpp
  • components/core/src/clp/ffi/ir_stream/Deserializer.hpp
  • components/core/src/clp/ffi/ir_stream/IrDeserializationError.cpp
  • components/core/src/clp/ffi/ir_stream/IrDeserializationError.hpp
  • components/core/src/clp/ffi/ir_stream/decoding_methods.cpp
  • components/core/src/clp/ffi/ir_stream/decoding_methods.hpp
  • components/core/src/clp/ffi/ir_stream/ir_unit_deserialization_methods.cpp
  • components/core/src/clp/ffi/ir_stream/utils.hpp
  • components/core/src/clp/ir/LogEventDeserializer.cpp
  • components/core/src/clp/ir/utils.cpp
  • components/core/tests/test-ir_encoding_methods.cpp
  • components/core/tests/test-ir_serializer.cpp

Comment on lines +642 to +643
REQUIRE(get_encoding_type(empty_ir_buffer, is_four_bytes_encoding).error()
== IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Split these negative-path assertions into has_error() and error() checks.

These three assertions collapse “the call must fail” and “it must fail with this code” into a single check, which makes regressions harder to diagnose than the two-step pattern you already use later in this file.

Suggested update
-    REQUIRE(get_encoding_type(empty_ir_buffer, is_four_bytes_encoding).error()
-            == IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream});
+    auto empty_result = get_encoding_type(empty_ir_buffer, is_four_bytes_encoding);
+    REQUIRE(empty_result.has_error());
+    REQUIRE(
+            empty_result.error()
+            == IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}
+    );

-    REQUIRE(get_encoding_type(incomplete_buffer, is_four_bytes_encoding).error()
-            == IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream});
+    auto incomplete_result = get_encoding_type(incomplete_buffer, is_four_bytes_encoding);
+    REQUIRE(incomplete_result.has_error());
+    REQUIRE(
+            incomplete_result.error()
+            == IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}
+    );

-    REQUIRE(get_encoding_type(invalid_ir_buffer, is_four_bytes_encoding).error()
-            == IrDeserializationError{IrDeserializationErrorEnum::InvalidMagicNumber});
+    auto invalid_result = get_encoding_type(invalid_ir_buffer, is_four_bytes_encoding);
+    REQUIRE(invalid_result.has_error());
+    REQUIRE(
+            invalid_result.error()
+            == IrDeserializationError{IrDeserializationErrorEnum::InvalidMagicNumber}
+    );

Also applies to: 649-650, 658-659

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@components/core/tests/test-ir_encoding_methods.cpp` around lines 642 - 643,
The test currently combines failure checking and specific error matching in one
REQUIRE for get_encoding_type(empty_ir_buffer, is_four_bytes_encoding).error();
change each of these negative-path assertions (including the instances at the
other two locations) to first assert that the result has_error() (e.g.,
REQUIRE(result.has_error())), then separately assert the specific error via
result.error() ==
IrDeserializationError{IrDeserializationErrorEnum::IncompleteStream}; locate the
calls to get_encoding_type and the variables empty_ir_buffer and
is_four_bytes_encoding and replace the single combined REQUIRE with the two-step
has_error() then error() checks.

Copy link
Member

@LinZhihao-723 LinZhihao-723 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will stop reading as I realized many API changes in this PR don't make sense as explained in the comment.
And in fact, I realized most of these APIs aren't needed anymore once we support using the kv-ir deserializer to deserialize the old IR format.
That said, I think the right way to do is probably:

  • Leave those APIs not used by kv-ir deserializer untouched in this PR, and only add result-style versions for those used by kv-ir deserializer (for example, deserialize_int).
  • Work on supporting the old IR format in the kv-ir deserializer next.
  • Use kv-ir deserializer to implement ir::LogEventDeserializer.
  • Move the rest unused APIs to a new namespace for deprecation.

Let me know ur thoughts.

Comment on lines +62 to +63
auto get_encoding_type(ReaderInterface& reader, bool& is_four_bytes_encoding)
-> ystdlib::error_handling::Result<void>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a bad practice we should try to avoid: if we're using result-based error handling, then we should use the result to return is_four_bytes_encoding.

  • The easy fix is to make the return type ystdlib::error_handling::Result<bool>
  • However, it's still a bit hacky and personally I'd create an enum named EncodingType which contains the encoding type we support.

Depends on u whether u want to keep this PR to step 1, or go further to step 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants