DevOne.Utils.ErrorHandling
1.2.0
See the version list below for details.
dotnet add package DevOne.Utils.ErrorHandling --version 1.2.0
NuGet\Install-Package DevOne.Utils.ErrorHandling -Version 1.2.0
<PackageReference Include="DevOne.Utils.ErrorHandling" Version="1.2.0" />
paket add DevOne.Utils.ErrorHandling --version 1.2.0
#r "nuget: DevOne.Utils.ErrorHandling, 1.2.0"
// Install DevOne.Utils.ErrorHandling as a Cake Addin #addin nuget:?package=DevOne.Utils.ErrorHandling&version=1.2.0 // Install DevOne.Utils.ErrorHandling as a Cake Tool #tool nuget:?package=DevOne.Utils.ErrorHandling&version=1.2.0
ErrorHandling
Installation
dotnet add package DevOne.Utils.ErrorHandling --version 1.2.0
Usage
- ErrorHandler with value as string
{
string name = "marcos test";
using (var eh = new ErrorHandler())
{
switch (name.Length)
{
case < 3:
eh.Add(new(nameof(name), "name length must be greater than 2"));
break;
case > 25:
eh.Add(new(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(new(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<Error<string>>
will throws.
if you want to verify errors in a specific part of your code, you can avoid using
and use like that:
{
string name = "marcos test";
var eh = new ErrorHandler();
switch (name.Length)
{
case < 3:
eh.Add(new(nameof(name), "name length must be greater than 2"));
break;
case > 25:
eh.Add(new(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(new(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
{
record CustomErrorValue(string Message, string Id, string Location);
}
- use this error value on error handler instance
{
using (var eh = new ErrorHandler<CustomErrorValue>()) {}
}
- and now you need pass this type as the value
{
eh.Add(new("my key", new CustomErrorValue("message", "id", "location")));
}
you can catch any exceptions from error handler like that:
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:
try {
...
}
catch (ErrorHandlerException<Error<string>> exception)
{
Console.WriteLine(exception.Message);
foreach (var error in exception.Errors)
{
Console.WriteLine($"{error.Key}: {error.Value}"); // you have intellisense
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Add missing tests and improve exceptions