Skip to main content

perspective_client/virtual_server/
error.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use prost::{DecodeError, EncodeError};
14use thiserror::Error;
15
16/// Error type for virtual server operations.
17///
18/// This enum represents the various errors that can occur when processing
19/// requests through a [`VirtualServer`](super::VirtualServer).
20#[derive(Clone, Error, Debug)]
21pub enum VirtualServerError<T: std::fmt::Debug> {
22    #[error("External Error: {0:?}")]
23    InternalError(#[from] T),
24
25    #[error("{0}")]
26    DecodeError(DecodeError),
27
28    #[error("{0}")]
29    EncodeError(EncodeError),
30
31    #[error("Unknown view '{0}'")]
32    UnknownViewId(String),
33
34    #[error("Invalid JSON'{0}'")]
35    InvalidJSON(std::sync::Arc<serde_json::Error>),
36
37    #[error("{0}")]
38    Other(String),
39}
40
41/// Extension trait for extracting internal errors from [`VirtualServerError`]
42/// results.
43///
44/// Provides a method to distinguish between internal handler errors and other
45/// virtual server errors.
46pub trait ResultExt<X, T> {
47    fn get_internal_error(self) -> Result<X, Result<T, String>>;
48}
49
50impl<X, T: std::fmt::Debug> ResultExt<X, T> for Result<X, VirtualServerError<T>> {
51    fn get_internal_error(self) -> Result<X, Result<T, String>> {
52        match self {
53            Ok(x) => Ok(x),
54            Err(VirtualServerError::InternalError(x)) => Err(Ok(x)),
55            Err(x) => Err(Err(x.to_string())),
56        }
57    }
58}