ErrorLib.Core
1.0.1
dotnet add package ErrorLib.Core --version 1.0.1
NuGet\Install-Package ErrorLib.Core -Version 1.0.1
<PackageReference Include="ErrorLib.Core" Version="1.0.1" />
<PackageVersion Include="ErrorLib.Core" Version="1.0.1" />
<PackageReference Include="ErrorLib.Core" />
paket add ErrorLib.Core --version 1.0.1
#r "nuget: ErrorLib.Core, 1.0.1"
#:package ErrorLib.Core@1.0.1
#addin nuget:?package=ErrorLib.Core&version=1.0.1
#tool nuget:?package=ErrorLib.Core&version=1.0.1
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. - π§
IExceptionMapperto map exceptions to messages, codes, HTTP statuses - π Logging abstraction via
ILogger, compatible with built-in or custom loggers - π«
TryCatchWrapper.Try()andTryAsync()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 | Versions 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. |
-
net8.0
- FluentValidation (>= 12.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
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.