foyer_memory/error.rs
1// Copyright 2025 foyer Project Authors
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
15use std::fmt::Display;
16
17/// In-memory cache error.
18#[derive(thiserror::Error, Debug)]
19pub enum Error {
20 /// Multiple error list.
21 #[error(transparent)]
22 Multiple(MultipleError),
23 /// Config error.
24 #[error("config error: {0}")]
25 ConfigError(String),
26}
27
28impl Error {
29 /// Combine multiple errors into one error.
30 pub fn multiple(errs: Vec<Error>) -> Self {
31 Self::Multiple(MultipleError(errs))
32 }
33}
34
35#[derive(thiserror::Error, Debug)]
36pub struct MultipleError(Vec<Error>);
37
38impl Display for MultipleError {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 write!(f, "multiple errors: [")?;
41 if let Some((last, errs)) = self.0.as_slice().split_last() {
42 for err in errs {
43 write!(f, "{}, ", err)?;
44 }
45 write!(f, "{}", last)?;
46 }
47 write!(f, "]")?;
48 Ok(())
49 }
50}
51
52/// In-memory cache result.
53pub type Result<T> = std::result::Result<T, Error>;