Skip to content

feat: add onError callback for custom error handling#11

Merged
EliaTolin merged 1 commit intomainfrom
feature/error-handler
Jan 23, 2026
Merged

feat: add onError callback for custom error handling#11
EliaTolin merged 1 commit intomainfrom
feature/error-handler

Conversation

@EliaTolin
Copy link
Owner

Summary

This PR implements the error handler feature as requested in #4, allowing developers to capture and handle serialization/deserialization errors.

Changes

  • Added CacheError class with detailed error information:

    • key: The cache key that failed
    • type: Type of error (serialization, deserializationJson, deserializationFromJson)
    • error: The underlying exception
    • stackTrace: Stack trace for debugging
    • rawData: The data that failed (if available)
    • message: Human-readable error message
  • Added CacheErrorType enum to distinguish error types

  • Added onError callback parameter to call() method

Usage Example

final data = await RemoteCaching.instance.call<User>(
  'user_profile',
  remote: () async => await fetchUser(),
  fromJson: (json) => User.fromJson(json as Map<String, dynamic>),
  onError: (error) {
    // Log to Sentry, Datadog, etc.
    analytics.logError('cache_error', {
      'key': error.key,
      'type': error.type.name,
      'message': error.message,
    });
  },
);

Backward Compatibility

  • No breaking changes
  • If onError is not provided, existing silent fallback behavior is preserved
  • Errors are still logged in verbose mode

Test plan

  • All existing tests pass
  • New tests for onError callback invocation
  • Tests for CacheError message formatting
  • Tests for backward compatibility (no callback)
  • Flutter analyze passes with no issues

Closes #4

🤖 Generated with Claude Code

Introduces an optional onError callback to the call() method, allowing
developers to handle serialization/deserialization errors:
- Log errors to external services (Sentry, Datadog, etc.)
- Track cache failure metrics
- Debug serialization issues in development

Key changes:
- Added CacheError class with error details (key, type, error, stackTrace, rawData)
- Added CacheErrorType enum (serialization, deserializationJson, deserializationFromJson)
- Added onError parameter to call() method
- Backward compatible: errors still fallback gracefully without callback
- Added comprehensive tests
- Updated README with documentation

Closes #4

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@EliaTolin EliaTolin force-pushed the feature/error-handler branch from 2cd1545 to ba136d3 Compare January 7, 2026 13:07
@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 68.00000% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.30%. Comparing base (5bd88bf) to head (ba136d3).

Files with missing lines Patch % Lines
lib/src/remote_caching_impl.dart 42.85% 8 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #11      +/-   ##
==========================================
- Coverage   88.80%   86.30%   -2.50%     
==========================================
  Files           3        4       +1     
  Lines         125      146      +21     
==========================================
+ Hits          111      126      +15     
- Misses         14       20       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@EliaTolin EliaTolin merged commit 9c1f52b into main Jan 23, 2026
3 checks passed
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.

[FEATURE] Add Error Handler

2 participants