Skip to content

Commit

Permalink
fix(local-command-bus): throw on send with unregistered command handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lucagiove committed Nov 29, 2024
1 parent dd8bcbc commit f34eaf6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
5 changes: 5 additions & 0 deletions packages/ddd-toolkit/.changeset/slimy-queens-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fizzbuds/ddd-toolkit': minor
---

throw on send with unregistered command handler
26 changes: 13 additions & 13 deletions packages/ddd-toolkit/src/command-bus/local-command-bus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ describe('LocalCommandBus', () => {
describe('When send a foo command', () => {
it('Should log warning message', async () => {
const command = new FooCommand({ foo: 'bar' });
await commandBus.send(command);

expect(loggerMock.warn).toBeCalledWith(`No handler found for ${FooCommand.name}`);
await expect(async () => await commandBus.send(command)).rejects.toThrow(
`No handler found for ${FooCommand.name}`,
);
});
});
});
Expand All @@ -64,7 +64,7 @@ describe('LocalCommandBus', () => {
const command = new FooCommand({ foo: 'bar' });
await commandBus.send(command);

await waitFor(() => expect(FooHandlerMock).toBeCalledWith(command));
await waitFor(() => expect(FooHandlerMock).toHaveBeenCalledWith(command));
});
});

Expand Down Expand Up @@ -97,8 +97,8 @@ describe('LocalCommandBus', () => {
const command = new FooCommand({ foo: 'bar' });
await commandBus.send(command);

await waitFor(() => expect(FooHandlerMock).toBeCalledWith(command));
expect(BarHandlerMock).not.toBeCalled();
await waitFor(() => expect(FooHandlerMock).toHaveBeenCalledWith(command));
expect(BarHandlerMock).not.toHaveBeenCalled();
});
});

Expand All @@ -107,8 +107,8 @@ describe('LocalCommandBus', () => {
const command = new BarCommand({ foo: 'bar' });
await commandBus.send(command);

expect(FooHandlerMock).not.toBeCalled();
expect(BarHandlerMock).toBeCalledWith(command);
expect(FooHandlerMock).not.toHaveBeenCalled();
expect(BarHandlerMock).toHaveBeenCalledWith(command);
});
});
});
Expand All @@ -135,21 +135,21 @@ describe('LocalCommandBus', () => {

it('handler should be called two times', async () => {
await waitFor(() => {
expect(handlerMock).toBeCalledTimes(2);
expect(handlerMock).toHaveBeenCalledTimes(2);
});
});

it('should not log error for failing handler', async () => {
await waitFor(() => {
expect(handlerMock).toBeCalledTimes(2);
expect(loggerMock.error).not.toBeCalled();
expect(handlerMock).toHaveBeenCalledTimes(2);
expect(loggerMock.error).not.toHaveBeenCalled();
});
});

it('should log one retry for failing handler', async () => {
await waitFor(() => {
expect(loggerMock.warn).toBeCalledTimes(1);
expect(loggerMock.warn).toBeCalledWith(
expect(loggerMock.warn).toHaveBeenCalledTimes(1);
expect(loggerMock.warn).toHaveBeenCalledWith(
expect.stringContaining(
'FooCommandHandlerOk failed to handle FooCommand command. Attempt 1/3',
),
Expand Down
6 changes: 2 additions & 4 deletions packages/ddd-toolkit/src/command-bus/local-command-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ export class LocalCommandBus implements ICommandBus {

public async send<C extends ICommand<unknown, unknown>>(command: C): Promise<void> {
const handler = this.handlers[command.name] as ICommandHandler<C>;
if (!handler) {
this.logger.warn(`No handler found for ${command.name}`);
return;
}
if (!handler) throw new Error(`No handler found for ${command.name}`);

void this.handleCommand(command, handler);
}

public async sendSync<C extends ICommand<unknown, unknown>>(command: C): Promise<C['_returnType']> {
const handler = this.handlers[command.name] as ICommandHandler<C>;
if (!handler) throw new Error(`No handler found for ${command.name}`);

return await handler.handle(command);
}

Expand Down

0 comments on commit f34eaf6

Please sign in to comment.