Class FaultResolver
- Namespace
- Cuemon.Diagnostics
- Assembly
- Cuemon.Diagnostics.dll
Provides a way to evaluate an exception and provide details about it in a developer friendly way.
public class FaultResolver : FaultHandler<ExceptionDescriptor>
- Inheritance
-
FaultResolver
- Inherited Members
Examples
FaultResolver registers exception-to-descriptor mappings for structured error reporting, using a validator predicate and a descriptor factory. This example registers a resolver that matches ArgumentNullException and produces an ExceptionDescriptor with code "ERR_NULL_ARG" and a message including the parameter name. It then tests resolution against both a matching ArgumentNullException("value") and a non-matching InvalidOperationException. Console output shows True, ERR_NULL_ARG, the descriptive message, the failure details ("value"), and False for the unmatched case where the result is null.
using System;
using Cuemon.Diagnostics; // for FaultResolver, ExceptionDescriptor
namespace MyApp.Examples;
public class FaultResolverExample
{
public void Demonstrate()
{
// Register a resolver that handles ArgumentNullException
var resolver = new FaultResolver(
validator: ex => ex is ArgumentNullException,
descriptor: ex =>
{
var argEx = (ArgumentNullException)ex;
return new ExceptionDescriptor(
argEx,
"ERR_NULL_ARG",
$"The parameter '{argEx.ParamName}' cannot be null.");
});
// Try to resolve an ArgumentNullException
var testEx = new ArgumentNullException("value");
bool resolved = resolver.TryResolveFault(testEx, out ExceptionDescriptor result);
Console.WriteLine(resolved); // True
Console.WriteLine(result.Code); // ERR_NULL_ARG
Console.WriteLine(result.Message); // The parameter 'value' cannot be null.
Console.WriteLine(result.Failure.Message); // value
// Try to resolve an unrelated exception
var otherEx = new InvalidOperationException("not handled");
resolved = resolver.TryResolveFault(otherEx, out result);
Console.WriteLine(resolved); // False
Console.WriteLine(result is null); // True
}
}
Constructors
FaultResolver(Func<Exception, bool>, Func<Exception, ExceptionDescriptor>)
Initializes a new instance of the FaultResolver class.
public FaultResolver(Func<Exception, bool> validator, Func<Exception, ExceptionDescriptor> descriptor)
Parameters
validatorFunc<Exception, bool>The function delegate that evaluates an Exception.
descriptorFunc<Exception, ExceptionDescriptor>The function delegate that provides details about an Exception.
Exceptions
- ArgumentNullException
validatorcannot be null -or-descriptorcannot be null.