1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! [PostgresClient](crate::PostgresClient)-related errors.

use std::fmt;

/// An error coming from Postgres.
#[derive(Debug)]
pub enum PostgresError {
    /// A determinate error from Postgres.
    Determinate(anyhow::Error),
    /// An indeterminate error from Postgres.
    Indeterminate(anyhow::Error),
}

impl std::fmt::Display for PostgresError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PostgresError::Determinate(x) => std::fmt::Display::fmt(x, f),
            PostgresError::Indeterminate(x) => std::fmt::Display::fmt(x, f),
        }
    }
}

impl std::error::Error for PostgresError {}

/// An impl of PartialEq purely for convenience in tests and debug assertions.
#[cfg(any(test, debug_assertions))]
impl PartialEq for PostgresError {
    fn eq(&self, other: &Self) -> bool {
        self.to_string() == other.to_string()
    }
}

impl From<anyhow::Error> for PostgresError {
    fn from(inner: anyhow::Error) -> Self {
        PostgresError::Indeterminate(inner)
    }
}

impl From<std::io::Error> for PostgresError {
    fn from(x: std::io::Error) -> Self {
        PostgresError::Indeterminate(anyhow::Error::new(x))
    }
}

impl From<deadpool_postgres::tokio_postgres::Error> for PostgresError {
    fn from(e: deadpool_postgres::tokio_postgres::Error) -> Self {
        let code = match e.as_db_error().map(|x| x.code()) {
            Some(x) => x,
            None => return PostgresError::Indeterminate(anyhow::Error::new(e)),
        };
        match code {
            // Feel free to add more things to this allowlist as we encounter
            // them as long as you're certain they're determinate.
            &deadpool_postgres::tokio_postgres::error::SqlState::T_R_SERIALIZATION_FAILURE => {
                PostgresError::Determinate(anyhow::Error::new(e))
            }
            _ => PostgresError::Indeterminate(anyhow::Error::new(e)),
        }
    }
}

impl From<deadpool_postgres::PoolError> for PostgresError {
    fn from(x: deadpool_postgres::PoolError) -> Self {
        match x {
            // We have logic for turning a postgres Error into an ExternalError,
            // so use it.
            deadpool_postgres::PoolError::Backend(x) => PostgresError::from(x),
            x => PostgresError::Indeterminate(anyhow::Error::new(x)),
        }
    }
}

impl From<tokio::task::JoinError> for PostgresError {
    fn from(x: tokio::task::JoinError) -> Self {
        PostgresError::Indeterminate(anyhow::Error::new(x))
    }
}