Handling errors

When an API function call fails, you should handle the error. In QML or C++, a signal is emitted when certain API calls fail. For example, if capturePhoto() fails, the photoCaptureFailed() signal is emitted. To handle this failure, your app can implement the onPhotoCaptureFailed() signal handler. In the Camera C API, your app should check the return code of each API function call. You might also want to monitor for camera status changes in case an error occurs during a camera operation. Camera status is defined in camera_devstatus_t. You can monitor status changes by implementing the status callback or by registering for event notifications. For an example that uses the status callback, see Camera resource management.

In some situations, the operating system might reassign resources to higher priority applications. When such a situation happens, your app might lose the resources, such as image buffers, that it's using. If you are using the QML or C++ API, buffer cleanup is done automatically. If you are using the C API, you can register for resource management notifications, which allow your app to clean up before losing any camera resources that it's using. For more information, refer to Camera resource management.

The code sample below demonstrates how to use the onPhotoCaptureFailed() signal handler to report the failure to the user.

Container {
    // The QML Camera object
    Camera {    
        id: qmlCameraObj  
  
        // ...

        onPhotoCaptureFailed: {
            qmlToast.body = "Photo could not be taken.";
            qmlToast.show();
            
            return;
        }
        // ...
    
    }
    // ...
}                     
                

The code sample below connects the onPhotoCaptureFailed() slot to the photoCaptureFailed() signal. The slot function reports the failure to the user.

ApplicationUI::ApplicationUI(Application *app) : QObject(app)
{
    bool result;
    // ...
	
	// Connect signal to slot
    result = connect(m_pCamera,
                     SIGNAL(photoCaptureFailed(bb::cascades::multimedia::CameraError::Type error)),
                     this,
                     SLOT(onPhotoCaptureFailed()));
	Q_ASSERT(result);
	
	// ...
}

void ApplicationUI::onCapturePhotoFailed()
{
    showToast("Failed to capture photo.");
}

Check the return code of each API function call and handle error cases. The following code sample checks for errors in a camera_take_photo() call.

camera_error_t err;

/* ... */

err = camera_take_photo(handle,
                    &shutter_callback,
                    NULL,
                    NULL,
                    &still_callback,
                    NULL,
                    false);
if (err != CAMERA_EOK) {
    fprintf(stderr, "camera_take_photo() error %d", err);

    /* If required, handle specific errors */
    /* ... */

} else {
    /* Successful call */
}

Last modified: 2015-05-07



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus