grid_sdk/error/mod.rs
1// Copyright 2018-2020 Cargill Incorporated
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//! Common set of basic errors used throughout the library.
16//!
17//! The errors in this module are intended to be used by themselves or as part of a more complex
18//! error `enum`.
19//!
20//! # Examples
21//!
22//! ## Returning an Error from a Function
23//!
24//! A function may return an error such as `InternalError` by itself.
25//!
26//! ```
27//! use std::fs;
28//!
29//! use grid_sdk::error::InternalError;
30//!
31//! fn check_path(path: &str) -> Result<bool, InternalError> {
32//! let metadata = fs::metadata(path).map_err(|e| InternalError::from_source(Box::new(e)))?;
33//! Ok(metadata.is_file())
34//! }
35//! ```
36//!
37//! ## Constructing Complex Errors
38//!
39//! Errors such as `InternalError` may be used to construct more complicated errors by defining
40//! an `enum`.
41//!
42//! ```
43//! use std::error;
44//! use std::fmt;
45//! use std::fs;
46//!
47//! use grid_sdk::error::InternalError;
48//!
49//! #[derive(Debug)]
50//! enum MyError {
51//! InternalError(InternalError),
52//! MissingFilenameExtension,
53//! }
54//!
55//! impl error::Error for MyError {}
56//!
57//! impl fmt::Display for MyError {
58//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59//! match self {
60//! MyError::InternalError(e) => write!(f, "{}", e),
61//! MyError::MissingFilenameExtension => write!(f, "Missing filename extension"),
62//! }
63//! }
64//! }
65//!
66//! fn check_path(path: &str) -> Result<bool, MyError> {
67//! match !path.ends_with(".md") {
68//! true => Err(MyError::MissingFilenameExtension),
69//! false => {
70//! let metadata = fs::metadata(path).map_err(|e| MyError::InternalError(InternalError::from_source(Box::new(e))))?;
71//! Ok(metadata.is_file())
72//! }
73//! }
74//! }
75//! ```
76
77#[cfg(feature = "client")]
78mod client;
79mod constraint_violation;
80mod internal;
81mod invalid_argument;
82mod invalid_state;
83mod unavailable;
84
85#[cfg(feature = "client")]
86pub use client::ClientError;
87pub use constraint_violation::{ConstraintViolationError, ConstraintViolationType};
88pub use internal::InternalError;
89pub use invalid_argument::InvalidArgumentError;
90pub use invalid_state::InvalidStateError;
91pub use unavailable::ResourceTemporarilyUnavailableError;