Net.Utils.ErrorHandler
1.0.1
See the version list below for details.
dotnet add package Net.Utils.ErrorHandler --version 1.0.1
NuGet\Install-Package Net.Utils.ErrorHandler -Version 1.0.1
<PackageReference Include="Net.Utils.ErrorHandler" Version="1.0.1" />
paket add Net.Utils.ErrorHandler --version 1.0.1
#r "nuget: Net.Utils.ErrorHandler, 1.0.1"
// Install Net.Utils.ErrorHandler as a Cake Addin #addin nuget:?package=Net.Utils.ErrorHandler&version=1.0.1 // Install Net.Utils.ErrorHandler as a Cake Tool #tool nuget:?package=Net.Utils.ErrorHandler&version=1.0.1
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 withFluentValidation
. - 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 | Versions 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. |
-
.NETStandard 2.1
- FluentValidation (>= 11.11.0)
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.