camel_processor/do_try.rs
1//! Shared helpers for the doTry segment (`do_try_segment.rs`).
2//!
3//! [`CatchMatcher`] matches a `doCatch` clause against a `CamelError`.
4//! `chain_predicate_error` attaches the original try error as the `cause`
5//! of a failed catch `when` / `on_when` predicate (language-value-boundary
6//! envelope). The runtime itself lives in `DoTrySegment`.
7
8use camel_api::{CamelError, Exchange, PredicateSource};
9
10/// Matcher for a `doCatch` clause.
11#[derive(Clone)]
12pub enum CatchMatcher {
13 /// Match by CamelError variant name (e.g. ["ProcessorError", "Io"]).
14 /// `"*"` matches any variant — equivalent to Camel's `doCatch(Throwable.class)`.
15 ByVariant(Vec<String>),
16 /// Match by a fallible predicate over the Exchange.
17 Predicate(PredicateSource),
18 /// Match a `CamelError::Business` whose `code` equals this value.
19 /// Non-`Business` errors never match.
20 ByCode(String),
21 /// Match a `CamelError::Business` whose `kind` equals this value.
22 /// Non-`Business` errors never match.
23 ByBusinessKind(String),
24}
25
26impl CatchMatcher {
27 /// Returns whether this matcher matches the given error and exchange.
28 ///
29 /// A failed predicate evaluation yields `Err` (language-value-boundary:
30 /// the caller chains the original error and fails the scope).
31 pub async fn matches(&self, err: &CamelError, ex: &Exchange) -> Result<bool, CamelError> {
32 match self {
33 CatchMatcher::ByVariant(names) => {
34 if names.iter().any(|n| n == "*") {
35 return Ok(true);
36 }
37 Ok(names.iter().any(|n| n == err.variant_name()))
38 }
39 CatchMatcher::Predicate(source) => source.matches(ex).await,
40 CatchMatcher::ByCode(code) => Ok(matches!(
41 err,
42 CamelError::Business {
43 code: error_code,
44 ..
45 } if error_code == code
46 )),
47 CatchMatcher::ByBusinessKind(kind) => Ok(matches!(
48 err,
49 CamelError::Business { kind: error_kind, .. } if error_kind == kind
50 )),
51 }
52 }
53}
54
55/// Chain the original try error into a failed catch-predicate error
56/// (language-value-boundary: catch `when` / `on_when` failure envelope).
57///
58/// The copy keeps every field unchanged except `cause`, which holds the
59/// original error so the chain stays retrievable. Two kinds of cause are
60/// replaced: an empty slot (`cause: None`) and a mapper-generated diagnostic
61/// cause identified by the camel-language-api provenance marker (never by
62/// variant or string shape). Genuine caller-supplied chains — including
63/// hand-built `ProcessorError`/`ProcessorErrorWithSource` causes — pass
64/// through untouched. Errors that carry no `cause` slot pass through
65/// untouched as well.
66pub(crate) fn chain_predicate_error(
67 mut predicate_err: CamelError,
68 original: CamelError,
69) -> CamelError {
70 let should_chain = match &predicate_err {
71 CamelError::ExpressionFailed { cause, .. } => {
72 cause.is_none() || camel_language_api::is_expression_detail_cause(&predicate_err)
73 }
74 _ => false,
75 };
76 if should_chain && let CamelError::ExpressionFailed { cause, .. } = &mut predicate_err {
77 *cause = Some(Box::new(original));
78 }
79 predicate_err
80}