I'm new to DDD and I trying to create an API using DDD concepts for study purposes. Today, I faced a scenario where I've to create an Application Service to expose a functionality to be cosumed from the external world. The scenario that I came across is to consult in the repository some Domain Entity informations by its id and if the Entity does not exists it should throw a Domain Error to the client, informing that the Entity does not exists. Therefore, my questions are:
- Should I throw Domain Errors in Application Services?
- Should this scenario be converted to a Domain Service that is called by the Application service, where the Domain Service treats the repository's response and throws the Error? If yes, how can I nominate this service?
My code looks like this:
import { CostumerRepository } from '@/main/com/acme/sales/domain/repositories';
import { CostumerNotFound } from '@/main/com/acme/sales/domain/errors';
import { GetCostumerHistoryInputDTO } from './dtos/inputs';
import { CostumerHistoryOutputDTO } from './dtos/output';
export class GetCostumerHistory {
constructor(private readonly costumerRepository: CostumerRepository) {}
execute(input: GetCostumerHistoryInputDTO): CostumerHistoryOutputDTO {
const costumer = this.costumerRepository.getById(input.costumerId);
if (costumer == null) throw new CostumerNotFound();
return new CostumerHistoryOutputDTO({ costumer });
}
}
nil
might be easier for callers to handle than throwing an exception.