Altinn.Authorization.ProblemDetails 3.2.1

dotnet add package Altinn.Authorization.ProblemDetails --version 3.2.1                
NuGet\Install-Package Altinn.Authorization.ProblemDetails -Version 3.2.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Altinn.Authorization.ProblemDetails" Version="3.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Altinn.Authorization.ProblemDetails --version 3.2.1                
#r "nuget: Altinn.Authorization.ProblemDetails, 3.2.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Altinn.Authorization.ProblemDetails as a Cake Addin
#addin nuget:?package=Altinn.Authorization.ProblemDetails&version=3.2.1

// Install Altinn.Authorization.ProblemDetails as a Cake Tool
#tool nuget:?package=Altinn.Authorization.ProblemDetails&version=3.2.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 ProblemDescriptors, which can trivially be converted into ProblemDetails 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 method New() to create a new instance of ProblemDescriptorFactory.

  • Create(): This method is used to create a new ProblemDescriptor object with a custom error code, HTTP status code, and error message. These can then be turned into ProblemDetails objects by calling ToProblemDetails().

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 ProblemDescriptors.

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.2.1 70 12/19/2024
3.2.0 100 12/17/2024
3.1.0 301 12/6/2024
3.0.1 5,700 8/27/2024
3.0.0 1,096 7/10/2024
2.0.0 531 6/11/2024
1.1.1 106 6/11/2024
1.1.0 104 6/11/2024
1.0.0 378 5/24/2024

# Changelog

## [3.2.1](https://github.com/Altinn/altinn-authorization-utils/compare/Altinn.Authorization.ProblemDetails-v3.2.0...Altinn.Authorization.ProblemDetails-v3.2.1) (2024-12-19)


### Bug Fixes

* add ProblemInstanceException constructor overloads ([#175](https://github.com/Altinn/altinn-authorization-utils/issues/175)) ([95b8a0b](https://github.com/Altinn/altinn-authorization-utils/commit/95b8a0bbfd8a01d3ca72b4239fce63d040f30eb5))

## [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))