Skip to content

Commit

Permalink
feat: add ProblemInstanceException
Browse files Browse the repository at this point in the history
Also add `EnsureSuccess` to `Result<T>`.
  • Loading branch information
Alxandr committed Dec 17, 2024
1 parent 1559064 commit 597fe9c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Altinn.Authorization.ProblemDetails;

/// <summary>
/// An exception that represents a <see cref="ProblemInstance"/>.
/// </summary>
public class ProblemInstanceException
: InvalidOperationException
{
/// <summary>
/// Initializes a new instance of the <see cref="ProblemInstanceException"/> class.
/// </summary>
/// <param name="problemInstance">The <see cref="ProblemInstance"/>.</param>
public ProblemInstanceException(ProblemInstance problemInstance)
: base(problemInstance.Detail)
{
ProblemInstance = problemInstance;
}

/// <summary>
/// Gets the <see cref="ProblemInstance"/>.
/// </summary>
public ProblemInstance ProblemInstance { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ public bool IsSuccess
get => _problem is null;
}

/// <summary>
/// Ensures that the operation succeeded.
/// </summary>
/// <exception cref="ProblemInstanceException">Thrown if the operation failed.</exception>
[MemberNotNull(nameof(Value))]
public void EnsureSuccess()
{
if (!IsSuccess)
{
throw new ProblemInstanceException(_problem!);
}
}

/// <summary>
/// Gets the problem instance if the operation failed, otherwise <see langword="null"/>.
/// </summary>
Expand Down

0 comments on commit 597fe9c

Please sign in to comment.