Skip to content

Commit 3e82784

Browse files
committed
Fix - replace KeyNotFoundException with NotFoundException for consistency across handlers
1 parent b6d5e25 commit 3e82784

3 files changed

Lines changed: 13 additions & 10 deletions

File tree

MailCore.Application.Tests/Commands/SendEmailCommandHandlerTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using MailCore.Application.Commands.Emails.SendEmail;
22
using MailCore.Application.DTOs.Emails;
33
using MailCore.Application.Emails;
4+
using MailCore.Application.Exceptions;
45
using MailCore.Application.Interfaces.Services;
56
using MailCore.Domain.Entities;
67
using MailCore.Domain.Enums;
@@ -106,35 +107,35 @@ public async Task Handle_AddsMailRecipientForEachToAddress()
106107
// ?? Failure paths ????????????????????????????????????????????????????????
107108

108109
[Fact]
109-
public async Task Handle_SenderNotFound_ThrowsKeyNotFoundException()
110+
public async Task Handle_SenderNotFound_ThrowsNotFoundException()
110111
{
111112
_userRepo.Setup(r => r.GetByIdAsync(_userId, default)).ReturnsAsync((User?)null);
112113

113-
await Assert.ThrowsAsync<KeyNotFoundException>(
114+
await Assert.ThrowsAsync<NotFoundException>(
114115
() => _sut.Handle(ValidCommand(), default));
115116

116117
_emailRepo.Verify(r => r.AddAsync(It.IsAny<Email>(), default), Times.Never);
117118
}
118119

119120
[Fact]
120-
public async Task Handle_ThreadNotFound_ThrowsKeyNotFoundException()
121+
public async Task Handle_ThreadNotFound_ThrowsNotFoundException()
121122
{
122123
var threadId = Guid.NewGuid();
123124
_userRepo.Setup(r => r.GetByIdAsync(_userId, default)).ReturnsAsync(_sender);
124125
_threadRepo.Setup(r => r.GetByIdAsync(threadId, default)).ReturnsAsync((Domain.Entities.Thread?)null);
125126

126-
await Assert.ThrowsAsync<KeyNotFoundException>(
127+
await Assert.ThrowsAsync<NotFoundException>(
127128
() => _sut.Handle(ValidCommand(threadId), default));
128129
}
129130

130131
[Fact]
131-
public async Task Handle_RecipientNotFound_ThrowsKeyNotFoundException()
132+
public async Task Handle_RecipientNotFound_ThrowsNotFoundException()
132133
{
133134
_userRepo.Setup(r => r.GetByIdAsync(_userId, default)).ReturnsAsync(_sender);
134135
_userRepo.Setup(r => r.GetByEmailAsync("recipient@example.com", default))
135136
.ReturnsAsync((User?)null);
136137

137-
await Assert.ThrowsAsync<KeyNotFoundException>(
138+
await Assert.ThrowsAsync<NotFoundException>(
138139
() => _sut.Handle(ValidCommand(), default));
139140
}
140141
}

MailCore.Application/Commands/Emails/SendEmail/SendEmailCommandHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MailCore.Application.Emails;
2+
using MailCore.Application.Exceptions;
23
using MailCore.Application.Interfaces.Services;
34
using MailCore.Application.Mappers;
45
using MailCore.Application.Notifications;
@@ -44,7 +45,7 @@ public async Task Handle(SendEmailCommand command, CancellationToken cancellatio
4445
var sender = await _userRepository.GetByIdAsync(userId, cancellationToken);
4546
if (sender == null)
4647
{
47-
throw new KeyNotFoundException("Sender not found.");
48+
throw new NotFoundException("Sender not found.");
4849
}
4950

5051
var now = DateTime.UtcNow;
@@ -79,7 +80,7 @@ await _publisher.Publish(new EmailSentNotification(
7980
if (threadId.HasValue)
8081
{
8182
var thread = await _threadRepository.GetByIdAsync(threadId.Value, ct)
82-
?? throw new KeyNotFoundException("Thread not found.");
83+
?? throw new NotFoundException("Thread not found.");
8384

8485
thread.Touch();
8586
return thread;

MailCore.Application/Emails/EmailComposer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using MailCore.Application.Interfaces.Services;
1+
using MailCore.Application.Exceptions;
2+
using MailCore.Application.Interfaces.Services;
23
using MailCore.Application.Models;
34
using MailCore.Domain.Entities;
45
using MailCore.Domain.Enums;
@@ -34,7 +35,7 @@ public async Task AddRecipientsAsync(
3435
.Distinct(StringComparer.OrdinalIgnoreCase))
3536
{
3637
var user = await _userRepository.GetByEmailAsync(address, ct)
37-
?? throw new KeyNotFoundException($"Recipient not found: {address}");
38+
?? throw new NotFoundException($"Recipient not found: {address}");
3839

3940
var mr = MailRecipient.Create(user.Id, email.Id, type, receivedAt);
4041
await _mailRecipientRepository.AddAsync(mr, ct);

0 commit comments

Comments
 (0)