RCi.ErrorAsValue
1.2.1
dotnet add package RCi.ErrorAsValue --version 1.2.1
NuGet\Install-Package RCi.ErrorAsValue -Version 1.2.1
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="RCi.ErrorAsValue" Version="1.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RCi.ErrorAsValue --version 1.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: RCi.ErrorAsValue, 1.2.1"
#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 RCi.ErrorAsValue as a Cake Addin #addin nuget:?package=RCi.ErrorAsValue&version=1.2.1 // Install RCi.ErrorAsValue as a Cake Tool #tool nuget:?package=RCi.ErrorAsValue&version=1.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
RCi.ErrorAsValue
.NET Error as value package
Usage:
Consider this example:
public sealed record Employee(string FirstName, string LastName, int Age);
private static readonly Dictionary<int, Employee> _employees = new()
{
{0, new("Michael", "Smith", 35)},
{1, new("Kenji", "Tanaka", 25)},
{2, new("Andrew", "Martinez", 50)},
{3, new("Emily", "Johnson", 28)},
{4, new("Jamal", "Washington", 30)},
};
public static Ve<Employee> GetEmployeeById(int id)
{
if (_employees.TryGetValue(id, out var employee))
{
return employee;
}
return Error.NewNotFound("cannot find employee by id", (nameof(id), id));
}
Try to get existing employee:
var (employee, err) = GetEmployeeById(3);
if (err)
{
Console.WriteLine(err.ToString());
}
else
{
Console.WriteLine(employee);
}
Output:
Employee { FirstName = Emily, LastName = Johnson, Age = 28 }
Try to get non-existing employee:
var (employee, err) = GetEmployeeById(420);
if (err)
{
Console.WriteLine(err.ToString());
}
Output:
NotFound: cannot find employee by id
We can get more details on the error:
var (employee, err) = GetEmployeeById(420);
if (err)
{
Console.WriteLine(JsonSerializer.Serialize(err.ToErrorDump(), new JsonSerializerOptions { WriteIndented = true }));
}
Output:
{
"Kind": "NotFound",
"Message": "cannot find employee by id",
"ThreadContext": "ManagedThreadId=1, ApartmentState=MTA, Name=",
"StackTrace": [
"at System.Environment.get_StackTrace()",
"at RCi.ErrorAsValue.Error.New(Error inner, String kind, String message, ValueTuple`2[] args)",
"at RCi.ErrorAsValue.Error.NewNotFound(String message, ValueTuple`2[] args)",
"at TestUsage.Program.GetEmployeeById(Int32 id) in D:\\code\\src\\RCi.ErrorAsValue\\TestUsage\\Program.cs:line 52",
"at TestUsage.Program.Test2() in D:\\code\\src\\RCi.ErrorAsValue\\TestUsage\\Program.cs:line 79",
"at TestUsage.Program.Main() in D:\\code\\src\\RCi.ErrorAsValue\\TestUsage\\Program.cs:line 12"
],
"Args": [
{
"Name": "id",
"Value": 420
}
]
}
Product | Versions 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.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.