prefix_register/
error.rs

1// Copyright TELICENT LTD
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
15//! Error types for the prefix registry service.
16
17use std::fmt;
18
19/// Result type alias for prefix registry operations.
20pub type Result<T> = std::result::Result<T, Error>;
21
22/// Configuration error types with specific variants.
23#[derive(Debug, Clone, thiserror::Error)]
24pub enum ConfigurationError {
25    /// Max connections is invalid (must be greater than 0).
26    #[error("max_connections must be greater than 0, got {0}")]
27    InvalidMaxConnections(usize),
28
29    /// Database URL is invalid or empty.
30    #[error("database_url is invalid: {0}")]
31    InvalidDatabaseUrl(String),
32}
33
34/// Error types for prefix registry operations.
35#[derive(Debug, thiserror::Error)]
36pub enum Error {
37    /// Database operation failed.
38    #[error("Database error: {0}")]
39    Database(String),
40
41    /// Database connection pool error.
42    #[error("Connection pool error: {0}")]
43    ConnectionPool(String),
44
45    /// Invalid configuration.
46    #[error("Configuration error: {0}")]
47    Configuration(#[from] ConfigurationError),
48
49    /// Prefix validation failed.
50    #[error("Invalid prefix: {0}")]
51    InvalidPrefix(String),
52
53    /// URI validation failed.
54    #[error("Invalid URI: {0}")]
55    InvalidUri(String),
56}
57
58impl Error {
59    /// Create a database error from any Display type.
60    pub fn database(msg: impl fmt::Display) -> Self {
61        Self::Database(msg.to_string())
62    }
63
64    /// Create a connection pool error.
65    pub fn connection_pool(msg: impl fmt::Display) -> Self {
66        Self::ConnectionPool(msg.to_string())
67    }
68
69    /// Create an invalid prefix error.
70    pub fn invalid_prefix(msg: impl fmt::Display) -> Self {
71        Self::InvalidPrefix(msg.to_string())
72    }
73
74    /// Create an invalid URI error.
75    pub fn invalid_uri(msg: impl fmt::Display) -> Self {
76        Self::InvalidUri(msg.to_string())
77    }
78}
79
80impl From<tokio_postgres::Error> for Error {
81    fn from(e: tokio_postgres::Error) -> Self {
82        Self::Database(e.to_string())
83    }
84}
85
86impl From<deadpool_postgres::PoolError> for Error {
87    fn from(e: deadpool_postgres::PoolError) -> Self {
88        Self::ConnectionPool(e.to_string())
89    }
90}
91
92impl From<deadpool_postgres::BuildError> for Error {
93    fn from(e: deadpool_postgres::BuildError) -> Self {
94        Self::ConnectionPool(format!("Pool build error: {}", e))
95    }
96}
97
98impl From<deadpool_postgres::CreatePoolError> for Error {
99    fn from(e: deadpool_postgres::CreatePoolError) -> Self {
100        Self::ConnectionPool(format!("Pool creation error: {}", e))
101    }
102}