envoy_sdk/error.rs
1// Copyright 2020 Tetrate
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! The `Error` type and helpers.
16//!
17//! # Examples
18//!
19//! ```
20//! # use envoy_sdk as envoy;
21//! use envoy::error::{bail, Result};
22//!
23//! fn on_request() -> Result<()> {
24//! // ...
25//! bail!("unexpected state");
26//! }
27//! ```
28//!
29//! ```
30//! # use envoy_sdk as envoy;
31//! use envoy::error::{ensure, Result};
32//!
33//! fn on_request() -> Result<()> {
34//! # let body_len = 0;
35//! ensure!(body_len != 0, "request body must not be empty");
36//! // ...
37//! # Ok(())
38//! }
39//! ```
40//!
41//! ```
42//! # use envoy_sdk as envoy;
43//! use envoy::error::{format_err, Result};
44//!
45//! fn on_request() -> Result<()> {
46//! # let method = "";
47//! if method == "DELETE" {
48//! return Err(format_err!("{} method is not allowed", method));
49//! }
50//! // ...
51//! # Ok(())
52//! }
53//! ```
54//!
55//! ```
56//! # use envoy_sdk as envoy;
57//! use envoy::error::{ErrorContext, Result};
58//! use envoy::host::Clock;
59//!
60//! fn on_request() -> Result<()> {
61//! # let instance_id = 123;
62//! let now = Clock::default().now()
63//! .with_context(|| format!("Failed to get time of the request {}", instance_id))?;
64//! // ...
65//! # Ok(())
66//! }
67//! ```
68
69pub use anyhow::{bail, ensure, format_err};
70pub use anyhow::{Context as ErrorContext, Error, Result};