grid_sdk/commits/store/
error.rs

1// Copyright 2018-2021 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/// Represents CommitStore errors
23#[derive(Debug)]
24pub enum CommitStoreError {
25    InternalError(InternalError),
26    ConstraintViolationError(ConstraintViolationError),
27    ResourceTemporarilyUnavailableError(ResourceTemporarilyUnavailableError),
28    NotFoundError(String),
29}
30
31impl Error for CommitStoreError {
32    fn source(&self) -> Option<&(dyn Error + 'static)> {
33        match self {
34            CommitStoreError::InternalError(err) => Some(err),
35            CommitStoreError::ConstraintViolationError(err) => Some(err),
36            CommitStoreError::ResourceTemporarilyUnavailableError(err) => Some(err),
37            CommitStoreError::NotFoundError(_) => None,
38        }
39    }
40}
41
42impl fmt::Display for CommitStoreError {
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        match self {
45            CommitStoreError::InternalError(err) => err.fmt(f),
46            CommitStoreError::ConstraintViolationError(err) => err.fmt(f),
47            CommitStoreError::ResourceTemporarilyUnavailableError(err) => err.fmt(f),
48            CommitStoreError::NotFoundError(ref s) => write!(f, "Commit not found: {}", s),
49        }
50    }
51}
52
53#[cfg(feature = "diesel")]
54impl From<diesel::result::Error> for CommitStoreError {
55    fn from(err: diesel::result::Error) -> CommitStoreError {
56        match err {
57            diesel::result::Error::DatabaseError(
58                diesel::result::DatabaseErrorKind::UniqueViolation,
59                _,
60            ) => CommitStoreError::ConstraintViolationError(
61                ConstraintViolationError::from_source_with_violation_type(
62                    ConstraintViolationType::Unique,
63                    Box::new(err),
64                ),
65            ),
66            diesel::result::Error::DatabaseError(
67                diesel::result::DatabaseErrorKind::ForeignKeyViolation,
68                _,
69            ) => CommitStoreError::ConstraintViolationError(
70                ConstraintViolationError::from_source_with_violation_type(
71                    ConstraintViolationType::ForeignKey,
72                    Box::new(err),
73                ),
74            ),
75            _ => CommitStoreError::InternalError(InternalError::from_source(Box::new(err))),
76        }
77    }
78}
79
80#[cfg(feature = "diesel")]
81impl From<diesel::r2d2::PoolError> for CommitStoreError {
82    fn from(err: diesel::r2d2::PoolError) -> CommitStoreError {
83        CommitStoreError::ResourceTemporarilyUnavailableError(
84            ResourceTemporarilyUnavailableError::from_source(Box::new(err)),
85        )
86    }
87}