DevOne.Utils.ErrorHandling 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DevOne.Utils.ErrorHandling --version 1.0.0                
NuGet\Install-Package DevOne.Utils.ErrorHandling -Version 1.0.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="DevOne.Utils.ErrorHandling" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DevOne.Utils.ErrorHandling --version 1.0.0                
#r "nuget: DevOne.Utils.ErrorHandling, 1.0.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.
// Install DevOne.Utils.ErrorHandling as a Cake Addin
#addin nuget:?package=DevOne.Utils.ErrorHandling&version=1.0.0

// Install DevOne.Utils.ErrorHandling as a Cake Tool
#tool nuget:?package=DevOne.Utils.ErrorHandling&version=1.0.0                

ErrorHandling

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 (ErrorHandlerBaseException<Error<string>> exception)
{
    Console.WriteLine(exception.Message);

    foreach (var error in exception.Errors)
    {
        Console.WriteLine($"{error.Key}: {error.Value}"); // you have intellisense
    }
}
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
2.0.0 308 3/1/2023
1.2.1 251 2/24/2023
1.2.0 318 12/22/2022
1.1.0 285 12/17/2022
1.0.0 305 12/17/2022

First package version