Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/marcos-venicius/csharperrorhandlingpackage
A CSharp ErrorHandling lib
https://github.com/marcos-venicius/csharperrorhandlingpackage
csharp error-handling nuget package
Last synced: about 1 month ago
JSON representation
A CSharp ErrorHandling lib
- Host: GitHub
- URL: https://github.com/marcos-venicius/csharperrorhandlingpackage
- Owner: marcos-venicius
- License: mit
- Created: 2022-12-17T12:31:06.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-03-01T12:40:01.000Z (almost 2 years ago)
- Last Synced: 2024-11-28T19:19:10.258Z (2 months ago)
- Topics: csharp, error-handling, nuget, package
- Language: C#
- Homepage: https://www.nuget.org/packages/DevOne.Utils.ErrorHandling
- Size: 38.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ErrorHandling
[![Tests with .NET 7 and 6](https://github.com/marcos-venicius/CSharpErrorHandlingPackage/actions/workflows/lib-tests.yml/badge.svg)](https://github.com/marcos-venicius/CSharpErrorHandlingPackage/actions/workflows/lib-tests.yml)
---------------## Installation
```sh
dotnet add package DevOne.Utils.ErrorHandling --version 2.0.0
```### Usage
- ErrorHandler with value as string
```cs
{
string name = "marcos test";using (var eh = new ErrorHandler())
{
switch (name.Length)
{
case < 3: // with indexer
eh[nameof(name)] = "name length must be greater than 2";
break;
case > 25: // with method
eh.Add(nameof(name), "name length must be less than 26");
break;
default:
break;
}var regex = new Regex(@"^[a-z0-9]+$");
if (!regex.IsMatch(name))
{
eh.Add(nameof(name), "name format is invalid");
}
}
}
```when the program get out of the `using` scope, if exists some error an exception of type
`new ErrorHandlerException>` will throws.if you want to verify errors in a specific part of your code, you can avoid `using` and use like that:
```cs
{
string name = "marcos test";var eh = new ErrorHandler();
switch (name.Length)
{
case < 3:
eh[nameof(name)] = "name length must be greater than 2";
break;
case > 25:
eh[nameof(name)] = "name length must be less than 26";
break;
default:
break;
}var regex = new Regex(@"^[a-z0-9]+$");
if (!regex.IsMatch(name))
{
eh[nameof(name)] = "name format is invalid";
}...
eh.Dispose(); // verify the errors
}
```if you want a diferent error value type use can do this like that:
* create a custom error value
```cs
{
record CustomErrorValue(string Message, string Id, string Location);
}
```* use this error value on error handler instance
```cs
{
using (var eh = new ErrorHandler()) {}
}
```* and now you need pass this type as the value
```cs
{
eh["my key"] = new CustomErrorValue("message", "id", "location");
}
```you can catch any exceptions from error handler like that:
```cs
try {
...
}
catch (ErrorHandlerBaseException exception)
{
Console.WriteLine(exception.Message);foreach (var error in exception.Errors)
{
Console.WriteLine(error);
}
}
```this way will catch any type of exception that the ErrorHandler throws
if you want to catch a specific exception type you can:```cs
try {
...
}
catch (ErrorHandlerException> exception)
{
Console.WriteLine(exception.Message);foreach (var error in exception.Errors)
{
Console.WriteLine($"{error.Key}: {error.Value}"); // you have intellisense
}
}
```