FalconWare.ErrorHandling 1.1.0

dotnet add package FalconWare.ErrorHandling --version 1.1.0
NuGet\Install-Package FalconWare.ErrorHandling -Version 1.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="FalconWare.ErrorHandling" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FalconWare.ErrorHandling --version 1.1.0
#r "nuget: FalconWare.ErrorHandling, 1.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.
// Install FalconWare.ErrorHandling as a Cake Addin
#addin nuget:?package=FalconWare.ErrorHandling&version=1.1.0

// Install FalconWare.ErrorHandling as a Cake Tool
#tool nuget:?package=FalconWare.ErrorHandling&version=1.1.0

build-test

FalconWare - Error Handling

Package provides OpResult class to represent the result of an operation successful or not. See original blog post on the Operation Result pattern in C# .NET here

Improve your error handling by enforcing:

  • Checking the result of an operation
  • Handling success and non-success code paths
  • Improving code readability

Usage

After adding this NuGet package e.g.

dotnet add package FalconWare.ErrorHandling

Handle results of an opertaion checking the result's WasSuccess then accessing the result Value, for example:

var pokemonName = "squirtle";
var result = await TryGetPokemonBmiAsync(pokemonName);
if (!result.WasSuccess)
{
    // handle the failure
    _logger.LogError($"Failed to get {pokemonName} BMI: {result.NonSuccessMessage}");
}
else
{
    // do something with the return value
    var bmi = result.Value;
    if (bmi < 18.5)
    {
        Console.WriteLine($"{pokemonName} is underweight!");
    }
    else if (bmi < 24.9)
    {
        Console.WriteLine($"{pokemonName} within normal range");
    }
    else
    {
        Console.WriteLine($"{pokemonName} is overweight!");        
    }
}

Implement your operations to return an OpResult<T> so callers try the operation and use the result without having to write try{} catch{} blocks for operations that can fail - leaving the onus of catching exceptions in the implementation, for example:

public async Task<OpResult<float>> TryGetPokemonBmiAsync(string name)
{
    // query the pokeapi for pokemon with name supplied
    string jsonString;
    try
    {
        var response = await _httpClient.GetAsync($"pokemon/{name}");
        if (response.IsSuccessStatusCode)
        {
            jsonString = await response.Content.ReadAsStringAsync();
        }
        else
        {
            return OpResultFactory.CreateFailure<float>($"Failed getting pokemon '{name}', HTTP status code: {response.StatusCode}");
        }
    }
    catch (HttpRequestException ex) 
    {
        return OpResultFactory.CreateFailure<float>(ex);
    }

    // parse pokemon json
    JObject pokemon;
    try
    {
        pokemon = JObject.Parse(jsonString);
    }
    catch (JsonReaderException jre)
    {
        return OpResultFactory.CreateFailure<float>(jre);
    }

    // try extract height and weight, calc bmi and return that
    try
    {
        var height = pokemon["height"].Value<float>();
        var weight = pokemon["weight"].Value<float>();
        if (height < 1.0f)
        {
            return OpResultFactory.CreateFailure<float>("Failed to parse pokemon - height cannot be less than 1");
        }
        if (weight < 1.0f)
        {
            return OpResultFactory.CreateFailure<float>("Failed to parse pokemon - weight cannot be less than 1");
        }
        var bmi = weight / (height * height);
        return OpResultFactory.CreateSuccess(bmi);
    }
    catch (NullReferenceException nre)
    {
        var msg = $"Failed parse pokemon response height or weight missing: {nre.Message}";
        return OpResultFactory.CreateFailure<float>(msg);
    }
    catch (FormatException fe) 
    {
        var msg = $"Failed parse pokemon response height or weight: {fe.Message}";
        return OpResultFactory.CreateFailure<float>(msg);
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.
  • 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
1.1.0 4,808 6/9/2023
1.0.0 116 6/9/2023