SafeWrap 8.1.0
dotnet add package SafeWrap --version 8.1.0
NuGet\Install-Package SafeWrap -Version 8.1.0
<PackageReference Include="SafeWrap" Version="8.1.0" />
<PackageVersion Include="SafeWrap" Version="8.1.0" />
<PackageReference Include="SafeWrap" />
paket add SafeWrap --version 8.1.0
#r "nuget: SafeWrap, 8.1.0"
#:package SafeWrap@8.1.0
#addin nuget:?package=SafeWrap&version=8.1.0
#tool nuget:?package=SafeWrap&version=8.1.0
SafeWrap
SafeWrap is a lightweight exception handling library for ASP.NET Core applications that provides a clean and consistent way to handle exceptions and convert them to appropriate HTTP responses.
Features
- 🛡️ Type-safe exception handling wrapper
- 🎯 Automatic HTTP status code mapping
- 🔄 Support for both synchronous and asynchronous operations
- 📦 Consistent error response format
- 🔌 Implicit conversion to ActionResult
- 🚀 Zero-configuration defaults with customization options
Installation
Package Manager Console
Install-Package SafeWrap
.NET CLI
dotnet add package SafeWrap
Package Reference
<PackageReference Include="SafeWrap" Version="8.1.0" />
Usage
Basic Exception Handling
public ActionResult GetData() =>
new SafeWrap<IEnumerable<DataItem>>().Execute( () =>
{
// Your code that might throw exceptions
return _service.GetItems();
},
(typeof(InvalidOperationException), StatusCodes.Status400BadRequest)
);
Async Operations
public async Task<ActionResult> GetDataAsync() =>
await new SafeWrap<IEnumerable<DataItem>>().ExecuteAsync( async () =>
{
// Your async code
return await _service.GetItemsAsync();
},
(typeof(InvalidOperationException), StatusCodes.Status400BadRequest)
);
Using Implicit Operator
public ActionResult GetData() =>
new SafeWrap<IEnumerable<DataItem>>( () =>
_service.GetItems(),
(typeof(InvalidOperationException), StatusCodes.Status400BadRequest)
);
Error Response Format
When an exception occurs, SafeWrap returns a consistent error response:
{ "message": "The error message", "errorType": "ExceptionTypeName" }
Advanced Example
[HttpGet("data")]
[ProducesResponseType(typeof(IEnumerable<DataItem>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(SafeWrappedResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(SafeWrappedResponse), StatusCodes.Status408RequestTimeout)]
[ProducesResponseType(typeof(SafeWrappedResponse), StatusCodes.Status500InternalServerError)]
public async Task<ActionResult> GetDataAsync() =>
await new SafeWrap<IEnumerable<DataItem>>().ExecuteAsync( async () =>
{
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
return await _service.GetItemsAsync(cts.Token);
},
(typeof(TimeoutException), StatusCodes.Status408RequestTimeout),
(typeof(ArgumentException), StatusCodes.Status400BadRequest),
(typeof(InvalidOperationException), StatusCodes.Status400BadRequest)
);
Exception Mapping
SafeWrap maps exceptions to HTTP status codes based on the configuration provided:
- Configured exceptions are mapped to their specified status codes
- Uncaught exceptions default to 500 Internal Server Error
- Multiple exception types can be mapped to the same status code
Best Practices
- Always specify expected exceptions and their status codes
- Use appropriate HTTP status codes for different error scenarios
- Keep error messages user-friendly but informative
- Use async methods for async operations
- Consider adding timeout handling for long-running operations
License
MIT License. See LICENSE file for details.
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
- Microsoft.AspNetCore (>= 2.3.0)
- Microsoft.AspNetCore.Mvc (>= 2.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
8.1.0 | 122 | 7/13/2025 |