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
// Copyright © 2021 Alexandra Frydl
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

pub use self::QueryOneError::NoRowsReturned;
pub use tokio_postgres::error::{DbError, ErrorPosition, Severity, SqlState};

use af_core::prelude::*;
use af_core::string::SharedString;

pub type Result<T = (), E = Error> = std::result::Result<T, E>;

#[derive(Debug, Error)]
pub enum Error {
  #[error(transparent)]
  Db(Box<DbError>),
  #[error("{0}")]
  Other(SharedString),
}

/// An error returned from a `query_one` function.
#[derive(Debug, Error)]
pub enum QueryOneError {
  /// An error indiating a statement returned no rows.
  #[error("No rows returned.")]
  NoRowsReturned,
  #[error(transparent)]
  Other(#[from] Error),
}

impl Error {
  /// Creates a new [`Error::Other`] with the given message.
  pub fn new(message: impl Into<SharedString>) -> Self {
    Self::Other(message.into())
  }
}

impl From<tokio_postgres::Error> for Error {
  fn from(err: tokio_postgres::Error) -> Self {
    match err.code() {
      Some(_) => Error::Db(std::error::Error::downcast(err.into_source().unwrap()).unwrap()),
      None => Error::Other(err.to_string().into()),
    }
  }
}