diff --git a/index.d.ts b/index.d.ts index 7834829..6e57ee6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -27,6 +27,7 @@ export interface I2CSlaveDevice { write16(registerNumber: number, value: number): Promise; } export declare class OperationError extends Error { + constructor(message: string); } export declare function requestI2CAccess(): Promise; export {}; diff --git a/index.js b/index.js index 65d1152..ffe19f3 100644 --- a/index.js +++ b/index.js @@ -45,15 +45,27 @@ class I2CPort { }); return { slaveAddress, - read8: cmd => bus.readByte(slaveAddress, cmd), - read16: cmd => bus.readWord(slaveAddress, cmd), - write8: (cmd, byte) => bus.writeByte(slaveAddress, cmd, byte), - write16: (cmd, word) => bus.writeWord(slaveAddress, cmd, word) + read8: cmd => bus.readByte(slaveAddress, cmd).catch(error => { + throw new OperationError(error); + }), + read16: cmd => bus.readWord(slaveAddress, cmd).catch(error => { + throw new OperationError(error); + }), + write8: (cmd, byte) => bus.writeByte(slaveAddress, cmd, byte).catch(error => { + throw new OperationError(error); + }), + write16: (cmd, word) => bus.writeWord(slaveAddress, cmd, word).catch(error => { + throw new OperationError(error); + }) }; } } exports.I2CPort = I2CPort; class OperationError extends Error { + constructor(message) { + super(message); + this.name = this.constructor.name; + } } exports.OperationError = OperationError; async function requestI2CAccess() {