# error.scoped.ScopeError

- **Module:** error.scoped
- **Package:** libtmux
- **Language:** Rust
- **Kind:** enum
- **Source:** https://github.com/libtmux/libtmux-rs/blob/f0e37052c232636b61d095817046e6bfc8f2ca40/crates/libtmux/src/error/scoped.rs#L51
- **Page:** https://libtmux.org/en/rs/latest/reference/error-scoped-scopeerror/

A scoped resource's creation, operation, or cleanup failure.

Returned by [`crate::Server::with_session`],
[`crate::Session::with_window`], and [`crate::Window::with_pane`]. The
operation error keeps its original type and value, with no `From<Error>`
requirement. Cleanup failures retain [`Error::AfterEffect`] because
creation succeeded; an operation error alone makes no replay guarantee.
[`Self::Cleanup`] retains the operation's own result too: it already
computed `T` when cleanup failed, and matching the variant is the only way
to reach it, since the outer `Result` is `Err` either way.

`Debug`, `Display`, and [`std::error::Error`] are implemented for every
`T` and `E`, but each only shows a value when its type supports it:
`Debug` needs `T: Debug` and `E: Debug`, `Display` needs `E: Display`, and
`Error` needs both, since it requires them as supertraits. A caller whose
types have neither still gets a working scope: both values remain
reachable by matching the variant, and creation and cleanup failures
format and chain regardless.

[`std::error::Error::source`] exposes the cleanup error in
[`Self::Cleanup`] and [`Self::OperationAndCleanup`], and the creation
error in [`Self::Creation`]. It never exposes the operation error: `E`
need not implement [`std::error::Error`] at all, so there is no
`&(dyn Error + 'static)` to hand back even when `Display` can show it.
Match the variant to reach it directly.

## Example

```rust
use libtmux::ScopeError;

let guard = libtmux::test::TestServer::new().await?;
let outcome = guard.server().with_session("work", async |_session| {
    Err::<(), _>("operation failed")
}).await;
assert!(matches!(outcome, Err(ScopeError::Operation("operation failed"))));
guard.shutdown().await?;
```

## Members

- `Creation` (constant): The resource could not be created; the operation did not run.
- `Operation` (constant): The operation failed and cleanup succeeded.
- `Cleanup` (constant): The operation succeeded, but cleanup failed after creation.
- `OperationAndCleanup` (constant): The operation and cleanup both failed.
- `into_operation` (method): Take the operation's own error, when the operation is what failed.
- `into_value` (method): Take the value the operation produced before cleanup failed.
- `tmux_error` (method): Borrow the tmux error from creation or cleanup.
