grid_sdk/migrations/
error.rs

1// Copyright 2018-2020 Cargill Incorporated
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
15use std::error::Error;
16use std::fmt;
17
18#[cfg(feature = "diesel")]
19use crate::error::ConstraintViolationType;
20use crate::error::{ConstraintViolationError, InternalError, ResourceTemporarilyUnavailableError};
21
22#[derive(Debug)]
23pub enum MigrationsError {
24    InternalError(InternalError),
25    ConstraintViolationError(ConstraintViolationError),
26    ResourceTemporarilyUnavailableError(ResourceTemporarilyUnavailableError),
27    MigrationError(Box<dyn Error>),
28}
29
30impl Error for MigrationsError {
31    fn source(&self) -> Option<&(dyn Error + 'static)> {
32        match self {
33            MigrationsError::InternalError(err) => Some(err),
34            MigrationsError::ConstraintViolationError(err) => Some(err),
35            MigrationsError::ResourceTemporarilyUnavailableError(err) => Some(err),
36            MigrationsError::MigrationError(e) => Some(&**e),
37        }
38    }
39}
40
41impl fmt::Display for MigrationsError {
42    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43        match self {
44            MigrationsError::InternalError(err) => err.fmt(f),
45            MigrationsError::ConstraintViolationError(err) => err.fmt(f),
46            MigrationsError::ResourceTemporarilyUnavailableError(err) => err.fmt(f),
47            MigrationsError::MigrationError(e) => write!(f, "Unable to migrate database: {}", e),
48        }
49    }
50}
51
52#[cfg(feature = "diesel")]
53impl From<diesel::ConnectionError> for MigrationsError {
54    fn from(err: diesel::ConnectionError) -> Self {
55        MigrationsError::ResourceTemporarilyUnavailableError(
56            ResourceTemporarilyUnavailableError::from_source(Box::new(err)),
57        )
58    }
59}
60
61#[cfg(feature = "diesel")]
62impl From<diesel_migrations::RunMigrationsError> for MigrationsError {
63    fn from(err: diesel_migrations::RunMigrationsError) -> Self {
64        MigrationsError::MigrationError(Box::new(err))
65    }
66}
67
68#[cfg(feature = "diesel")]
69impl From<diesel::result::Error> for MigrationsError {
70    fn from(err: diesel::result::Error) -> Self {
71        match err {
72            diesel::result::Error::DatabaseError(
73                diesel::result::DatabaseErrorKind::UniqueViolation,
74                _,
75            ) => MigrationsError::ConstraintViolationError(
76                ConstraintViolationError::from_source_with_violation_type(
77                    ConstraintViolationType::Unique,
78                    Box::new(err),
79                ),
80            ),
81            diesel::result::Error::DatabaseError(
82                diesel::result::DatabaseErrorKind::ForeignKeyViolation,
83                _,
84            ) => MigrationsError::ConstraintViolationError(
85                ConstraintViolationError::from_source_with_violation_type(
86                    ConstraintViolationType::ForeignKey,
87                    Box::new(err),
88                ),
89            ),
90            _ => MigrationsError::InternalError(InternalError::from_source(Box::new(err))),
91        }
92    }
93}