Skip to content

Commit

Permalink
[FIX] expose stop error argument, as client handler will want this
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed Nov 9, 2022
1 parent b95bffb commit 8ed6228
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
11 changes: 6 additions & 5 deletions nats-base-client/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ export type Service = {
*/
reset(): void;
/**
* Stop the service returning a promise that resolves to null or an error
* depending on whether stopping resulted in an error.
* Stop the service returning a promise once the service completes.
* If the service was stopped due to an error, that promise resolves to
* the specified error
*/
stop(): Promise<null | Error>;
stop(err?: Error): Promise<null | Error>;
};

/**
Expand Down Expand Up @@ -578,8 +579,8 @@ export class ServiceImpl implements Service {
return this._done;
}

stop(): Promise<null | Error> {
return this.close();
stop(err?: Error): Promise<null | Error> {
return this.close(err);
}

reset(): void {
Expand Down
42 changes: 42 additions & 0 deletions tests/service_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import {
assertEquals,
assertExists,
assertRejects,
fail,
} from "https://deno.land/std@0.125.0/testing/asserts.ts";
import {
Empty,
ErrorCode,
JSONCodec,
Msg,
NatsError,
QueuedIterator,
} from "../nats-base-client/mod.ts";
import { NatsConnectionImpl } from "../nats-base-client/nats.ts";
Expand Down Expand Up @@ -199,3 +202,42 @@ Deno.test("service - handler error", async () => {

await cleanup(ns, nc);
});

Deno.test("service - stop error", async () => {
const { ns, nc } = await setup({
authorization: {
users: [{
user: "a",
password: "a",
permissions: {
subscribe: {
deny: "fail",
},
},
}],
},
}, { user: "a", pass: "a" });

const service = await addService(nc, {
name: "test",
endpoint: {
subject: "fail",
handler: (err): Promise<void> => {
if (err) {
service.stop(err);
return Promise.reject(err);
}
fail("shouldn't have subscribed");
return Promise.resolve();
},
},
});

const err = await service.done as NatsError;
assertEquals(
err.code,
ErrorCode.PermissionsViolation,
);

await cleanup(ns, nc);
});

0 comments on commit 8ed6228

Please sign in to comment.