Skip to content

Commit

Permalink
♻️ Improve error messages (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
spontoreau authored Nov 11, 2023
2 parents 15c28b2 + 55dc48b commit 0ddb74e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
7 changes: 7 additions & 0 deletions src/core/errors/multipleHandlers.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class MultipleHandlersError extends Error {
constructor(messageType: string) {
super(
`Multiple handlers are registred, use publish function instead (MESSAGE_TYPE: ${messageType})`,
);
}
}
7 changes: 0 additions & 7 deletions src/core/errors/multipleHandlersMessage.error.ts

This file was deleted.

6 changes: 4 additions & 2 deletions src/core/errors/unknownMessage.error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export class UnknownMessageError extends Error {
constructor() {
super(`The message doesn't have any corresponding handler`);
constructor(messageType: string) {
super(
`The message doesn't have any corresponding handler (MESSAGE_TYPE: ${messageType})`,
);
}
}
13 changes: 11 additions & 2 deletions src/core/mediator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Message } from './message';
import { MessageHandler } from './messageHandler';
import { clear, mediator } from './mediator';
import { UnknownMessageError } from './errors/unknownMessage.error';
import { SendToMultipleHandlersError } from './errors/multipleHandlersMessage.error';
import { MultipleHandlersError } from './errors/multipleHandlers.error';
import { should } from 'chai';
import { Middleware } from './middlewares';

Expand Down Expand Up @@ -87,6 +87,9 @@ describe('mediator.send', () => {

// Assert
actual.should.be.instanceOf(UnknownMessageError);
actual.message.should.be.equal(
`The message doesn't have any corresponding handler (MESSAGE_TYPE: CREATE_PERSON)`,
);
});

it('should throw error when sending a message through the mediator that can be handled with multiple handlers', async () => {
Expand Down Expand Up @@ -126,7 +129,10 @@ describe('mediator.send', () => {
}

// Assert
actual.should.be.instanceOf(SendToMultipleHandlersError);
actual.should.be.instanceOf(MultipleHandlersError);
actual.message.should.be.equal(
`Multiple handlers are registred, use publish function instead (MESSAGE_TYPE: CREATE_PERSON)`,
);
});
});

Expand Down Expand Up @@ -213,6 +219,9 @@ describe('mediator.publish', () => {

// Assert
actual.should.be.instanceOf(UnknownMessageError);
actual.message.should.be.equal(
`The message doesn't have any corresponding handler (MESSAGE_TYPE: CREATE_PERSON)`,
);
});
});

Expand Down
6 changes: 3 additions & 3 deletions src/core/mediator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Message } from './message';
import { MessageHandler } from './messageHandler';
import { MessageResult } from './messageResult';
import { SendToMultipleHandlersError } from './errors/multipleHandlersMessage.error';
import { MultipleHandlersError } from './errors/multipleHandlers.error';
import { UnknownMessageError } from './errors/unknownMessage.error';
import { AddOptions, Middleware, middlewares } from './middlewares';

Check warning on line 6 in src/core/mediator.ts

View workflow job for this annotation

GitHub Actions / call-shared-workflow / lint

'Middleware' is defined but never used

Check warning on line 6 in src/core/mediator.ts

View workflow job for this annotation

GitHub Actions / call-shared-workflow / lint

'Middleware' is defined but never used

Check warning on line 6 in src/core/mediator.ts

View workflow job for this annotation

GitHub Actions / call-shared-workflow / lint

'Middleware' is defined but never used

Expand All @@ -19,7 +19,7 @@ const getHandlers = <TKey extends string, TMessage extends Message<TKey>>(
messageType: TKey,
): Array<MessageHandler<TMessage>> => {
if (!registry.has(messageType)) {
throw new UnknownMessageError();
throw new UnknownMessageError(messageType);
}

const handlers = registry.get(messageType) as Array<MessageHandler<TMessage>>;
Expand All @@ -33,7 +33,7 @@ const send = async <TMessage extends Message>({
const handlers = getHandlers<typeof type, TMessage>(type);

if (handlers.length > 1) {
throw new SendToMultipleHandlersError();
throw new MultipleHandlersError(type);
}

return handlers[0](data);
Expand Down

0 comments on commit 0ddb74e

Please sign in to comment.