diff --git a/packages/binding-mbus/src/mbus-connection.ts b/packages/binding-mbus/src/mbus-connection.ts index 8abe56403..fe7b8af31 100644 --- a/packages/binding-mbus/src/mbus-connection.ts +++ b/packages/binding-mbus/src/mbus-connection.ts @@ -156,7 +156,7 @@ export class MBusConnection { } } - async execute(op: PropertyOperation): Promise> { + async execute(op: PropertyOperation): Promise | undefined> { this.trigger(); return op.execute(); } @@ -182,7 +182,7 @@ export class MBusConnection { // inform all the operations that the connection cannot be recovered this.queue.forEach((transaction) => { transaction.operations.forEach((op) => { - op.failed(`${error}`); + op.failed(error instanceof Error ? error : new Error(JSON.stringify(error))); }); }); } @@ -215,7 +215,9 @@ export class MBusConnection { } catch (err) { warn(`Read operation failed on unit ${transaction.unitId}. ${err}.`); // inform all operations and the invoker - transaction.operations.forEach((op) => op.failed(`${error}`)); + transaction.operations.forEach((op) => + op.failed(error instanceof Error ? error : new Error(JSON.stringify(error))) + ); throw err; } } @@ -278,11 +280,13 @@ export class PropertyOperation { * Trigger execution of this operation. * */ - async execute(): Promise> { - return new Promise((resolve, reject) => { - this.resolve = resolve; - this.reject = reject; - }); + async execute(): Promise<(Content | PromiseLike) | undefined> { + return new Promise( + (resolve: (value?: Content | PromiseLike) => void, reject: (reason?: Error) => void) => { + this.resolve = resolve; + this.reject = reject; + } + ); } /** @@ -315,12 +319,11 @@ export class PropertyOperation { const resp = new Content("application/json", Readable.from(JSON.stringify(payload))); - // resolve the Promise given to the invoking script - if (this.resolve) { - this.resolve(resp); - } else { - warn("resolve undefined"); + if (!this.resolve) { + throw new Error("Function 'done' was invoked before executing the Mbus operation"); } + // resolve the Promise given to the invoking script + this.resolve(resp); } /** @@ -328,13 +331,12 @@ export class PropertyOperation { * * @param reason Reason of failure */ - failed(reason: string): void { - warn("Operation failed:", reason); - // reject the Promise given to the invoking script - if (this.reject) { - this.reject(new Error(reason)); - } else { - warn("reject undefined"); + failed(reason: Error): void { + warn(`Operation failed: ${reason}`); + if (!this.reject) { + throw new Error("Function 'failed' was invoked before executing the Mbus operation"); } + // reject the Promise given to the invoking script + this.reject(reason); } }