Skip to main content

Results and Errors

Dispatch provides a comprehensive result type system for handling operation outcomes without relying on exceptions for control flow. The IMessageResult and IMessageResult<T> interfaces enable clean error handling with full support for railway-oriented programming patterns.

Why Should I Care?

Throwing exceptions for business validation ("order not found", "insufficient stock") is expensive and makes error paths invisible in your code. IMessageResult makes success and failure first-class values -- you can pattern-match, chain with .Map() and .Match(), and convert directly to HTTP responses with .ToApiResult(). Your handlers return results, not throw exceptions.

Before You Start​

  • .NET 10.0
  • Install the required package:
    dotnet add package Excalibur.Dispatch
  • Familiarity with actions and handlers

MessageResult API​

Creating Success Results​

// Simple success (no return value)
return MessageResult.Success();

// Success with a typed value
return MessageResult.Success(order);

// Success from cache hit
return MessageResult.SuccessFromCache();

// Success with additional context
return MessageResult.Success(
value: order,
routingDecision: null,
validationResult: validationContext,
authorizationResult: authResult,
disposition: MessageDisposition.Handled);

Creating Failed Results​

// Simple failure with error message
return MessageResult.Failed("Order not found");

// Failure with problem details
return MessageResult.Failed(new MessageProblemDetails
{
Type = ProblemDetailsTypes.NotFound, // "urn:dispatch:error:not-found"
Title = "Resource Not Found",
Status = 404,
Detail = $"Order with ID {orderId} was not found"
});

// Typed failure
return MessageResult.Failed<Order>("Validation failed", problemDetails);

// Failure from an exception
return MessageResult.Failed(exception);

// Typed failure from an exception
return MessageResult.Failed<Order>(exception);

// Failure with validation and authorization context
return MessageResult.Failed(problemDetails, validationResult, authorizationResult);

Creating Cancelled Results​

Use Cancelled() when an operation is cancelled via a CancellationToken or business logic. The non-generic overload returns a cached singleton for hot-path performance.

// Cancellation (returns cached singleton)
return MessageResult.Cancelled();

// Typed cancellation
return MessageResult.Cancelled<Order>();

IMessageResult Interface​

The base interface for all result types:

public interface IMessageResult
{
// Primary success indicator
bool Succeeded { get; }


// Error message when failed
string? ErrorMessage { get; }

// How the result was produced: Handled, ServedFromCache, or SuppressedAsDuplicate
MessageDisposition Disposition { get; }

// Validation context
object? ValidationResult { get; }

// Authorization context
object? AuthorizationResult { get; }

// Structured error information
IMessageProblemDetails? ProblemDetails { get; }
}

IMessageResult<T> Interface​

Extends the base interface with a typed return value:

public interface IMessageResult<out T> : IMessageResult
{
// The return value (null if failed)
T? ReturnValue { get; }
}

Checking Results​

var result = await dispatcher.DispatchAsync(action, cancellationToken);

// Check success
if (result.Succeeded)
{
// Handle success
}

// Alternative syntax
if (result.Succeeded)
{
// Handle success
}

// Check how the result was produced. `Succeeded` says whether the operation failed; it does not say
// whether a handler ran. A cached result and a suppressed duplicate both succeed with no handler
// invocation, so code that records a message as processed must read the disposition, not just success.
switch (result.Disposition)
{
case MessageDisposition.ServedFromCache:
_logger.LogDebug("Result served from cache");
break;
case MessageDisposition.SuppressedAsDuplicate:
_logger.LogDebug("Message already accepted for processing; no handler ran");
break;
case MessageDisposition.Handled:
default:
break;
}

// Access error information
if (!result.Succeeded)
{
Console.WriteLine(result.ErrorMessage);
Console.WriteLine(result.ProblemDetails?.Detail);
}

Functional Composition​

Dispatch supports railway-oriented programming patterns that enable cleaner, more expressive code when working with results. Instead of verbose null checking and conditional logic, you can chain operations functionally.

Why Functional Composition?​

Traditional result handling requires verbose conditional logic:

// Verbose pattern - lots of null checks and early returns
public async Task<IActionResult> GetOrder(Guid orderId, CancellationToken ct)
{
var action = new GetOrderAction(orderId);
var result = await _dispatcher.DispatchAsync<GetOrderAction, Order>(action, ct);

if (!result.Succeeded || result.ReturnValue is null)
{
return result.ProblemDetails?.Status switch
{
404 => NotFound(result.ProblemDetails),
400 => BadRequest(result.ProblemDetails),
_ => StatusCode(500, result.ProblemDetails)
};
}

var order = result.ReturnValue;
var dto = new OrderDto(order);
_logger.LogInformation("Retrieved order {OrderId}", dto.Id);
return Ok(dto);
}

Functional composition enables cleaner, more declarative code:

// Functional pattern - chain operations cleanly
public async Task<IActionResult> GetOrder(Guid orderId, CancellationToken ct)
{
var action = new GetOrderAction(orderId);

return await _dispatcher
.DispatchAsync<GetOrderAction, Order>(action, ct)
.Map(order => new OrderDto(order))
.Tap(dto => _logger.LogInformation("Retrieved order {OrderId}", dto.Id))
.Match(
onSuccess: dto => Ok(dto),
onFailure: problem => problem?.Status switch
{
404 => NotFound(problem),
400 => BadRequest(problem),
_ => StatusCode(500, problem)
});
}

The functional pattern:

  • Eliminates null checks (handled automatically)
  • Makes the happy path obvious (read top to bottom)
  • Ensures errors propagate correctly (railway pattern)
  • Reduces nesting and improves readability

When to Use Each Pattern​

MethodUse When
MapTransforming the success value (e.g., entity to DTO)
BindChaining operations that might fail (e.g., validation, external calls)
MatchConverting the result to a different type (e.g., IActionResult)
TapExecuting side effects (logging, metrics, notifications)
GetValueOrDefaultYou need a fallback value when the result fails
GetValueOrThrowYou want to fail fast with an exception

Full signatures and async overloads for each method are in the Result API reference.

Complete Example: Order Processing​

public class OrderController : ControllerBase
{
private readonly IDispatcher _dispatcher;
private readonly ILogger<OrderController> _logger;

[HttpPost]
public async Task<IActionResult> CreateOrder(
CreateOrderRequest request,
CancellationToken ct)
{
var action = new CreateOrderAction(request.CustomerId, request.Items);

return await _dispatcher
.DispatchAsync<CreateOrderAction, Order>(action, ct)
.Tap(order => _logger.LogInformation("Order {Id} created", order.Id))
.Map(order => new OrderResponse(order))
.Match(
onSuccess: response => CreatedAtAction(
nameof(GetOrder),
new { id = response.Id },
response),
onFailure: problem => problem switch
{
{ Status: 400 } => BadRequest(problem),
{ Status: 404 } => NotFound(problem),
{ Status: 409 } => Conflict(problem),
_ => StatusCode(500, problem)
});
}

[HttpGet("{id}")]
public async Task<IActionResult> GetOrder(Guid id, CancellationToken ct)
{
var action = new GetOrderAction(id);

return await _dispatcher
.DispatchAsync<GetOrderAction, Order>(action, ct)
.Map(order => new OrderResponse(order))
.Match(
onSuccess: Ok,
onFailure: problem => problem?.Status == 404
? NotFound()
: Problem(problem?.Detail));
}
}

For Minimal API endpoints, the terminal operators in Excalibur.Dispatch.Hosting.AspNetCore remove the explicit Match branch — see Results to HTTP responses.

Handler Implementation​

Return results from your handlers:

public class CreateOrderHandler : IActionHandler<CreateOrderAction, Order>
{
private readonly IOrderRepository _repository;
private readonly IValidator<CreateOrderAction> _validator;

// Note: Handlers return TResult directly (not IMessageResult<TResult>).
// The framework wraps the return value in IMessageResult automatically.
// Use validation middleware for pre-validation, or throw exceptions
// that exception mapping middleware converts to proper results.
public async Task<Order> HandleAsync(
CreateOrderAction action,
CancellationToken ct)
{
// Validate (or use validation middleware for automatic validation)
var validation = await _validator.ValidateAsync(action, ct);
if (!validation.IsValid)
{
throw new ValidationException(
validation.Errors.ToDictionary(
e => e.PropertyName,
e => new[] { e.ErrorMessage }));
}

// Create order
var order = new Order
{
Id = Guid.NewGuid(),
CustomerId = action.CustomerId,
Items = action.Items,
CreatedAt = DateTime.UtcNow
};

await _repository.SaveAsync(order, ct);
return order; // Framework wraps in IMessageResult<Order>
}
}

Problem Details​

Use IMessageProblemDetails for RFC 7807 compliant error responses:

public interface IMessageProblemDetails
{
string Type { get; set; } // URI identifying the problem type
string Title { get; set; } // Short human-readable summary
int? Status { get; set; } // HTTP status code (RFC 7807)
int ErrorCode { get; set; } // Application-specific error code
string Detail { get; set; } // Human-readable explanation
string Instance { get; set; } // URI identifying the specific occurrence
IDictionary<string, object?> Extensions { get; } // Additional extension fields
}

Creating Problem Details​

using Excalibur.Dispatch;

var problemDetails = new MessageProblemDetails
{
Type = ProblemDetailsTypes.Validation, // "urn:dispatch:error:validation"
Title = "Insufficient Funds",
Status = 402,
Detail = $"Account {accountId} has insufficient funds. Required: {required}, Available: {available}",
Instance = $"/orders/{orderId}"
};

return MessageResult.Failed<PaymentResult>("Payment failed", problemDetails);

Use the ProblemDetailsTypes constants rather than inline strings. The full set, and the convention for adding your own, is in the Result API reference.

Best Practices​

Prefer Results Over Exceptions​

// Good: Return result for expected failures
public async Task<IMessageResult<Order>> GetOrderAsync(Guid id, CancellationToken ct)
{
var order = await _repository.FindByIdAsync(id, ct);
if (order is null)
return MessageResult.Failed<Order>("Order not found");

return MessageResult.Success(order);
}

// Avoid: Throwing exceptions for expected cases
public async Task<Order> GetOrderAsync(Guid id, CancellationToken ct)
{
var order = await _repository.FindByIdAsync(id, ct);
if (order is null)
throw new ResourceNotFoundException($"Order {id} not found"); // Don't do this

return order;
}

Use Functional Composition​

// Good: Chain operations functionally
return await GetOrderAsync(id, ct)
.Bind(ValidateOrderAsync)
.Bind(ProcessPaymentAsync)
.Map(CreateConfirmation);

// Avoid: Verbose null checking
var orderResult = await GetOrderAsync(id, ct);
if (!orderResult.Succeeded) return orderResult.Failed();

var validationResult = await ValidateOrderAsync(orderResult.ReturnValue);
if (!validationResult.Succeeded) return validationResult.Failed();

var paymentResult = await ProcessPaymentAsync(validationResult.ReturnValue);
// ...

Include Context in Problem Details​

// Good: Detailed, actionable error information
return MessageResult.Failed<T>(
"Validation failed",
new MessageProblemDetails
{
Type = ProblemDetailsTypes.Validation, // "urn:dispatch:error:validation"
Title = "Validation Error",
Status = 400,
Detail = "The 'email' field must be a valid email address",
Instance = $"/users/{userId}"
});

// Avoid: Vague error messages
return MessageResult.Failed<T>("Invalid input");

When to Use Exceptions vs Results​

ScenarioApproach
Expected business failure (e.g., insufficient funds)Return MessageResult.Failed
Unexpected infrastructure failureThrow exception
Resource not found in handlerEither works - choose consistently
Validation errorsValidationException or MessageResult.Failed with validation details
External service timeoutOperationTimeoutException
Concurrency conflictConcurrencyException

The exception types named here, and the full hierarchy they belong to, are in the Result API reference. To convert them to Problem Details centrally instead of per handler, see Exception Mapping.

Error Handling Decision Tree​

Use this to pick the right error strategy for your scenario:

Is the error a business rule violation (validation, not found, insufficient funds)?
├── YES → Return MessageResult.Failed(problemDetails)
│ Don't throw exceptions for expected business outcomes.
│
└── NO → Is it a transient infrastructure failure (timeout, connection reset)?
├── YES → Let resilience middleware handle it
│ Add: dispatch.UseResilience()
│ The middleware retries automatically with backoff.
│
└── NO → Is it a permanent infrastructure failure (bad config, missing table)?
├── YES → Throw an exception
│ Let it propagate to the caller. Log it. Fix the root cause.
│
└── NO → Is it a message that cannot be processed after retries?
└── YES → Use the dead-letter pattern
Messages move to a DLQ after max retries.
See: Dead Letter Pattern docs
ScenarioStrategyCode Pattern
Validation failureMessageResult.Failed(problem)Return result, don't throw
Entity not foundMessageResult.Failed("Not found")Return result
Transient failureResilience middlewaredispatch.UseResilience()
Permanent failureExceptionthrow new InvalidOperationException(...)
Poison messageDead letter queueTransport DLQ config
Concurrency conflictReload + retryCatch ConcurrencyException, reload aggregate

Next Steps​

What's Next​

See Also​