eventide-application 0.1.1

Application layer for the eventide DDD/CQRS toolkit: command bus, query bus, handlers, application context, and an in-memory bus implementation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! # Application-layer unified error type
//!
//! This module defines [`AppError`], the canonical error type returned by
//! command/query handlers and the surrounding application-layer plumbing.
//! It integrates seamlessly with the [`ErrorCode`] trait from
//! `eventide-domain` so that the same `code()` / `kind()` / `http_status()`
//! contract used in the domain layer can be re-exported all the way up to
//! the HTTP/gRPC boundary.
//!
//! ## Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────┐
//! │  eventide-domain                                                │
//! │  ┌─────────────┐  ┌─────────────┐  ┌─────────────────────────┐  │
//! │  │ ErrorKind   │  │ ErrorCode   │  │ DomainError             │  │
//! │  │ (category)  │  │ (trait)     │  │ impl ErrorCode ✓        │  │
//! │  └─────────────┘  └─────────────┘  └─────────────────────────┘  │
//! └─────────────────────────────────────────────────────────────────┘
//!//!//! ┌─────────────────────────────────────────────────────────────────┐
//! │  eventide-application (this module)                             │
//! │  ┌───────────────────────────────────────────────────────────┐  │
//! │  │ AppError                                                  │  │
//! │  │ impl ErrorCode ✓                                          │  │
//! │  │ impl From<DomainError> ✓                                  │  │
//! │  └───────────────────────────────────────────────────────────┘  │
//! └─────────────────────────────────────────────────────────────────┘
//! ```
//!
//! ## Quick start
//!
//! ### Using AppError
//!
//! ```rust
//! use eventide_application::error::{AppError, AppResult};
//! use eventide_domain::error::DomainError;
//!
//! fn process_command() -> AppResult<String> {
//!     // Domain errors are converted into AppError automatically.
//!     let domain_result: Result<String, DomainError> =
//!         Err(DomainError::not_found("user 123"));
//!     domain_result?;
//!
//!     Ok("success".to_string())
//! }
//!
//! fn validate_input(input: &str) -> AppResult<()> {
//!     if input.is_empty() {
//!         return Err(AppError::validation("input cannot be empty"));
//!     }
//!     Ok(())
//! }
//! ```
//!
//! ### Mapping to an API response
//!
//! ```rust,ignore
//! use axum::response::{IntoResponse, Response};
//! use axum::http::StatusCode;
//! use axum::Json;
//! use eventide_domain::error::ErrorCode;
//! use serde::Serialize;
//!
//! #[derive(Serialize)]
//! pub struct ApiErrorResponse {
//!     pub code: String,
//!     pub message: String,
//! }
//!
//! pub struct ApiError<E>(pub E);
//!
//! impl<E: ErrorCode> IntoResponse for ApiError<E> {
//!     fn into_response(self) -> Response {
//!         let status = StatusCode::from_u16(self.0.http_status())
//!             .unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
//!
//!         let body = ApiErrorResponse {
//!             code: self.0.code().to_string(),
//!             message: self.0.to_string(),
//!         };
//!
//!         (status, Json(body)).into_response()
//!     }
//! }
//! ```

use eventide_domain::error::{DomainError, ErrorCode, ErrorKind};
use std::error::Error as StdError;
use std::fmt;

/// Unified application-layer error type.
///
/// `AppError` is the single error type produced by application-layer
/// handlers. It can wrap a [`DomainError`] (with the domain's `kind` and
/// `code` preserved) or carry an application-specific failure such as
/// validation, authorization, or a handler routing problem.
///
/// # Features
///
/// - Implements [`ErrorCode`], so it can be turned directly into an HTTP /
///   API response without further mapping.
/// - Provides `From<DomainError>` so that `?` lifts domain errors
///   transparently.
/// - Offers application-specific constructors for common failure modes
///   (validation, authorization, handler routing, type coercion).
///
/// # Examples
///
/// ```rust
/// use eventide_application::error::AppError;
/// use eventide_domain::error::{ErrorCode, ErrorKind};
///
/// let err = AppError::validation("email format invalid");
/// assert_eq!(err.kind(), ErrorKind::InvalidValue);
/// assert_eq!(err.code(), "VALIDATION_ERROR");
///
/// let err = AppError::handler_not_found("CreateUserHandler");
/// assert_eq!(err.kind(), ErrorKind::Internal);
/// assert_eq!(err.code(), "HANDLER_NOT_FOUND");
/// ```
pub struct AppError {
    kind: ErrorKind,
    code: &'static str,
    message: Box<str>,
    source: Option<Source>,
}

enum Source {
    Domain(DomainError),
    Other(Box<dyn StdError + Send + Sync>),
}

impl AppError {
    /// Construct a new application error with the given kind, stable code
    /// string, and human-readable message.
    fn new(kind: ErrorKind, code: &'static str, message: impl Into<Box<str>>) -> Self {
        Self {
            kind,
            code,
            message: message.into(),
            source: None,
        }
    }

    // ==================== Convenience constructors ====================

    /// Build a validation error.
    ///
    /// Use this for application-layer input validation failures (shape,
    /// format, length) that occur before reaching domain logic.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use eventide_application::error::AppError;
    /// use eventide_domain::error::ErrorCode;
    ///
    /// let err = AppError::validation("email format invalid");
    /// assert_eq!(err.code(), "VALIDATION_ERROR");
    /// assert_eq!(err.http_status(), 400);
    /// ```
    #[must_use]
    pub fn validation(msg: impl Into<Box<str>>) -> Self {
        Self::new(ErrorKind::InvalidValue, "VALIDATION_ERROR", msg)
    }

    /// Build an unauthorized error.
    ///
    /// Use this when the caller's identity cannot be verified or the
    /// presented credentials are insufficient.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use eventide_application::error::AppError;
    /// use eventide_domain::error::ErrorCode;
    ///
    /// let err = AppError::unauthorized("invalid token");
    /// assert_eq!(err.code(), "UNAUTHORIZED");
    /// assert_eq!(err.http_status(), 401);
    /// ```
    #[must_use]
    pub fn unauthorized(msg: impl Into<Box<str>>) -> Self {
        Self::new(ErrorKind::Unauthorized, "UNAUTHORIZED", msg)
    }

    /// Build a "handler not found" error.
    ///
    /// Returned by buses (such as [`crate::InMemoryCommandBus`] /
    /// [`crate::InMemoryQueryBus`]) when no handler is registered for the
    /// dispatched type. The provided `handler_name` is included in the
    /// error message for diagnostics.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use eventide_application::error::AppError;
    /// use eventide_domain::error::ErrorCode;
    ///
    /// let err = AppError::handler_not_found("CreateUserHandler");
    /// assert_eq!(err.code(), "HANDLER_NOT_FOUND");
    /// ```
    #[must_use]
    pub fn handler_not_found(handler_name: &str) -> Self {
        Self::new(
            ErrorKind::Internal,
            "HANDLER_NOT_FOUND",
            format!("handler not found: {handler_name}"),
        )
    }

    /// Build an "aggregate not found" error.
    ///
    /// Use this in command/query handlers that need to load an aggregate
    /// by id and find that the underlying repository returned no record.
    /// The aggregate type and id are interpolated into the message.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use eventide_application::error::AppError;
    /// use eventide_domain::error::ErrorCode;
    ///
    /// let err = AppError::aggregate_not_found("User", "user-123");
    /// assert_eq!(err.code(), "AGGREGATE_NOT_FOUND");
    /// assert_eq!(err.http_status(), 404);
    /// ```
    #[must_use]
    pub fn aggregate_not_found(aggregate_type: &str, aggregate_id: &str) -> Self {
        Self::new(
            ErrorKind::NotFound,
            "AGGREGATE_NOT_FOUND",
            format!("{aggregate_type} not found: {aggregate_id}"),
        )
    }

    /// Build a "handler already registered" error.
    ///
    /// Returned by the in-memory buses when the caller attempts to register
    /// a second handler for a key that is already populated. Registration is
    /// intentionally exclusive to keep the dispatch deterministic.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use eventide_application::error::AppError;
    /// use eventide_domain::error::ErrorCode;
    ///
    /// let err = AppError::handler_already_registered("CreateUserHandler");
    /// assert_eq!(err.code(), "HANDLER_ALREADY_REGISTERED");
    /// ```
    #[must_use]
    pub fn handler_already_registered(handler_name: &str) -> Self {
        Self::new(
            ErrorKind::Internal,
            "HANDLER_ALREADY_REGISTERED",
            format!("handler already registered: {handler_name}"),
        )
    }

    /// Build a "type mismatch" error.
    ///
    /// Used by the type-erased buses when a dynamically dispatched value
    /// cannot be downcast back to its expected concrete type. This usually
    /// indicates a registry corruption or a programming error rather than a
    /// runtime user-facing condition.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use eventide_application::error::AppError;
    /// use eventide_domain::error::ErrorCode;
    ///
    /// let err = AppError::type_mismatch("String", "i32");
    /// assert_eq!(err.code(), "TYPE_MISMATCH");
    /// ```
    #[must_use]
    pub fn type_mismatch(expected: &str, found: &str) -> Self {
        Self::new(
            ErrorKind::Internal,
            "TYPE_MISMATCH",
            format!("type mismatch: expected={expected}, found={found}"),
        )
    }

    /// Build a generic internal error.
    ///
    /// Use this as a last resort when the failure cannot be expressed by a
    /// more specific constructor. Maps to HTTP 500.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use eventide_application::error::AppError;
    /// use eventide_domain::error::ErrorCode;
    ///
    /// let err = AppError::internal("unexpected state");
    /// assert_eq!(err.code(), "INTERNAL_ERROR");
    /// assert_eq!(err.http_status(), 500);
    /// ```
    #[must_use]
    pub fn internal(msg: impl Into<Box<str>>) -> Self {
        Self::new(ErrorKind::Internal, "INTERNAL_ERROR", msg)
    }

    // ==================== Inspection methods ====================

    /// Return the [`ErrorKind`] category of this error.
    #[must_use]
    pub fn kind(&self) -> ErrorKind {
        self.kind
    }

    /// Return a reference to the wrapped [`DomainError`], if this `AppError`
    /// originated from a domain-layer failure via `From<DomainError>`.
    #[must_use]
    pub fn domain_error(&self) -> Option<&DomainError> {
        match &self.source {
            Some(Source::Domain(e)) => Some(e),
            _ => None,
        }
    }

    /// Return a reference to the inner source error, regardless of whether
    /// it originated as a [`DomainError`] or as an arbitrary error wrapped
    /// via [`AppError::wrap`].
    #[must_use]
    pub fn get_ref(&self) -> Option<&(dyn StdError + Send + Sync + 'static)> {
        match &self.source {
            Some(Source::Domain(e)) => Some(e),
            Some(Source::Other(e)) => Some(e.as_ref()),
            None => None,
        }
    }

    /// Attempt to downcast the inner source to a specific error type.
    ///
    /// Supports retrieval of the original concrete error from sources
    /// created with [`DomainError::custom`] or [`AppError::wrap`].
    #[must_use]
    pub fn downcast_ref<E: StdError + 'static>(&self) -> Option<&E> {
        match &self.source {
            Some(Source::Domain(e)) => e.downcast_ref(),
            Some(Source::Other(e)) => e.downcast_ref(),
            None => None,
        }
    }

    /// Wrap an arbitrary error into an `AppError`.
    ///
    /// The wrapped error's type information is preserved and can be
    /// recovered later with [`AppError::downcast_ref`]. The error's
    /// `Display` value is used as the human-readable message.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use eventide_application::error::AppError;
    /// use eventide_domain::error::ErrorKind;
    /// use std::io;
    ///
    /// let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
    /// let err = AppError::wrap(ErrorKind::Internal, "IO_ERROR", io_err);
    ///
    /// assert!(err.downcast_ref::<io::Error>().is_some());
    /// ```
    #[must_use]
    pub fn wrap<E: StdError + Send + Sync + 'static>(
        kind: ErrorKind,
        code: &'static str,
        error: E,
    ) -> Self {
        Self {
            kind,
            code,
            message: error.to_string().into(),
            source: Some(Source::Other(Box::new(error))),
        }
    }

    /// Test whether this error matches the given kind/code pair.
    ///
    /// Useful in tests and conditional handling code that needs to react
    /// to a specific error variant without inspecting the message text.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use eventide_application::error::AppError;
    /// use eventide_domain::error::ErrorKind;
    ///
    /// let err = AppError::validation("invalid email");
    ///
    /// assert!(err.matches(ErrorKind::InvalidValue, "VALIDATION_ERROR"));
    /// assert!(!err.matches(ErrorKind::NotFound, "VALIDATION_ERROR"));
    /// ```
    #[must_use]
    pub fn matches(&self, kind: ErrorKind, code: &str) -> bool {
        self.kind == kind && self.code == code
    }
}

// ==================== Trait implementations ====================

impl ErrorCode for AppError {
    fn kind(&self) -> ErrorKind {
        self.kind
    }

    fn code(&self) -> &str {
        self.code
    }
}

impl fmt::Debug for AppError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AppError")
            .field("kind", &self.kind)
            .field("code", &self.code)
            .field("message", &self.message)
            .field("source", &self.source.as_ref().map(|_| "..."))
            .finish()
    }
}

impl fmt::Display for AppError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl StdError for AppError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match &self.source {
            Some(Source::Domain(e)) => Some(e),
            Some(Source::Other(e)) => Some(e.as_ref()),
            None => None,
        }
    }
}

impl From<DomainError> for AppError {
    fn from(e: DomainError) -> Self {
        // Use static_code() so that any custom code attached to the
        // DomainError is propagated verbatim into the AppError.
        let code = e.static_code();
        Self {
            kind: e.kind(),
            code,
            message: e.to_string().into(),
            source: Some(Source::Domain(e)),
        }
    }
}

// ==================== Result alias ====================

/// Application-layer `Result` alias using [`AppError`] as the error type.
pub type AppResult<T> = Result<T, AppError>;

// ==================== Tests ====================

#[cfg(test)]
mod tests {
    use super::*;

    // Verify that the convenience constructors produce errors with the
    // expected kind/code/message tuple.
    #[test]
    fn test_app_error_convenience_methods() {
        let err = AppError::validation("test");
        assert_eq!(err.kind(), ErrorKind::InvalidValue);
        assert_eq!(err.code(), "VALIDATION_ERROR");
        assert_eq!(err.to_string(), "test");

        let err = AppError::unauthorized("no token");
        assert_eq!(err.kind(), ErrorKind::Unauthorized);
        assert_eq!(err.code(), "UNAUTHORIZED");

        let err = AppError::handler_not_found("TestHandler");
        assert_eq!(err.kind(), ErrorKind::Internal);
        assert_eq!(err.code(), "HANDLER_NOT_FOUND");
    }

    // Verify that `From<DomainError>` preserves both the kind and the
    // ability to recover the original DomainError reference.
    #[test]
    fn test_from_domain_error() {
        let domain_err = DomainError::not_found("user 123");
        let app_err: AppError = domain_err.into();

        assert_eq!(app_err.kind(), ErrorKind::NotFound);
        assert!(app_err.domain_error().is_some());
    }

    // Verify that AppError implements ErrorCode and exposes the expected
    // HTTP status / retryability semantics.
    #[test]
    fn test_app_error_implements_error_code() {
        let err = AppError::validation("invalid input");

        assert_eq!(err.kind(), ErrorKind::InvalidValue);
        assert_eq!(err.code(), "VALIDATION_ERROR");
        assert_eq!(err.http_status(), 400);
        assert!(!err.is_retryable());
    }

    // Verify the `aggregate_not_found` convenience constructor formats the
    // message correctly and exposes the NotFound / 404 mapping.
    #[test]
    fn test_aggregate_not_found() {
        let err = AppError::aggregate_not_found("User", "user-123");
        assert_eq!(err.kind(), ErrorKind::NotFound);
        assert_eq!(err.code(), "AGGREGATE_NOT_FOUND");
        assert_eq!(err.http_status(), 404);
        assert!(err.to_string().contains("User"));
        assert!(err.to_string().contains("user-123"));
    }

    // Verify that `From<DomainError>` keeps a custom code attached via
    // `with_code`, instead of overriding it with a generic kind-based code.
    #[test]
    fn test_from_domain_error_preserves_custom_code() {
        let domain_err = DomainError::not_found("user 123").with_code("USER_NOT_FOUND");
        let app_err: AppError = domain_err.into();

        assert_eq!(app_err.kind(), ErrorKind::NotFound);
        assert_eq!(app_err.code(), "USER_NOT_FOUND"); // custom code is preserved
    }

    // Verify `wrap` preserves the underlying error type so it can be
    // recovered later through downcast_ref / get_ref.
    #[test]
    fn test_wrap_preserves_error() {
        use std::io;

        let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
        let err = AppError::wrap(ErrorKind::Internal, "IO_ERROR", io_err);

        assert_eq!(err.code(), "IO_ERROR");
        assert!(err.downcast_ref::<io::Error>().is_some());
        assert!(err.get_ref().is_some());
    }

    // Verify that errors smuggled through DomainError::custom into an
    // AppError can still be downcast back to their original concrete type.
    #[test]
    fn test_downcast_ref_through_domain_error() {
        use std::io;

        let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
        let domain_err = DomainError::custom(ErrorKind::Internal, io_err);
        let app_err: AppError = domain_err.into();

        // Recover the original io::Error through the AppError surface.
        assert!(app_err.downcast_ref::<io::Error>().is_some());
    }

    // Verify the `matches` helper performs strict (kind, code) equality.
    #[test]
    fn test_matches() {
        let err = AppError::validation("invalid email");
        assert!(err.matches(ErrorKind::InvalidValue, "VALIDATION_ERROR"));
        assert!(!err.matches(ErrorKind::NotFound, "VALIDATION_ERROR"));
        assert!(!err.matches(ErrorKind::InvalidValue, "WRONG_CODE"));

        let err = AppError::handler_not_found("TestHandler");
        assert!(err.matches(ErrorKind::Internal, "HANDLER_NOT_FOUND"));
    }
}