Skip to content

Return a result or error from a Cratis Arc command

A Cratis Arc command can return the identifier of a newly registered author or validation feedback explaining why registration failed. Write through your database or application service, then return what the caller needs. Returning a value does not persist it; event appends require the separate Cratis Chronicle integration.

Use a configured standalone Arc application and an author registration command. The C# handler below replaces the handler from the MongoDB tutorial and uses its domain types and collection setup. Add using Cratis.Monads; and using Cratis.Arc.Validation; to the C# command file. The JVM and TypeScript fragments instead expect an application-owned AuthorRepository with name lookup and save methods.

Keep the database unique index from validation. It enforces uniqueness when two requests try to register the same name.

The return shapes in this table are C# signatures. The JVM and TypeScript fragments below show their corresponding handler contracts.

You want to… Return
Complete without response data void, Task, or ValueTask
Give the caller a value that value, or Task<T> / ValueTask<T>
Succeed with data or reject validation Result<TResponse, ValidationResult> (possibly asynchronous)
Give the caller several pieces of data one response data transfer object (DTO) containing those values
Combine a response with server-side work a tuple containing one ordinary response and operations or other server-handled values

Replace the handler with a name check followed by an insert. If the name already exists, give the caller a validation message associated with the name field:

public async Task<Result<AuthorId, ValidationResult>> Handle(IMongoCollection<Author> authors)
{
if (await authors.Find(author => author.Name == Name).AnyAsync())
{
return ValidationResult.Error("An author with that name is already registered.", [nameof(Name)]);
}
await authors.InsertOneAsync(new Author(Id, Name));
return Id;
}

View C# snippet source on GitHub

C# declares success and rejection alternatives with Result<TResponse, ValidationResult>. The other backends use different contracts; do not copy that signature into their handlers.

On the JVM, keep one client response type for the generated proxy and OpenAPI metadata. Throw an application exception implementing io.cratis.arc.validation.ValidationFailure to reject. The pipeline exposes its validation feedback, not its exception message or stack trace. An ordinary exception still becomes an exception result.

TypeScript returns rejected(validation(message, members)) for a typed failure, or the response value on success.

The insert is the persistent effect; the AuthorId is response data. The pre-check gives a friendly failure but is not atomic. Keep the database unique index from validation and explicitly translate its duplicate-write failure if you need friendly errors under races. Other storage exceptions remain exception results; do not disguise them as ordinary validation.

For rules that can reject before work begins, prefer a backend command validator; use a typed handler failure when the write/decision itself discovers the failure. Provide() can also short-circuit with typed validation.

Regenerate the command proxy after changing the response contract. Its execute() returns a CommandResult containing the response for this signature. Check isSuccess before using response; on failure, inspect validationResults, isAuthorized, and exception information. The command-result contract explains these fields: a command can fail for reasons other than validation.

Submit an unused author name. Expect success, the returned AuthorId, and a stored author with that identifier. Submit the same name again: expect validation feedback associated with the name field and no second author from this request.

This checks the sequential path, not concurrency protection. The pre-check can pass in two requests at once; the unique index must reject the duplicate write. Use a command test to assert the stored or delegated work as well as the result.

If you want to record an event rather than insert an author directly, configure Arc + Chronicle first. On C#, its registered-event response handlers append events using these integration signatures; standalone Arc does not interpret an event-like class name as a request to persist it:

Integrated intention Return shape and prerequisite
Append a registered event AuthorRegistered Handle(); Chronicle resolves the source from the command
Select the source in the response (AuthorId, AuthorRegistered) only when AuthorId derives from Chronicle EventSourceId / EventSourceId<T>
Return caller data alongside an event (result, event); unhandled values remain response data
Reject or append Result<AuthorRegistered, ValidationResult>
Append several events an enumerable of registered events; use EventForEventSourceId for explicit cross-stream targeting

The standalone tutorial’s AuthorId : ConceptAs<Guid> is not an event-source identifier. An ordinary Guid in a response remains ordinary caller data; do not reinterpret it as an append target. See Chronicle response identifiers for explicit-source contracts.