Altinn.Authorization.ProblemDetails.Abstractions
1.1.1
See the version list below for details.
dotnet add package Altinn.Authorization.ProblemDetails.Abstractions --version 1.1.1
NuGet\Install-Package Altinn.Authorization.ProblemDetails.Abstractions -Version 1.1.1
<PackageReference Include="Altinn.Authorization.ProblemDetails.Abstractions" Version="1.1.1" />
paket add Altinn.Authorization.ProblemDetails.Abstractions --version 1.1.1
#r "nuget: Altinn.Authorization.ProblemDetails.Abstractions, 1.1.1"
// Install Altinn.Authorization.ProblemDetails.Abstractions as a Cake Addin #addin nuget:?package=Altinn.Authorization.ProblemDetails.Abstractions&version=1.1.1 // Install Altinn.Authorization.ProblemDetails.Abstractions as a Cake Tool #tool nuget:?package=Altinn.Authorization.ProblemDetails.Abstractions&version=1.1.1
Altinn.Authorization.ProblemDetails Library
The Altinn.Authorization.ProblemDetails
library provides a convenient way to generate ProblemDetails
objects with custom
error codes. These ProblemDetails
objects can be used to represent errors in ASP.NET Core applications following the
RFC 7807 specification.
Installation
You can install the library via NuGet Package Manager Console:
Install-Package Altinn.Authorization.ProblemDetails
Or via the .NET CLI:
dotnet add package Altinn.Authorization.ProblemDetails
Usage
ProblemDetails
This library allows for defining custom errors that contain error codes usable by clients to determine what went wrong. This is done by creating custom ProblemDescriptor
s, which can trivially be converted into ProblemDetail
s by calling ToProblemDetails()
on them.
ProblemDetails Example
Here's a basic example demonstrating how to use the Altinn.Authorization.ProblemDetails
library:
internal static class MyAppErrors
{
private static readonly ProblemDescriptorFactory _factory
= ProblemDescriptorFactory.New("APP");
public static ProblemDescriptor BadRequest { get; }
= _factory.Create(0, HttpStatusCode.BadRequest, "Bad request");
public static ProblemDescriptor NotFound { get; }
= _factory.Create(1, HttpStatusCode.NotFound, "Not found");
public static ProblemDescriptor InternalServerError { get; }
= _factory.Create(2, HttpStatusCode.InternalServerError, "Internal server error");
public static ProblemDescriptor NotImplemented { get; }
= _factory.Create(3, HttpStatusCode.NotImplemented, "Not implemented");
}
Explanation
ProblemDescriptorFactory
: This class provides a factory methodNew()
to create a new instance ofProblemDescriptorFactory
.Create()
: This method is used to create a newProblemDescriptor
object with a custom error code, HTTP status code, and error message. These can then be turned intoProblemDetails
objects by callingToProblemDetails()
.
Validation Errors
A predefined AltinnValidationProblemDetails
is provided for the case where you have one or more validation errors that should be returned to the client. This variant of ProblemDetails
takes a list of validation errors, which can be created in a similar fasion to ProblemDescriptor
s.
ValidationErrors Example
Here's a basic example demonstrating how to create custom validation errors:
internal static class MyAppValidationDescriptors
{
private static readonly ValidationErrorDescriptorFactory _factory
= ValidationErrorDescriptorFactory.New("APP");
public static ValidationErrorDescriptor FieldRequired { get; }
= _factory.Create(0, "Field is required.");
public static ValidationErrorDescriptor FieldOutOfRange { get; }
= _factory.Create(1, "Field is out of range.");
public static ValidationErrorDescriptor PasswordsMustMatch { get; }
= _factory.Create(2, "Passwords must match.");
}
And how to use them:
var details = new AltinnValidationProblemDetails([
MyAppValidationDescriptors.FieldRequired.ToValidationError("/field1"),
MyAppValidationDescriptors.FieldRequired.ToValidationError("/field2"),
MyAppValidationDescriptors.PasswordsMustMatch.ToValidationError(["/password", "/confirmPassword"]),
]);
A set of common validation errors are also provided through the StdValidationErrors
class.
Customization
You can customize the prefix used for error codes by passing a custom prefix to the New()
method of ProblemDescriptorFactory
. All application-domains should have their own prefix.
ProblemDescriptorFactory.New("PFX");
The prefix is required to be only uppercase ASCII letters of either 2, 3, or 4 characters in length.
Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on GitHub.
License
This library is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- CommunityToolkit.Diagnostics (>= 8.2.2)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Altinn.Authorization.ProblemDetails.Abstractions:
Package | Downloads |
---|---|
Altinn.Authorization.ProblemDetails
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
# Changelog
## [1.1.1](https://github.com/Altinn/altinn-authorization-utils/compare/Altinn.Authorization.ProblemDetails-v1.1.0...Altinn.Authorization.ProblemDetails-v1.1.1) (2024-06-11)
### Bug Fixes
* doc-comment issue ([433fa54](https://github.com/Altinn/altinn-authorization-utils/commit/433fa548c4da6d356ed128a5c3216a3766ddf686))
## [1.1.0](https://github.com/Altinn/altinn-authorization-utils/compare/Altinn.Authorization.ProblemDetails-v1.0.0...Altinn.Authorization.ProblemDetails-v1.1.0) (2024-06-11)
### Features
* improve ProblemDetails ([9115fc2](https://github.com/Altinn/altinn-authorization-utils/commit/9115fc2994f61bc6d2ded09d874fb48cfdbe1b6a))
### Bug Fixes
* create documentation files ([171dd71](https://github.com/Altinn/altinn-authorization-utils/commit/171dd7120ab70c8c5629224e6e7a2380ad827306))
## 1.0.0 (2024-05-24)
### Features
* create Altinn.Authorization.ProblemDetails ([b544e57](https://github.com/Altinn/altinn-authorization-utils/commit/b544e57b6bec5d81c36bd693e73082c3ea11eec2))
* create Altinn.Authorization.ProblemDetails.Abstractions ([7ff8f2e](https://github.com/Altinn/altinn-authorization-utils/commit/7ff8f2e20dd563bf01c0e11456ee36122f9de539))