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
87
88
89
90
91
92
use std::{error, fmt};

use http_error::{HttpError, StatusCode};
use tracing::Span;

/// An error communicating with the Postgres server.
#[derive(Debug)]
pub struct Error {
    pub(crate) kind: ErrorKind,
    span: Span,
}

#[derive(Debug)]
pub enum ErrorKind {
    MissingDatabaseUrlEnv,
    RowNotFound,
    Postgres(tokio_postgres::Error),
    Build(deadpool_postgres::BuildError),
    Pool(deadpool_postgres::PoolError),
}

impl Error {
    fn new(kind: ErrorKind) -> Self {
        Self {
            kind,
            span: Span::current(),
        }
    }

    /// Whether this is a row not found error.
    pub fn is_row_not_found(&self) -> bool {
        matches!(self.kind, ErrorKind::RowNotFound)
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match &self.kind {
            ErrorKind::MissingDatabaseUrlEnv => None,
            ErrorKind::RowNotFound => None,
            ErrorKind::Postgres(err) => Some(err),
            ErrorKind::Build(err) => Some(err),
            ErrorKind::Pool(err) => Some(err),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            ErrorKind::MissingDatabaseUrlEnv => f.write_str("env DATABASE_URL not set"),
            ErrorKind::RowNotFound => f.write_str("No rows returned, but at least one expected"),
            ErrorKind::Postgres(err) => err.fmt(f),
            ErrorKind::Build(_) => write!(f, "failed to build postgres connection pool"),
            ErrorKind::Pool(_) => write!(f, "failed to acquire postgres connection from pool"),
        }
    }
}

impl HttpError for Error {
    fn status_code(&self) -> StatusCode {
        StatusCode::INTERNAL_SERVER_ERROR
    }

    fn span(&self) -> Option<&tracing::Span> {
        Some(&self.span)
    }
}

impl From<tokio_postgres::Error> for Error {
    fn from(err: tokio_postgres::Error) -> Self {
        Self::new(ErrorKind::Postgres(err))
    }
}

impl From<deadpool_postgres::BuildError> for Error {
    fn from(err: deadpool_postgres::BuildError) -> Self {
        Self::new(ErrorKind::Build(err))
    }
}

impl From<deadpool_postgres::PoolError> for Error {
    fn from(err: deadpool_postgres::PoolError) -> Self {
        Self::new(ErrorKind::Pool(err))
    }
}

impl From<ErrorKind> for Error {
    fn from(kind: ErrorKind) -> Self {
        Self::new(kind)
    }
}