Skip to content

Commit 3f38fce

Browse files
committed
Atualiza versão e melhora tratamento de exceções
Esta atualização inclui melhorias na estrutura de tratamento de exceções e testes. * Atualização de dependências - Alterado `SPPreview` de `-preview-4.3` para `-preview-4.4`. * Melhoria no tratamento de exceções - Adicionadas duas sobrecargas do método `ToException` na classe `Problems`. - Modificadas as funções `EnsureSuccess` e `EnsureHasValue` para usar `ToException`. * Adição de testes - Novos testes para verificar o comportamento do método `ToException` na classe `ProblemsTests`.
1 parent 90d951e commit 3f38fce

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111
<PropertyGroup>
1212
<SPVer>1.0.0</SPVer>
13-
<SPPreview>-preview-4.3</SPPreview>
13+
<SPPreview>-preview-4.4</SPPreview>
1414
</PropertyGroup>
1515
<PropertyGroup>
1616
<FluentValidationVer>12.0.0</FluentValidationVer>

src/RoyalCode.SmartProblems.Tests/Basics/ProblemsTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,56 @@ public void Problems_CopyTo_Must_ThrowArgumentException_When_LengthIsNotEnough()
282282
// Assert
283283
Assert.Throws<ArgumentException>(act);
284284
}
285+
286+
[Fact]
287+
public void Problems_ToException()
288+
{
289+
// Arrange
290+
Problems problems = [Problems.InvalidParameter("Invalid parameter 1"), Problems.InvalidParameter("Invalid parameter 2")];
291+
292+
// Act
293+
var exception = problems.ToException();
294+
295+
// Assert
296+
Assert.NotNull(exception);
297+
Assert.IsType<InvalidOperationException>(exception);
298+
Assert.Contains("Invalid parameter 1", exception.Message);
299+
Assert.Contains("Invalid parameter 2", exception.Message);
300+
}
301+
302+
[Fact]
303+
public void Problems_ToException_WithMessagePattern()
304+
{
305+
// Arrange
306+
Problems problems = [Problems.InvalidParameter("Invalid parameter 1"), Problems.InvalidParameter("Invalid parameter 2")];
307+
308+
// Act
309+
var exception = problems.ToException("Custom message: {0}");
310+
311+
// Assert
312+
Assert.NotNull(exception);
313+
Assert.IsType<InvalidOperationException>(exception);
314+
Assert.Contains("Custom message:", exception.Message);
315+
Assert.Contains("Invalid parameter 1", exception.Message);
316+
Assert.Contains("Invalid parameter 2", exception.Message);
317+
}
318+
319+
[Fact]
320+
public void Problems_ToException_WithMessagePattern_And_Separator()
321+
{
322+
// Arrange
323+
Problems problems = [Problems.InvalidParameter("Invalid parameter 1"), Problems.InvalidParameter("Invalid parameter 2")];
324+
325+
// Act
326+
var exception = problems.ToException("Custom message:\n - {0}", "\n - ");
327+
328+
// Assert
329+
Assert.NotNull(exception);
330+
Assert.IsType<InvalidOperationException>(exception);
331+
Assert.Contains("Custom message:", exception.Message);
332+
Assert.Contains("\n - Category: InvalidParameter, Details: Invalid parameter 1", exception.Message);
333+
Assert.Contains("\n - Category: InvalidParameter, Details: Invalid parameter 2", exception.Message);
334+
}
285335
}
286336

287337
#region classes

src/RoyalCode.SmartProblems/Problems.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,31 @@ public Problem this[int index]
445445
}
446446

447447
#endregion
448+
449+
/// <summary>
450+
/// Creates an <see cref="InvalidOperationException"/> from the collection of problems.
451+
/// The exception message will contain the details of all problems in the collection.
452+
/// </summary>
453+
/// <returns>A new instance of <see cref="InvalidOperationException"/>.</returns>
454+
public InvalidOperationException ToException()
455+
{
456+
return new InvalidOperationException(string.Join("\n", this.Select(p => p.ToString())));
457+
}
458+
459+
/// <summary>
460+
/// Creates an <see cref="InvalidOperationException"/> from the collection of problems.
461+
/// The exception message will be formatted using the provided pattern,
462+
/// where the first argument is the details of all problems in the collection.
463+
/// </summary>
464+
/// <remarks>
465+
/// The message pattern should contain a single placeholder (e.g., <c>"{0}"</c>, <c>"Errors: {0}"</c>),
466+
/// </remarks>
467+
/// <param name="messagePattern">The message pattern to format the exception message.</param>
468+
/// <param name="separator">The separator to use between problem details in the message, optional, defaults to newline character.</param>
469+
/// <returns>A new instance of <see cref="InvalidOperationException"/>.</returns>
470+
public InvalidOperationException ToException(string messagePattern, string separator = "\n")
471+
{
472+
var message = string.Format(messagePattern, string.Join(separator, this.Select(p => p.ToString())));
473+
return new InvalidOperationException(message);
474+
}
448475
}

src/RoyalCode.SmartProblems/Result'0.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public Problem this[int index]
196196
public void EnsureSuccess()
197197
{
198198
if (IsFailure)
199-
throw new InvalidOperationException(string.Join("\n", problems.Select(p => p.ToString())));
199+
throw problems.ToException();
200200
}
201201

202202
#region Has/Is

src/RoyalCode.SmartProblems/Result'1.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public Problem this[int index]
274274
public void EnsureSuccess()
275275
{
276276
if (IsFailure)
277-
throw new InvalidOperationException(string.Join("\n", problems.Select(p => p.ToString())));
277+
throw problems.ToException();
278278
}
279279

280280
/// <summary>
@@ -288,7 +288,7 @@ public void EnsureSuccess()
288288
public void EnsureHasValue([NotNull] out TValue value)
289289
{
290290
if (IsFailure)
291-
throw new InvalidOperationException(string.Join("\n", problems.Select(p => p.ToString())));
291+
throw problems.ToException();
292292

293293
value = this.value;
294294
}

0 commit comments

Comments
 (0)