Skip to content

Commit

Permalink
added a private method to retrieve the command name
Browse files Browse the repository at this point in the history
  • Loading branch information
Dracks committed Sep 22, 2023
1 parent a990d62 commit aa1e784
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/command-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export class CommandBus<CommandBase extends ICommand = ICommand>
this._publisher = _publisher;
}

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

/**
* Executes a command.
* @param command The command to execute.
Expand All @@ -58,9 +63,8 @@ export class CommandBus<CommandBase extends ICommand = ICommand>
const commandId = this.getCommandId(command);
const handler = this.handlers.get(commandId);
if (!handler) {
throw new CommandHandlerNotFoundException(
Object.getPrototypeOf(command).constructor.name,
);
const commandName = this.getCommandName(command);
throw new CommandHandlerNotFoundException(commandName);
}
this._publisher.publish(command);
return handler.execute(command);
Expand Down

0 comments on commit aa1e784

Please sign in to comment.