ErrorLib.Core 1.0.1

dotnet add package ErrorLib.Core --version 1.0.1
                    
NuGet\Install-Package ErrorLib.Core -Version 1.0.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="ErrorLib.Core" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ErrorLib.Core" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="ErrorLib.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ErrorLib.Core --version 1.0.1
                    
#r "nuget: ErrorLib.Core, 1.0.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.
#:package ErrorLib.Core@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ErrorLib.Core&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=ErrorLib.Core&version=1.0.1
                    
Install as a Cake Tool

ErrorLib.Core

πŸ”§ Portable, extensible, and fluent error/result handling for modern .NET applications.

ErrorLib is a clean, developer-friendly result and exception handling library for .NET and ASP.NET Core. It provides a structured Result<T> pattern, centralized exception mapping, logging abstraction, and out-of-the-box middleware integration for Web APIs.


✨ Features

  • βœ… Strongly typed Result, DataResult<T>, SuccessResult, ErrorResult, ErrorDataResult<T>
  • πŸ”„ Fluent chaining: .WithStatusCode(), .WithErrorCode(), .WithValidationErrors()
  • πŸ”— Conditional logic: .IfSuccess(), .IfError(), .IfStatusCode() etc.
  • 🧠 IExceptionMapper to map exceptions to messages, codes, HTTP statuses
  • πŸ”Œ Logging abstraction via ILogger, compatible with built-in or custom loggers
  • 🚫 TryCatchWrapper.Try() and TryAsync() for safe execution
  • 🌐 ASP.NET Core middleware support via ErrorLib.AspNetCore

πŸš€ Basic Usage βœ… TryCatchWrapper (Sync)

var result = TryCatchWrapper.Try(() β‡’ { // your business logic return new UserDto(); });

if (!result.IsSuccess) Console.WriteLine(result.Message); βœ… TryCatchWrapper (Async)

var result = await TryCatchWrapper.TryAsync(async () β‡’ { return await _userService.GetUserAsync(1); });

if (!result.IsSuccess) logger.LogError(result.Message); βœ… ResultFactory

return ResultFactory.Success("Operation completed"); return ResultFactory.Success(userDto); return ResultFactory.Error("User not found", 404, "USER_NOT_FOUND"); βœ… Validation Error Example

return ResultFactory.ValidationError<UserDto>( new List<string> { "Name is required", "Email must be valid" } ); βœ… Fluent Result Extensions

return new ErrorResult("Invalid input") .WithStatusCode<ErrorResult>(400) .WithErrorCode<ErrorResult>("VALIDATION") .WithTraceId<ErrorResult>("abc-123");

result .IfError(() β‡’ logger.LogError("❌ Error occurred")) .IfSuccess(() β‡’ logger.LogInfo("βœ… All good")) .IfStatusCode(404, () β‡’ logger.LogWarning("πŸ” Not Found")); βš™οΈ Exception Mapping Customize exception handling globally:

public class MyExceptionMapper : IExceptionMapper { public int MapStatusCode(Exception ex) β‡’ ex switch { NotFoundException β‡’ 404, BusinessException β‡’ 409, ValidationException β‡’ 400, _ β‡’ 500 };

public string MapMessage(Exception ex) =>
    ex.Message;

public string MapErrorCode(Exception ex) =>
    "GENERIC_ERROR";

} Register in DI:

services.AddSingleton<IExceptionMapper, MyExceptionMapper>(); πŸ”Œ Logging Abstraction ConsoleLogger

ILogger logger = new ConsoleLogger(); logger.LogError("Something went wrong", ex); MicrosoftLoggerAdapter csharp Kopyala Düzenle var adapter = new MicrosoftLoggerAdapter( app.Services.GetRequiredService<Microsoft.Extensions.Logging.ILogger>() ); 🌐 ASP.NET Core Middleware (ErrorLib.AspNetCore) Enable global exception handling in Program.cs:

app.UseCustomExceptionHandler(); Register services:

builder.Services.AddSingleton<ILogger, ConsoleLogger>(); // or MicrosoftLoggerAdapter builder.Services.AddSingleton<IExceptionMapper, DefaultExceptionMapper>(); Automatically catches all unhandled exceptions in HTTP pipeline and returns standard JSON error responses.

πŸ§ͺ Supported Platforms βœ… .NET 6 / .NET 7 / .NET 8

βœ… ASP.NET Core Web API

βœ… Console apps, Workers, Background jobs

πŸ“¦ Installation

ErrorLib.Core

dotnet add package ErrorLib.Core
dotnet add package ErrorLib.AspNetCore
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 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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 ErrorLib.Core:

Package Downloads
ErrorLib.AspNetCore

Robust global exception handling middleware for ASP.NET Core built on top of ErrorLib.Core.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 193 5/9/2025
1.0.0 161 5/9/2025