exception

Macro exception 

Source
macro_rules! exception {
    ($exception_type:expr, $error:expr $(, $key:literal = $value:expr)* $(,)?) => { ... };
}
Expand description

Records an exception event on the current span and returns an error value.

This macro adds an exception event to the current OpenTelemetry span following the semantic conventions, then evaluates to the provided error value.

§Syntax

// In closures (ok_or_else, map_err, etc.)
.ok_or_else(|| otel::exception!("invalid_params", Error::new("msg")))?

// In function bodies with explicit return
return Err(otel::exception!("invalid_params", Error::new("msg")));

// With additional attributes
otel::exception!("source_not_found", error, "uri" => uri.to_string())

§Parameters

  • exception_type: String literal for the exception.type attribute
  • error_value: The error value to return (must implement Display for exception.message)
  • Additional key-value pairs: Optional extra attributes to add to the exception event

§Examples

// In ok_or_else closure
let arg = arguments.first().ok_or_else(|| {
    otel::exception!("invalid_params",
        jsonrpc::Error::invalid_params("Missing arguments"))
})?;

// In map_err closure
let uri = Uri::parse(uri_str).map_err(|e| {
    otel::exception!("invalid_uri",
        jsonrpc::Error::invalid_params(format!("Invalid URI: {}", e)),
        "uri_str" => uri_str.to_string()
    )
})?;

// Direct return
if cache.is_empty() {
    return Err(otel::exception!("cache_empty",
        jsonrpc::Error::internal_error("Cache is empty"),
        "operation" => "lookup"
    ));
}

§OpenTelemetry Semantic Conventions

The macro follows the OpenTelemetry semantic conventions for exceptions:

  • Event name: "exception"
  • Required attributes:
    • exception.type: The type/category of the exception
    • exception.message: The error message extracted via Display
  • Additional attributes: Any extra key-value pairs provided