parsec_client/
error.rs

1// Copyright 2020 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3//! Error types specific to the Parsec client
4use parsec_interface::requests::ResponseStatus;
5use std::error;
6use std::fmt;
7
8/// Enum used to denote errors returned to the library user
9#[derive(Debug)]
10pub enum Error {
11    /// Errors originating in the service
12    Service(ResponseStatus),
13    /// Errors originating in the client
14    Client(ClientErrorKind),
15}
16
17impl fmt::Display for Error {
18    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19        match self {
20            Error::Service(response_status) => response_status.fmt(f),
21            Error::Client(client_error_kind) => client_error_kind.fmt(f),
22        }
23    }
24}
25impl error::Error for Error {}
26
27/// Types of errors local to the client library
28#[derive(Debug)]
29pub enum ClientErrorKind {
30    /// Errors generated by the Parsec interface library
31    Interface(ResponseStatus),
32    /// Errors generated by interacting with the underlying IPC mechanism
33    Ipc(::std::io::Error),
34    /// The opcode of the response does not match the opcode of the request
35    InvalidServiceResponseType,
36    /// The operation is not supported by the selected provider
37    InvalidProvider,
38    /// Client is missing an implicit provider
39    NoProvider,
40    /// Service is missing authenticator or none of the authenticators is supported
41    /// by the client
42    NoAuthenticator,
43    /// Required parameter was not provided
44    MissingParam,
45    /// The requested resource was not found.
46    NotFound,
47    /// The socket address provided is not valid
48    InvalidSocketAddress,
49    /// The socket URL is invalid
50    InvalidSocketUrl,
51    /// Error while using the SPIFFE Workload API
52    #[cfg(feature = "spiffe-auth")]
53    Spiffe(spiffe::workload_api::client::ClientError),
54}
55
56impl From<ClientErrorKind> for Error {
57    fn from(client_error: ClientErrorKind) -> Self {
58        Error::Client(client_error)
59    }
60}
61
62impl From<url::ParseError> for Error {
63    fn from(_: url::ParseError) -> Self {
64        Error::Client(ClientErrorKind::InvalidSocketUrl)
65    }
66}
67
68impl fmt::Display for ClientErrorKind {
69    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70        match self {
71            ClientErrorKind::Interface(response_status) => response_status.fmt(f),
72            ClientErrorKind::Ipc(error) => error.fmt(f),
73            ClientErrorKind::InvalidServiceResponseType => write!(
74                f,
75                "the opcode of the response does not match the opcode of the request"
76            ),
77            ClientErrorKind::InvalidProvider => {
78                write!(f, "operation not supported by selected provider")
79            }
80            ClientErrorKind::NoProvider => write!(f, "client is missing an implicit provider"),
81            ClientErrorKind::NoAuthenticator => write!(f, "service is not reporting any authenticators or none of the reported ones are supported by the client"),
82            ClientErrorKind::MissingParam => write!(f, "one of the `Option` parameters was required but was not provided"),
83            ClientErrorKind::NotFound => write!(f, "one of the resources required in the operation was not found"),
84            ClientErrorKind::InvalidSocketAddress => write!(f, "the socket address provided in the URL is not valid"),
85            ClientErrorKind::InvalidSocketUrl => write!(f, "the socket URL is invalid"),
86            #[cfg(feature = "spiffe-auth")]
87            ClientErrorKind::Spiffe(error) => error.fmt(f),
88        }
89    }
90}
91
92/// Result type used for the internals and interface of the Parsec client
93pub type Result<T> = ::std::result::Result<T, Error>;