SafeWrap 8.1.0

dotnet add package SafeWrap --version 8.1.0
                    
NuGet\Install-Package SafeWrap -Version 8.1.0
                    
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="SafeWrap" Version="8.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SafeWrap" Version="8.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SafeWrap" />
                    
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 SafeWrap --version 8.1.0
                    
#r "nuget: SafeWrap, 8.1.0"
                    
#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 SafeWrap@8.1.0
                    
#: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=SafeWrap&version=8.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SafeWrap&version=8.1.0
                    
Install as a Cake Tool

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

  1. Always specify expected exceptions and their status codes
  2. Use appropriate HTTP status codes for different error scenarios
  3. Keep error messages user-friendly but informative
  4. Use async methods for async operations
  5. Consider adding timeout handling for long-running operations

License

MIT License. See LICENSE file for details.

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

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