Skip to content

Commit 79ebaa4

Browse files
authored
BusinessErrorCollectionをBusinessExceptionのコンストラクターで受け取れるようにする (#116)
* BusinessErrorCollection対応のコンストラクタを追加 BusinessExceptionにBusinessErrorCollection?型を受け取る2種のコンストラクタ(内部例外あり/なし)を追加。null時は空配列として処理。これに対応するテストも追加し、null渡し時にGetErrors()が空となることを検証。 * 誤字を修正 * コンストラクターのテストパターンを追加
1 parent 8846752 commit 79ebaa4

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

src/Maris.Core/BusinessException.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public BusinessException()
1717
{
1818
}
1919

20+
/// <summary>
21+
/// 業務エラーのリストを指定して
22+
/// <see cref="BusinessException"/> クラスの新しいインスタンスを初期化します。
23+
/// </summary>
24+
/// <param name="businessErrors">業務エラーのリスト。</param>
25+
public BusinessException(BusinessErrorCollection? businessErrors)
26+
: this(null, businessErrors?.ToArray() ?? [])
27+
{
28+
}
29+
2030
/// <summary>
2131
/// 業務エラーのリストを指定して
2232
/// <see cref="BusinessException"/> クラスの新しいインスタンスを初期化します。
@@ -27,6 +37,17 @@ public BusinessException(params BusinessError[] businessErrors)
2737
{
2838
}
2939

40+
/// <summary>
41+
/// 内部例外と業務エラーのリストを指定して
42+
/// <see cref="BusinessException"/> クラスの新しいインスタンスを初期化します。
43+
/// </summary>
44+
/// <param name="innerException">内部例外。</param>
45+
/// <param name="businessErrors">業務エラーのリスト。</param>
46+
public BusinessException(Exception? innerException, BusinessErrorCollection? businessErrors)
47+
: this(innerException, businessErrors?.ToArray() ?? [])
48+
{
49+
}
50+
3051
/// <summary>
3152
/// 内部例外と業務エラーのリストを指定して
3253
/// <see cref="BusinessException"/> クラスの新しいインスタンスを初期化します。

tests/Maris.Core.Tests/BusinessExceptionTest.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,90 @@
22

33
public class BusinessExceptionTest
44
{
5+
[Fact]
6+
public void Constructor_SetNullBusinessErrorCollection()
7+
{
8+
// Arrange
9+
BusinessErrorCollection? businessErrors = null;
10+
11+
// Act
12+
var businessEx = new BusinessException(businessErrors);
13+
14+
// Assert
15+
Assert.Empty(businessEx.GetErrors());
16+
Assert.Null(businessEx.InnerException);
17+
}
18+
19+
[Fact]
20+
public void Constructor_SetBusinessErrorCollection()
21+
{
22+
// Arrange
23+
BusinessErrorCollection businessErrors = new BusinessErrorCollection();
24+
businessErrors.AddOrMerge(new BusinessError("exceptionId1", new ErrorMessage("Error message 1.")));
25+
businessErrors.AddOrMerge(new BusinessError("exceptionId2", new ErrorMessage("Error message 2.")));
26+
27+
// Act
28+
var businessEx = new BusinessException(businessErrors);
29+
30+
// Assert
31+
Assert.Collection(
32+
businessEx.GetErrors(),
33+
error =>
34+
{
35+
Assert.Equal("exceptionId1", error.ExceptionId);
36+
Assert.Equal("Error message 1.", error.ErrorMessage);
37+
},
38+
error =>
39+
{
40+
Assert.Equal("exceptionId2", error.ExceptionId);
41+
Assert.Equal("Error message 2.", error.ErrorMessage);
42+
});
43+
Assert.Null(businessEx.InnerException);
44+
}
45+
46+
[Fact]
47+
public void Constructor_SetBusinessErrorCollectionWithInnerException()
48+
{
49+
// Arrange
50+
Exception innerEx = new Exception();
51+
BusinessErrorCollection businessErrors = new BusinessErrorCollection();
52+
businessErrors.AddOrMerge(new BusinessError("exceptionId1", new ErrorMessage("Error message 1.")));
53+
businessErrors.AddOrMerge(new BusinessError("exceptionId2", new ErrorMessage("Error message 2.")));
54+
55+
// Act
56+
var businessEx = new BusinessException(innerEx, businessErrors);
57+
58+
// Assert
59+
Assert.Collection(
60+
businessEx.GetErrors(),
61+
error =>
62+
{
63+
Assert.Equal("exceptionId1", error.ExceptionId);
64+
Assert.Equal("Error message 1.", error.ErrorMessage);
65+
},
66+
error =>
67+
{
68+
Assert.Equal("exceptionId2", error.ExceptionId);
69+
Assert.Equal("Error message 2.", error.ErrorMessage);
70+
});
71+
Assert.Same(innerEx, businessEx.InnerException);
72+
}
73+
74+
[Fact]
75+
public void Constructor_SetNullBusinessErrorCollectionWithInnerException()
76+
{
77+
// Arrange
78+
Exception innerEx = new Exception();
79+
BusinessErrorCollection? businessErrors = null;
80+
81+
// Act
82+
var businessEx = new BusinessException(innerEx, businessErrors);
83+
84+
// Assert
85+
Assert.Empty(businessEx.GetErrors());
86+
Assert.Same(innerEx, businessEx.InnerException);
87+
}
88+
589
[Fact]
690
public void BusinessErrors_ReturnsAddedBusinessErrors()
791
{

0 commit comments

Comments
 (0)