public sealed class UserErrors : ErrorCategory
{
public static readonly ErrorCode NotFound = new ( "USER_001" , "User not found" ) ;
public static readonly ErrorCode InvalidEmail = new ( "USER_002" , "Invalid email" ) ;
}
var registry = new ErrorCodeRegistry ( ) ;
registry . RegisterCategory < UserErrors > ( ) ;
var registry = new ErrorCodeRegistry ( ) ;
await registry . TryRegisterAsync ( UserErrors . NotFound , cancellationToken ) ;
var error = AppError . From ( UserErrors . NotFound )
. WithContext ( "id" , userId ) ;
var error = AppError . From ( UserErrors . InvalidEmail )
. WithContext ( new ErrorContext ( "email" , address ) ) ;
var error = AppError . From ( UserErrors . InvalidEmail )
. WithMetadata ( "traceId" , traceId ) ;
Result < User > result = user ;
Result failed = AppError . From ( UserErrors . NotFound ) ;
var details = ErrorProblemDetails . FromError ( error , status : 400 ) ;
var json = ErrorProblemDetailsJson . Serialize ( details ) ;
var roundTrip = ErrorProblemDetailsJson . Deserialize ( json ) ;
Publishing errors to observers
services . AddErrorKit ( ) ;
var hub = provider . GetRequiredService < IErrorHub > ( ) ;
hub . RegisterObserver ( new ConsoleObserver ( ) ) ;
var reporter = provider . GetRequiredService < IErrorReporter > ( ) ;
reporter . Report ( error ) ;
Publishing errors to async observers
services . AddErrorKit ( ) ;
var hub = provider . GetRequiredService < IErrorHub > ( ) ;
hub . RegisterAsyncObserver ( new TelemetryObserver ( ) ) ;
var reporter = provider . GetRequiredService < IErrorReporter > ( ) ;
await reporter . ReportAsync ( error , cancellationToken ) ;
Converting errors to exceptions
var bridge = provider . GetRequiredService < IErrorExceptionBridge > ( ) ;
throw bridge . ToException ( error ) ;
Converting exceptions to errors
var bridge = provider . GetRequiredService < IErrorExceptionBridge > ( ) ;
var error = bridge . FromException ( exception ) ;
Creating errors asynchronously
var factory = provider . GetRequiredService < IAsyncErrorFactory > ( ) ;
var error = await factory . CreateAsync ( UserErrors . NotFound , cancellationToken : cancellationToken ) ;