Skip to content

Commit

Permalink
Merge pull request #1492 from Dracks/master
Browse files Browse the repository at this point in the history
feat(): Improve command bus to throw error with command name
  • Loading branch information
kamilmysliwiec authored Sep 28, 2023
2 parents f0d75e1 + 076c584 commit e17cdc7
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/command-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export class CommandBus<CommandBase extends ICommand = ICommand>
const commandId = this.getCommandId(command);
const handler = this.handlers.get(commandId);
if (!handler) {
throw new CommandHandlerNotFoundException(commandId);
const commandName = this.getCommandName(command);
throw new CommandHandlerNotFoundException(commandName);
}
this._publisher.publish(command);
return handler.execute(command);
Expand Down Expand Up @@ -97,6 +98,11 @@ export class CommandBus<CommandBase extends ICommand = ICommand>
return commandMetadata.id;
}

private getCommandName(command: Type<CommandBase>): string {
const { constructor } = Object.getPrototypeOf(command);
return constructor.name as string;
}

private reflectCommandId(handler: CommandHandlerType): string | undefined {
const command: Type<ICommand> = Reflect.getMetadata(
COMMAND_HANDLER_METADATA,
Expand Down

0 comments on commit e17cdc7

Please sign in to comment.