Altinn.Authorization.ProblemDetails.Abstractions
3.2.0
See the version list below for details.
dotnet add package Altinn.Authorization.ProblemDetails.Abstractions --version 3.2.0
NuGet\Install-Package Altinn.Authorization.ProblemDetails.Abstractions -Version 3.2.0
<PackageReference Include="Altinn.Authorization.ProblemDetails.Abstractions" Version="3.2.0" />
paket add Altinn.Authorization.ProblemDetails.Abstractions --version 3.2.0
#r "nuget: Altinn.Authorization.ProblemDetails.Abstractions, 3.2.0"
// Install Altinn.Authorization.ProblemDetails.Abstractions as a Cake Addin #addin nuget:?package=Altinn.Authorization.ProblemDetails.Abstractions&version=3.2.0 // Install Altinn.Authorization.ProblemDetails.Abstractions as a Cake Tool #tool nuget:?package=Altinn.Authorization.ProblemDetails.Abstractions&version=3.2.0
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. net9.0 is compatible. |
-
net8.0
- CommunityToolkit.Diagnostics (>= 8.4.0)
-
net9.0
- CommunityToolkit.Diagnostics (>= 8.4.0)
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
## [3.2.0](https://github.com/Altinn/altinn-authorization-utils/compare/Altinn.Authorization.ProblemDetails-v3.1.0...Altinn.Authorization.ProblemDetails-v3.2.0) (2024-12-17)
### Features
* add `ProblemInstanceException` ([#172](https://github.com/Altinn/altinn-authorization-utils/issues/172)) ([7bed62f](https://github.com/Altinn/altinn-authorization-utils/commit/7bed62fdf844d534883286ba73c3095714d079b9))
## [3.1.0](https://github.com/Altinn/altinn-authorization-utils/compare/Altinn.Authorization.ProblemDetails-v3.0.1...Altinn.Authorization.ProblemDetails-v3.1.0) (2024-12-06)
### Features
* upgrade to .NET 9 ([#166](https://github.com/Altinn/altinn-authorization-utils/issues/166)) ([867c940](https://github.com/Altinn/altinn-authorization-utils/commit/867c9400ac8fd9a37c71d0af6386fbb414523267))
## [3.0.1](https://github.com/Altinn/altinn-authorization-utils/compare/Altinn.Authorization.ProblemDetails-v3.0.0...Altinn.Authorization.ProblemDetails-v3.0.1) (2024-08-27)
### Bug Fixes
* add documentation for problem-details ([#128](https://github.com/Altinn/altinn-authorization-utils/issues/128)) ([9eca254](https://github.com/Altinn/altinn-authorization-utils/commit/9eca2540b7d234327d2806fbadb15928000ebf2e))
## [3.0.0](https://github.com/Altinn/altinn-authorization-utils/compare/Altinn.Authorization.ProblemDetails-v2.0.0...Altinn.Authorization.ProblemDetails-v3.0.0) (2024-07-10)
### ⚠ BREAKING CHANGES
* The ValidationErrorBuilder class has been moved to the ProblemDetails.Abstractions assembly.
* The various Extensions properties in ProblemDetails.Abstractions have been changed from being a immutable array of key-value pairs, to a new ProblemExtensionData type.
### Features
* add ProblemExtensionData to ProblemDetails.Abstractions ([91c428a](https://github.com/Altinn/altinn-authorization-utils/commit/91c428adcd8341c9096b6b015fe65d118dcc55bf))
* add Result to ProblemDetails.Abstractions ([#81](https://github.com/Altinn/altinn-authorization-utils/issues/81)) ([91c428a](https://github.com/Altinn/altinn-authorization-utils/commit/91c428adcd8341c9096b6b015fe65d118dcc55bf))
* create ValidationProblemInstance in ProblemDetails.Abstractions. ([91c428a](https://github.com/Altinn/altinn-authorization-utils/commit/91c428adcd8341c9096b6b015fe65d118dcc55bf))
### Code Refactoring
* move ValidationErrorBuilder to ProblemDetails.Abstractions ([91c428a](https://github.com/Altinn/altinn-authorization-utils/commit/91c428adcd8341c9096b6b015fe65d118dcc55bf))
## [2.0.0](https://github.com/Altinn/altinn-authorization-utils/compare/Altinn.Authorization.ProblemDetails-v1.1.1...Altinn.Authorization.ProblemDetails-v2.0.0) (2024-06-11)
### ⚠ BREAKING CHANGES
* Multiple overloads for ProblemDetails and related types have been removed to avoid ambiguity. ValidationErrors has also been renamed to ValidationErrorBuilder and moved from abstractions to the ProblemDetails package.
### Bug Fixes
* Resolve ambigous method overloads ([4620f64](https://github.com/Altinn/altinn-authorization-utils/commit/4620f64555252fddca3c165269de33166eb35c9b))
## [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))