Instruction file imported from hebelmx/ExxerCube.Prisma (
.cursor/rules/1027_ExxerAIResultPattern.mdc). Copyright stays with the author.
ExxerAI Result Pattern
Meta
Title: ExxerAI Result Pattern Description: Functional error handling pattern using Result instead of exceptions Applies-to: All C# code in ExxerAI project
Requirements
if (agentId == Guid.Empty)
return Result<Agent>.WithFailure("Agent ID cannot be empty");
var agent = await _repository.GetByIdAsync(agentId, cancellationToken);
return agent != null
? Result<Agent>.Success(agent)
: Result<Agent>.WithFailure($"Agent not found with ID: {agentId}");
}
</correct-example>
<incorrect-example>
```csharp
// ❌ Incorrect: Throwing exceptions in normal control flow
public async Task<Agent> GetAgentAsync(Guid agentId)
{
if (agentId == Guid.Empty)
throw new ArgumentException("Agent ID cannot be empty");
var agent = await _repository.GetByIdAsync(agentId);
if (agent == null)
throw new AgentNotFoundException($"Agent not found with ID: {agentId}");
return agent;
}
var agent = agentResult.Value; var name = agent.Name; var validationResult = ValidateAgentName(name); if (validationResult.IsFailure) return validationResult;
return $"Agent {name} is valid";
</incorrect-example>
</requirement>
<requirement priority="high">
**Description**: Use early returns for validation and cancellation
**Examples**:
<correct-example>
```csharp
// ✅ Correct: Early returns for validation
public async Task<Result<Agent>> CreateAgentAsync(string name, CancellationToken cancellationToken = default)
{
// Early cancellation check
if (cancellationToken.IsCancellationRequested)
return ResultExtensions.Cancelled<Agent>();
// Early validation
if (string.IsNullOrWhiteSpace(name))
return Result<Agent>.WithFailure("Agent name cannot be empty");
// Business logic
var agent = new Agent { Name = name };
return await _repository.AddAsync(agent, cancellationToken);
}