kgrzybek/modular-monolith-with-ddd · warning · InvalidCommandException
Command validation error
Error message
Command validation error
What it means
Thrown by the UserAccess module ValidationCommandHandlerWithResultDecorator (TResult-returning variant). It runs all FluentValidation validators for command T, aggregates ErrorMessages, and throws InvalidCommandException(errors) with the literal message 'Command validation error'; the actionable detail is in Errors.
Source
Thrown at src/Modules/UserAccess/Infrastructure/Configuration/Processing/ValidationCommandHandlerWithResultDecorator.cs:33
public ValidationCommandHandlerWithResultDecorator(
IList<IValidator<T>> validators,
ICommandHandler<T, TResult> decorated)
{
this._validators = validators;
_decorated = decorated;
}
public Task<TResult> Handle(T command, CancellationToken cancellationToken)
{
var errors = _validators
.Select(v => v.Validate(command))
.SelectMany(result => result.Errors)
.Where(error => error != null)
.ToList();
if (errors.Any())
{
throw new InvalidCommandException(errors.Select(x => x.ErrorMessage).ToList());
}
return _decorated.Handle(command, cancellationToken);
}
}
}View on GitHub (pinned to 91c8ef24b4)
Solutions
- Read ex.Errors for the specific failing validators (message is generic).
- Fix the command values to pass validation.
- Pre-validate on the client with the same rules.
- Adjust validators if a valid value is wrongly rejected.
Example fix
// before
var userId = await _commandDispatcher.SendAsync(new AuthenticateCommand("", ""));
// after
var userId = await _commandDispatcher.SendAsync(new AuthenticateCommand(login, password)); Defensive patterns
Strategy: validation
Validate before calling
var validator = new AuthenticateCommandValidator(); var result = await validator.ValidateAsync(cmd); if (!result.IsValid) return BadRequest(result.Errors.Select(e => e.ErrorMessage)); var userId = await _commandDispatcher.SendAsync(cmd);
Try / catch
try { var userId = await _commandDispatcher.SendAsync(cmd); }
catch (InvalidCommandException ex)
{ return BadRequest(new { errors = ex.Errors }); } // message is generic; detail is in Errors Prevention
- Validate required credentials client-side before dispatch.
- Always read ex.Errors since the summary message is generic.
- Keep validators current with command DTO changes.
When it happens
Trigger: Sending a result-returning UserAccess command (e.g. authenticate, register user) with properties failing a registered validator.
Common situations: Authentication/register commands with empty login, weak password, or missing required fields.
Related errors
- Command validation error
- {validator error messages}
- {validator error messages}
- {validator error messages}
- {validator error messages}
AI-assisted analysis of kgrzybek/modular-monolith-with-ddd@91c8ef24b4 (2026-08-13).
Data as JSON: /api/errors/c863ff440c63614d.
Report an issue: GitHub.