Skip to content

Implement error callback system for simplified error handling - #7

Merged
wysaid merged 8 commits into
mainfrom
copilot/fix-dcb62d71-0b64-45d4-a9cc-260fdfe72bc1
Aug 14, 2025
Merged

Implement error callback system for simplified error handling#7
wysaid merged 8 commits into
mainfrom
copilot/fix-dcb62d71-0b64-45d4-a9cc-260fdfe72bc1

Conversation

Copilot AI commented Aug 13, 2025

Copy link
Copy Markdown
Contributor

This PR transforms the error callback system from per-provider to global configuration, making error handling more convenient and accessible throughout the CCAP implementation as requested in feedback.

Key Changes

Error Callback System

  • C++ Interface: ccap::setErrorCallback() and ccap::getErrorCallback() replace per-provider callbacks
  • C Interface: ccap_set_error_callback() for global error handling
  • Thread-safe: Global callback storage with mutex protection
  • Exception-safe: User callback exceptions are caught to prevent library crashes

Simplified Usage

Before (Per-Provider):

ccap::Provider provider;
provider.setErrorCallback([](ccap::ErrorCode errorCode, const std::string& description) {
    std::cerr << "Error: " << description << std::endl;
});

After (Global):

// Set once globally
ccap::setErrorCallback([](ccap::ErrorCode errorCode, const std::string& description) {
    std::cerr << "Error: " << description << std::endl;
});

ccap::Provider provider; // No per-provider setup needed

Comprehensive Error Codes

Detailed error codes cover all common scenarios:

  • NoDeviceFound - No camera device found during discovery
  • InvalidDevice - Invalid device name or index specified
  • DeviceOpenFailed - Camera device open failed
  • DeviceStartFailed - Camera start failed
  • UnsupportedResolution - Requested resolution not supported
  • UnsupportedPixelFormat - Requested pixel format not supported
  • FrameCaptureTimeout - Frame capture timeout
  • FrameCaptureFailed - Frame capture failed
  • And more covering various error scenarios

Benefits

Convenience

  • One-time Setup: Configure error handling globally instead of per provider instance
  • Implementation Access: CCAP internal code can easily access and invoke error callbacks
  • Consistent Handling: All camera operations use the same error reporting mechanism

Updated Examples

All desktop examples now demonstrate the global callback approach:

  • C++ examples use ccap::setErrorCallback()
  • C examples use ccap_set_error_callback()
  • Consistent error handling patterns across all examples

Implementation Details

Error Reporting Integration

  • Added error reporting calls throughout Linux (V4L2) and Windows (DirectShow) implementations
  • Global callbacks are triggered for device discovery failures, open failures, start failures, etc.
  • Maintains backward compatibility for existing error handling through return values

Thread Safety

  • Global callback storage protected by std::mutex
  • Thread-local storage ensures thread safety for C interface string returns
  • Exception handling prevents user callback errors from crashing the library

Documentation Updates

  • Updated README.md with global callback examples for both C++ and C interfaces
  • Enhanced C_Interface.md documentation with detailed error handling section
  • All documentation now reflects the simplified global approach

Testing

  • ✅ All existing functionality preserved and working
  • ✅ Global error callbacks working correctly for both C++ and C interfaces
  • ✅ Error code to string conversion working
  • ✅ Thread safety validated
  • ✅ Exception safety in callbacks validated
  • ✅ Cross-platform compatibility maintained
  • ✅ All examples updated and building successfully

This enhancement significantly simplifies error handling setup while providing the same detailed error information, making the library more convenient to use and integrate.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits August 13, 2025 20:04
Co-authored-by: wysaid <1430725+wysaid@users.noreply.github.com>
Co-authored-by: wysaid <1430725+wysaid@users.noreply.github.com>
Copilot AI changed the title [WIP] 我想给本项目添加错误回调相关实现, 支持外部设置一个处理错误的回调函数, 然后当 ccap 内部遇到错误的时候, 如果设置了这个错误回调, 那么可以直接调用这个回调函数告知到用户。 �... Add comprehensive error callback system for detailed error reporting Aug 13, 2025
Copilot AI requested a review from wysaid August 13, 2025 20:09
@wysaid

wysaid commented Aug 14, 2025

Copy link
Copy Markdown
Owner

The current implementation differs from my expectations. I want the error callback to be configured globally, not per provider. With a globally configured callback, the CCAP implementation can access and invoke it more conveniently, not limited to provider members. Additionally, some examples in the examples directory currently omit this error callback; please add it to all of them.
@copilot

Co-authored-by: wysaid <1430725+wysaid@users.noreply.github.com>

Copilot AI commented Aug 14, 2025

Copy link
Copy Markdown
Contributor Author

The current implementation differs from my expectations. I want the error callback to be configured globally, not per provider. With a globally configured callback, the CCAP implementation can access ...

I've implemented the global error callback system as requested. The changes include:

Global Error Callback Functions:

  • C++: ccap::setGlobalErrorCallback() and ccap::getGlobalErrorCallback()
  • C: ccap_set_global_error_callback()

All Examples Updated:

  • Removed per-provider error callbacks from all 8 desktop examples
  • Added global error callback setup to each example
  • All examples now use the global approach consistently

Thread-Safe Implementation:

  • Global callback storage with mutex protection
  • Error reporting throughout the CCAP implementation now uses the global callback
  • Maintains backward compatibility while providing the convenience you requested

The global callback is now accessible from all CCAP implementation code without being limited to specific provider instances. Commit: 425c680

Co-authored-by: wysaid <1430725+wysaid@users.noreply.github.com>
Copilot AI changed the title Add comprehensive error callback system for detailed error reporting Implement global error callback system for simplified error handling Aug 14, 2025
@wysaid

wysaid commented Aug 14, 2025

Copy link
Copy Markdown
Owner

Go on; remove the "global" keyword from your naming, do not use "setGlobalErrorCallback"; use "setErrorCallback" instead. The C interface does the same. @copilot

Co-authored-by: wysaid <1430725+wysaid@users.noreply.github.com>

Copilot AI commented Aug 14, 2025

Copy link
Copy Markdown
Contributor Author

Go on; remove the "global" keyword from your naming, do not use "setGlobalErrorCallback"; use "setErrorCallback" instead. The C interface does the same. @copilot

I've removed the "global" keyword from all error callback function names as requested:

C++ Interface:

  • setGlobalErrorCallback()setErrorCallback()
  • getGlobalErrorCallback()getErrorCallback()

C Interface:

  • ccap_set_global_error_callback()ccap_set_error_callback()

Updated all header files, implementations, examples, and documentation. The functionality remains exactly the same - only the naming has been simplified. Build verification successful.

Commit: d2300cf

Copilot AI changed the title Implement global error callback system for simplified error handling Implement error callback system for simplified error handling Aug 14, 2025
@coderabbitai

coderabbitai Bot commented Aug 14, 2025

Copy link
Copy Markdown

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Join our Discord community for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@wysaid
wysaid marked this pull request as ready for review August 14, 2025 09:00
@wysaid
wysaid merged commit 4aac7a7 into main Aug 14, 2025
13 checks passed
@wysaid
wysaid deleted the copilot/fix-dcb62d71-0b64-45d4-a9cc-260fdfe72bc1 branch August 14, 2025 09:01
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