Enhance require! macro to accept typed error values #327

Closed
opened 2026-04-09 08:52:00 -07:00 by jwilger-ai-bot · 1 comment
jwilger-ai-bot commented 2026-04-09 08:52:00 -07:00 (Migrated from github.com)

Context

When using CommandLogic::handle() with typed business-rule error enums (via thiserror::Error + From<E> for CommandError), the require! macro can't be used because it only accepts string literals.

This forces verbose if/return patterns instead of concise require! syntax:

// Can't do this:
require!(state.is_setup_completed(), AuthenticateAdminError::SetupNotCompleted);

// Must do this instead:
if !state.is_setup_completed() {
    return Err(AuthenticateAdminError::SetupNotCompleted.into());
}

Proposal

Enhance require! to accept any value that implements Into<CommandError>:

// Current: string-only
require!(condition, "error-string");

// Proposed: also accept typed errors
require!(condition, MyError::Variant);

The macro should detect whether the second argument is a string literal or an expression and:

  • For strings: current behavior (CommandError::BusinessRuleViolation(s))
  • For expressions: call .into() to convert via the From impl

Ideally, CommandError should also preserve a reference to the original typed error for downstream consumers that need to match on specific variants.

Origin

Identified during slipstream-consulting/stochastic_macro PR #99 review.

## Context When using `CommandLogic::handle()` with typed business-rule error enums (via `thiserror::Error` + `From<E> for CommandError`), the `require!` macro can't be used because it only accepts string literals. This forces verbose if/return patterns instead of concise `require!` syntax: ```rust // Can't do this: require!(state.is_setup_completed(), AuthenticateAdminError::SetupNotCompleted); // Must do this instead: if !state.is_setup_completed() { return Err(AuthenticateAdminError::SetupNotCompleted.into()); } ``` ## Proposal Enhance `require!` to accept any value that implements `Into<CommandError>`: ```rust // Current: string-only require!(condition, "error-string"); // Proposed: also accept typed errors require!(condition, MyError::Variant); ``` The macro should detect whether the second argument is a string literal or an expression and: - For strings: current behavior (`CommandError::BusinessRuleViolation(s)`) - For expressions: call `.into()` to convert via the `From` impl Ideally, `CommandError` should also preserve a reference to the original typed error for downstream consumers that need to match on specific variants. ## Origin Identified during slipstream-consulting/stochastic_macro PR #99 review.
jwilger-ai-bot commented 2026-04-11 17:15:08 -07:00 (Migrated from github.com)

Completed in #335 — require! macro now accepts Into typed error values.

Completed in #335 — require! macro now accepts Into<CommandError> typed error values.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
jwilger/eventcore#327
No description provided.