Pandatech.ResponseCrafter 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Pandatech.ResponseCrafter --version 2.0.0                
NuGet\Install-Package Pandatech.ResponseCrafter -Version 2.0.0                
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="Pandatech.ResponseCrafter" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pandatech.ResponseCrafter --version 2.0.0                
#r "nuget: Pandatech.ResponseCrafter, 2.0.0"                
#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 Pandatech.ResponseCrafter as a Cake Addin
#addin nuget:?package=Pandatech.ResponseCrafter&version=2.0.0

// Install Pandatech.ResponseCrafter as a Cake Tool
#tool nuget:?package=Pandatech.ResponseCrafter&version=2.0.0                

Pandatech.ResponseCrafter

Introduction

Pandatech.ResponseCrafter is a comprehensive NuGet package for .NET 8+, specifically designed to enhance exception handling and logging in ASP.NET Core applications. This package simplifies managing standard and custom exceptions by crafting detailed error responses suitable for both development and production environments.

Features

  • Custom Exception Handling: Streamlines the process of managing both standard HTTP exceptions and custom exceptions.
  • Detailed Error Responses: Generates verbose error messages, including stack traces for in-depth debugging in development environments.
  • Environment-Sensitive Logging: Provides flexible logging and response behavior based on visibility settings (Public or Private):
    • Private: All exceptions are sent to the client as defined, and 4xx errors are logged as warnings while 5xx errors are logged as errors.
    • Public: 4xx exceptions are sent to the client as defined, while 5xx errors are concealed with a generic message. Logging remains the same as in Private.
  • Frontend-Friendly Error Messages: Supports converting error messages to your desired case convention, facilitating easier integration with frontend localization systems.
  • Standardized Error Responses: Provides a standardized error response format, making it easier for frontend applications to parse and display error messages. The error response format is shown below:
{
  "TraceId": "0HMVFE0A284AM:00000001",
  "Instance": "POST - 164.54.144.23:443/users/register",
  "StatusCode": 400,
  "Type": "BadRequestException",
  "Errors": {
    "email": "email_address_is_not_in_a_valid_format",
    "password": "password_must_be_at_least_8_characters_long"
  },
  "Message": "the_request_was_invalid_or_cannot_be_otherwise_served."
}

Installation

Install the package via NuGet Package Manager or use the following command:

Install-Package ResponseCrafter

Usage

1. Setup Exception Handlers:

Add AddResponseCrafter in program.cs bby providing an optional naming convention, and configure ResponseCrafterVisibility in your settings.

var builder = WebApplication.CreateBuilder(args);

// Basic setup
builder.AddResponseCrafter();

// Setup with a specific naming convention
builder.AddResponseCrafter(NamingConvention.ToSnakeCase);

var app = builder.Build();
app.UseResponseCrafter();
app.Run();

Configure visibility in your appsettings.json:

{
  "ResponseCrafterVisibility": "Public"
}

Supported naming conventions:

public enum NamingConvention
{
    Default = 0,
    ToSnakeCase = 1,
    ToPascalCase = 2,
    ToCamelCase = 3,
    ToKebabCase = 4,
    ToTitleCase = 5,
    ToHumanCase = 6
}

2. Define Custom Exceptions:

Create custom exception classes that inherit from ApiException or use the predefined ones. Use ErrorDetails records for specific error messages related to API requests.

3. Configure Middleware:

  • Implement the exception handling middleware in your application's pipeline.
app.UseResponseCrafter();

4. Logging and Error Responses:

The package automatically logs warnings or errors and provides crafted responses based on the exception type.

Custom HTTP Exception Already Created

  • BadRequestException
  • UnauthorizedException
  • PaymentRequiredException
  • ForbiddenException
  • NotFoundException
  • ConflictException
  • TooManyRequestsException
  • InternalServerErrorException
  • ServiceUnavailableException

Recommendations

  • Error Message Formatting: It's recommended to use snake_case for error messages to aid frontend applications in implementing localization.

Limitations

  • This package is specifically tailored for .NET 8 and above.

License

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

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Pandatech.ResponseCrafter:

Package Downloads
Pandatech.SharedKernel

Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.1 34 11/21/2024
5.0.0 34 11/21/2024
4.0.1 97 11/11/2024
4.0.0 98 10/4/2024
3.0.1 128 9/11/2024
3.0.0 109 7/18/2024
2.2.2 115 6/29/2024
2.2.1 114 6/29/2024
2.2.0 117 6/27/2024
2.1.4 117 6/27/2024
2.1.3 148 6/22/2024
2.1.2 93 6/22/2024
2.1.1 99 6/22/2024
2.1.0 99 6/22/2024
2.0.0 110 6/13/2024
1.7.0 93 6/13/2024
1.6.3 101 6/9/2024
1.6.2 107 6/6/2024
1.6.1 125 6/2/2024
1.5.1 115 5/28/2024
1.5.0 114 5/26/2024
1.4.14 118 5/24/2024
1.4.13 100 5/24/2024
1.4.12 111 5/17/2024
1.4.10 125 5/8/2024
1.4.9 132 5/7/2024
1.4.8 111 5/6/2024
1.4.7 123 5/5/2024
1.4.6 84 5/3/2024
1.4.5 121 4/24/2024
1.4.4 116 4/24/2024
1.4.3 112 4/24/2024
1.4.2 110 4/23/2024
1.4.1 131 4/16/2024
1.4.0 122 4/16/2024
1.3.0 111 4/16/2024
1.2.1 143 4/3/2024
1.2.0 147 4/2/2024
1.1.0 176 3/22/2024
1.0.4 325 12/14/2023
1.0.3 158 11/29/2023
1.0.2 146 11/29/2023
1.0.1 136 11/28/2023
1.0.0 155 11/28/2023

Dropped support for EfCoreMagicQuery library