Camera/Image stream: show error message?

I’m extending the Blink app and got struggle with the image recovery.
The Blink server gives a http error if the camera (http post for creating new snapshot) is busy. In this case I want to abort the image refresh in the Homey app and show a red message “camera is busy”.

I followed the Homey API description and trow an error. But the camera view is still busy with the circeling refresh icon and no message is shown.

The code is:

        const _snapshotImage = new Homey.Image();
        _snapshotImage.setStream(async stream => {
            self.log('_registerSnapshotImage() -> setStream -> Capture_snap');
            self._getNewSnapshotUrl()
                .then( 
                     //...get the immage via URL
                    res.body.pipe(stream);
                })
                .catch((error) => {
                    self.log("_registerSnapshotImage Error calling _getNewSnapshotUrl: "+error);
                    throw new Error('Camera is busy');
                });
        _snapshotImage.register()
            .then(() => self.log('_registerSnapshotImage() -> registered'))
            .then(() => self.setCameraImage('snapshot', 'Snapshot', _snapshotImage))
            .catch(self.error);

I think it’s similar to the example below “Using a stream”:
https://apps-sdk-v2.developer.athom.com/tutorial-Images.html

Has anyone an example how to deal with errors on image refresh? Many thanks.
Ronny

Do you also use the rest of the code as shown in the example, specifically this:

_snapshotImage.register().catch(console.error);

If so, you’re just catching the error and showing it on console, but I don’t think it will be propagated back the the SDK.

You could also try and explicitly destroy the stream:

…
.catch(error => {
  self.log(…);
  const err = Error('Camera is busy');
  stream.destroy(err);
  throw err;
});

Hello Robert,

the _snapshotImage.register() is there. I edited the code above.

The stream.destroy results in an exception and the app is killed.
Instead, stream.close( ) is working. One step ahead :slight_smile:

The image refresh ist stopped, but it seems I can’t set an error text. Homey displays the default error caused by the empty stream.

It seems, Homey is checking the exception in another way I thought. After throwing the error, Homey tries to get the image again (but then there is stil the http error), and that’s causing a delay until the refresh got his timeout. So invalid_content_typüe is not nice, but working for the moment.

Have you tried removing the .catch() after the .register()?

Yes, for one try. But thats not the fault. The register( ) doesnt throw an exeption. Thanks for your help.

I have implemented a way to wait for a updated snapshot URL if the camera is too slow. So I hope this error will not occour in the normal usage.