-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create Altinn.Authorization.ProblemDetails.Abstractions
- Loading branch information
Showing
25 changed files
with
1,002 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...s/src/ProblemDetails.Abstractions/Altinn.Authorization.ProblemDetails.Abstractions.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<RootNamespace>Altinn.Authorization.ProblemDetails</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<InternalsVisibleTo Include="Altinn.Authorization.ProblemDetails" /> | ||
<InternalsVisibleTo Include="Altinn.Authorization.ProblemDetails.Tests" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CommunityToolkit.Diagnostics" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/Altinn.Authorization.ProblemDetails/src/ProblemDetails.Abstractions/ProblemDescriptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using CommunityToolkit.Diagnostics; | ||
using System.Net; | ||
|
||
namespace Altinn.Authorization.ProblemDetails; | ||
|
||
/// <summary> | ||
/// An immutable descriptor for a problem. | ||
/// </summary> | ||
public sealed class ProblemDescriptor | ||
{ | ||
/// <summary> | ||
/// Gets the error code. | ||
/// </summary> | ||
public ErrorCode ErrorCode { get; } | ||
|
||
/// <summary> | ||
/// Gets the status code. | ||
/// </summary> | ||
public HttpStatusCode StatusCode { get; } | ||
|
||
/// <summary> | ||
/// Gets the error details. | ||
/// </summary> | ||
public string Detail { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ProblemDescriptor"/> class. | ||
/// </summary> | ||
/// <param name="errorCode">The error code.</param> | ||
/// <param name="statusCode">The HTTP status code.</param> | ||
/// <param name="detail">The error description.</param> | ||
internal ProblemDescriptor(ErrorCode errorCode, HttpStatusCode statusCode, string detail) | ||
{ | ||
Guard.IsNotDefault(errorCode); | ||
Guard.IsNotNullOrWhiteSpace(detail); | ||
|
||
ErrorCode = errorCode; | ||
StatusCode = statusCode; | ||
Detail = detail; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
....Authorization.ProblemDetails/src/ProblemDetails.Abstractions/ProblemDescriptorFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Net; | ||
|
||
namespace Altinn.Authorization.ProblemDetails; | ||
|
||
/// <summary> | ||
/// A factory for creating <see cref="ProblemDescriptor"/>s. | ||
/// </summary> | ||
public sealed class ProblemDescriptorFactory | ||
{ | ||
/// <summary> | ||
/// Creates a new <see cref="ProblemDescriptorFactory"/> for a given domain name. | ||
/// </summary> | ||
/// <param name="domainName">The domain name.</param> | ||
/// <returns>A <see cref="ProblemDescriptorFactory"/>.</returns> | ||
/// <remarks>Domain names must be 2-4 letter ASCII uppercase.</remarks> | ||
public static ProblemDescriptorFactory New(string domainName) | ||
=> new(ErrorCodeDomain.Get(domainName)); | ||
|
||
private readonly ErrorCodeDomain _domain; | ||
|
||
private ProblemDescriptorFactory(ErrorCodeDomain domain) | ||
{ | ||
_domain = domain; | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="ProblemDescriptor"/>. | ||
/// </summary> | ||
/// <param name="code">The (domain specific) error code.</param> | ||
/// <param name="statusCode">The <see cref="HttpStatusCode"/> for the error.</param> | ||
/// <param name="detail">The error details (message).</param> | ||
/// <returns>A newly created <see cref="ProblemDescriptor"/>.</returns> | ||
public ProblemDescriptor Create(uint code, HttpStatusCode statusCode, string detail) | ||
=> new ProblemDescriptor(_domain.Code(code), statusCode, detail); | ||
} |
24 changes: 24 additions & 0 deletions
24
...inn.Authorization.ProblemDetails/src/ProblemDetails.Abstractions/StdProblemDescriptors.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Net; | ||
|
||
namespace Altinn.Authorization.ProblemDetails; | ||
|
||
/// <summary> | ||
/// Standard problem descriptors. | ||
/// </summary> | ||
public static class StdProblemDescriptors | ||
{ | ||
internal const string DOMAIN_NAME = "STD"; | ||
|
||
private static readonly ProblemDescriptorFactory _factory | ||
= ProblemDescriptorFactory.New(DOMAIN_NAME); | ||
|
||
/// <summary> | ||
/// Gets a problem descriptor for a validation error. | ||
/// </summary> | ||
/// <remarks> | ||
/// This property should remain internal to avoid direct use. To create a validation error | ||
/// use the AltinnValidationProblemDetails class from the Altinn.Authorization.ProblemDetails project. | ||
/// </remarks> | ||
internal static ProblemDescriptor ValidationError { get; } | ||
= _factory.Create(0, HttpStatusCode.BadRequest, "One or more validation errors occurred."); | ||
} |
16 changes: 16 additions & 0 deletions
16
...ltinn.Authorization.ProblemDetails/src/ProblemDetails.Abstractions/StdValidationErrors.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Altinn.Authorization.ProblemDetails; | ||
|
||
/// <summary> | ||
/// Standard validation errors. | ||
/// </summary> | ||
public static class StdValidationErrors | ||
{ | ||
private static readonly ValidationErrorDescriptorFactory _factory | ||
= ValidationErrorDescriptorFactory.New(StdProblemDescriptors.DOMAIN_NAME); | ||
|
||
/// <summary> | ||
/// Gets a validation error descriptor for a required field missing. | ||
/// </summary> | ||
public static ValidationErrorDescriptor Required { get; } | ||
= _factory.Create(0, "The field is required."); | ||
} |
33 changes: 33 additions & 0 deletions
33
...Authorization.ProblemDetails/src/ProblemDetails.Abstractions/ValidationErrorDescriptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using CommunityToolkit.Diagnostics; | ||
|
||
namespace Altinn.Authorization.ProblemDetails; | ||
|
||
/// <summary> | ||
/// An immutable descriptor for a validation error. | ||
/// </summary> | ||
public sealed class ValidationErrorDescriptor | ||
{ | ||
/// <summary> | ||
/// Gets the error code. | ||
/// </summary> | ||
public ErrorCode ErrorCode { get; } | ||
|
||
/// <summary> | ||
/// Gets the error details. | ||
/// </summary> | ||
public string Detail { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ValidationErrorDescriptor"/> class. | ||
/// </summary> | ||
/// <param name="errorCode">The error code.</param> | ||
/// <param name="detail">The error description.</param> | ||
internal ValidationErrorDescriptor(ErrorCode errorCode, string detail) | ||
{ | ||
Guard.IsNotDefault(errorCode); | ||
Guard.IsNotNullOrWhiteSpace(detail); | ||
|
||
ErrorCode = errorCode; | ||
Detail = detail; | ||
} | ||
} |
Oops, something went wrong.