Net.Utils.ErrorHandler 1.0.2

dotnet add package Net.Utils.ErrorHandler --version 1.0.2                
NuGet\Install-Package Net.Utils.ErrorHandler -Version 1.0.2                
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="Net.Utils.ErrorHandler" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Net.Utils.ErrorHandler --version 1.0.2                
#r "nuget: Net.Utils.ErrorHandler, 1.0.2"                
#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 Net.Utils.ErrorHandler as a Cake Addin
#addin nuget:?package=Net.Utils.ErrorHandler&version=1.0.2

// Install Net.Utils.ErrorHandler as a Cake Tool
#tool nuget:?package=Net.Utils.ErrorHandler&version=1.0.2                

Net.Utils.ErrorHandler

A C# library for managing error handling through enums and custom attributes. This package simplifies error management by allowing you to define errors as enum members decorated with a custom attribute and then convert them into FluentValidation exceptions.


Installation

.NET CLI

dotnet add package Net.Utils.ErrorHandler

Package Manager

Install-Package Net.Utils.ErrorHandler

Features

  • Enum-Based Error Definitions: Define all your error codes in a single enum and associate each member with a specific error message using the [Error] attribute.
  • FluentValidation Integration: Convert enum errors directly into a ValidationException, which is useful when working with FluentValidation.
  • Customizable Error Information: Attach custom state and property names to errors when converting them, providing additional context for error handling.

Installation

Install the package via the .NET CLI:

dotnet add package Net.Utils.ErrorHandler

Or via the Package Manager:

Install-Package Net.Utils.ErrorHandler

Usage

1. Define Your Error Enum

Decorate each enum member with the [Error] attribute to specify the corresponding error message.

using Net.Utils.ErrorHandler.Attributes;

public enum Error
{
    [Error("Internal server error occurred.")]
    InternalServerError,

    [Error("The requested resource was not found.")]
    NotFound,

    // Note: Enum members without the Error attribute will throw an exception when used.
    UndefinedError
}

2. Convert Enum Errors to Exceptions

Use the extension methods provided in the Net.Utils.ErrorHandler.Extensions namespace to convert your enum errors into a FluentValidation ValidationException.

using FluentValidation;
using Net.Utils.ErrorHandler.Extensions;

public class SomeService
{
    public void Process()
    {
        // Throw an exception for a simple error
        throw Error.NotFound.ToException();

        // Throw an exception with additional custom state and property name
        var customData = new { Detail = "Additional error information." };
        throw Error.InternalServerError.ToException(customData, "SomeProperty");
    }
}

3. Retrieve Error Codes and Messages

You can also retrieve the error code (the enum member's name) and the associated error message:

using Net.Utils.ErrorHandler.Extensions;

var errorCode = Error.NotFound.ToErrorCode();       // Returns "NotFound"
var errorMessage = Error.NotFound.ToErrorMessage();   // Returns "The requested resource was not found."
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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 Net.Utils.ErrorHandler:

Package Downloads
TokenSchedule.FluentValidation

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 34 2/14/2025
1.0.1 106 2/11/2025
1.0.0 79 2/7/2025