cap:error-state - error reporting

The error-state capability is used when a thing can report an error, such as a humidifier running out of water or a autonomous vacuum getting stuck.

if(thing.matches('cap:error-state')) {
        if(thing.error) {
                console.log('Error is:', thing.error);
        }
}

API

error()

Get the current error or null if no error.

Returns:Promise that resolves to a code if the thing is currently in an error state, or null if no error state.

Example:

thing.error()
        .then(err => ...)
        .catch(...);

const error = await thing.error();

Events

errorChanged

The current error has changed. The payload will be the current error state as a code or null.

Example:

thing.on('errorChanged', error => console.log('Error state:', error));
error

Emitted when an error occurs. The payload will be the error.

Example:

thing.on('error', error => console.log('Error occured:', error));
errorCleared

Emitted when the thing no longer has an error.

Example:

thing.on('errorCleared', () => console.log('Thing no longer has an error'));

Protected methods

updateError(batteryLevel)

Update the current error state.

Arguments:
  • error (code) – The new error state as a code or null if no error.

Example:

this.updateError('some-error');
this.updateError(null);

Implementing capability

When implementing this capability the implementor needs to call updateError whenever an error state is entered or left.

const { Thing, ErrorState } = require('abstract-things');

class Example extends Thing.with(ErrorState) {

}