Skip to main content

pib_service_inventory/
error.rs

1// SPDX-FileCopyrightText: Politik im Blick developers
2// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
3//
4// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2
5
6use oparl_types::namespace::ErrorNamespaceUrl;
7use snafu::Snafu;
8
9#[derive(Debug, Snafu)]
10#[snafu(visibility(pub))]
11pub enum Error {
12    #[snafu(display("Entry not found"))]
13    NotFound,
14
15    #[snafu(display("Error interfacing with inventory backend"))]
16    Inventory {
17        source: Box<dyn std::error::Error + Send + Sync>,
18    },
19}
20
21impl Error {
22    pub const fn http_status_code(&self) -> u16 {
23        match self {
24            Error::NotFound => 404,
25            Error::Inventory { .. } => 500,
26        }
27    }
28
29    pub const fn is_not_found(&self) -> bool {
30        matches!(self, Self::NotFound)
31    }
32}
33
34impl From<Error> for oparl_types::Error {
35    fn from(value: Error) -> Self {
36        oparl_types::Error {
37            namespace: ErrorNamespaceUrl::Identifier,
38            message: value.to_string(),
39            debug: None,
40            extensions: serde_json::Map::new(),
41        }
42    }
43}