Refactor error handling in Bumper, Media, Group, and Junction components to include contextual information
ci / build-backend (push) Successful in 1m35s
ci / build-frontend (push) Successful in 51s
ci / tests (push) Successful in 1m46s
ci / sonar (push) Successful in 4m43s

Updated error definitions in BumperErrors, MediaErrors, GroupErrors, and TemplateErrors to accept a parameter for contextual information, enhancing the clarity of error messages. Refactored related command handlers to utilize these updated error messages, ensuring that users receive specific details about the usage context when attempting to delete resources. Adjusted unit tests to verify the correctness of the new error handling logic.
This commit is contained in:
Leonid Pershin
2026-07-28 13:19:30 +03:00
parent f895e1ddbc
commit f924891f3b
12 changed files with 227 additions and 27 deletions
@@ -155,7 +155,7 @@ public class DeleteGuardsTests
);
Assert.False(result.IsSuccess);
Assert.Equal(MediaErrors.InUse, result.Error);
Assert.Equal(MediaErrors.InUse(string.Empty).Code, result.Error.Code);
// Файлы не тронуты: до чистки хранилища дело не дошло.
await storage
.DidNotReceiveWithAnyArgs()
@@ -184,7 +184,7 @@ public class DeleteGuardsTests
).Handle(new DeleteMediaAssetCommand(asset.Id), CancellationToken.None);
Assert.False(result.IsSuccess);
Assert.Equal(MediaErrors.InUse, result.Error);
Assert.Equal(MediaErrors.InUse(string.Empty).Code, result.Error.Code);
}
[Fact]
@@ -222,7 +222,7 @@ public class DeleteGuardsTests
);
Assert.False(result.IsSuccess);
Assert.Equal(GroupErrors.InUse, result.Error);
Assert.Equal(GroupErrors.InUse(string.Empty).Code, result.Error.Code);
}
/// <summary>Запасная группа шаблона внешнего ключа не имеет — её пришлось бы ловить отдельно.</summary>
@@ -248,7 +248,38 @@ public class DeleteGuardsTests
);
Assert.False(result.IsSuccess);
Assert.Equal(GroupErrors.InUse, result.Error);
Assert.Equal(GroupErrors.InUse(string.Empty).Code, result.Error.Code);
}
/// <summary>
/// Отказ должен называть адрес: «используется в сетке» без имени канала отправляет обходить
/// их все руками. Связи полные — значит и канал в тексте есть.
/// </summary>
[Fact]
public async Task DeleteGroup_UsedByChannel_NamesIt()
{
var fixture = new TestDb();
var group = Group.Create("G");
var channel = Channel.Create("Первый", $"first-{Guid.NewGuid():N}", T0);
var template = ScheduleTemplate.Create(channel.Id, "Сетка");
template.SetFallbackGroup(group.Id);
await using (var seed = fixture.New())
{
seed.Groups.Add(group);
seed.Channels.Add(channel);
seed.ScheduleTemplates.Add(template);
await seed.SaveChangesAsync(CancellationToken.None);
}
await using var db = fixture.New();
var result = await new DeleteGroupCommandHandler(db).Handle(
new DeleteGroupCommand(group.Id),
CancellationToken.None
);
Assert.False(result.IsSuccess);
Assert.Contains("Первый", result.Error.Message, StringComparison.Ordinal);
}
[Fact]