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 /// Wait error.
27 #[error("wait for concurrent fetch result error: {0}")]
28 Wait(Box<dyn std::error::Error + Send + Sync + 'static>),
29}
30
31impl Error {
32 /// Combine multiple errors into one error.
33 pub fn multiple(errs: Vec<Error>) -> Self {
34 Self::Multiple(MultipleError(errs))
35 }
36
37 /// Error on waiting for concurrrent fetch result.
38 pub fn wait(err: impl std::error::Error + Send + Sync + 'static) -> Self {
39 Self::Wait(Box::new(err))
40 }
41}
42
43#[derive(thiserror::Error, Debug)]
44pub struct MultipleError(Vec<Error>);
45
46impl Display for MultipleError {
47 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48 write!(f, "multiple errors: [")?;
49 if let Some((last, errs)) = self.0.as_slice().split_last() {
50 for err in errs {
51 write!(f, "{err}, ")?;
52 }
53 write!(f, "{last}")?;
54 }
55 write!(f, "]")?;
56 Ok(())
57 }
58}
59
60/// In-memory cache result.
61pub type Result<T> = std::result::Result<T, Error>;