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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// 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.

use std::fmt;

use mz_ore::str::StrExt;
use mz_sql::catalog::CatalogError as SqlCatalogError;

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct Error {
    #[from]
    pub(crate) kind: ErrorKind,
}

#[derive(Debug, thiserror::Error)]
pub enum ErrorKind {
    #[error("corrupt catalog: {detail}")]
    Corruption { detail: String },
    #[error("id counter overflows i64")]
    IdExhaustion,
    #[error("oid counter overflows i64")]
    OidExhaustion,
    #[error(transparent)]
    Sql(#[from] SqlCatalogError),
    #[error("database '{0}' already exists")]
    DatabaseAlreadyExists(String),
    #[error("schema '{0}' already exists")]
    SchemaAlreadyExists(String),
    #[error("role '{0}' already exists")]
    RoleAlreadyExists(String),
    #[error("cluster '{0}' already exists")]
    ClusterAlreadyExists(String),
    #[error("cannot create multiple replicas named '{0}' on cluster '{1}'")]
    DuplicateReplica(String, String),
    #[error("catalog item '{0}' already exists")]
    ItemAlreadyExists(String),
    #[error("unacceptable schema name '{0}'")]
    ReservedSchemaName(String),
    #[error("role name {} is reserved", .0.quoted())]
    ReservedRoleName(String),
    #[error("cluster name {} is reserved", .0.quoted())]
    ReservedClusterName(String),
    #[error("replica name {} is reserved", .0.quoted())]
    ReservedReplicaName(String),
    #[error("system schema '{0}' cannot be modified")]
    ReadOnlySystemSchema(String),
    #[error("system item '{0}' cannot be modified")]
    ReadOnlyItem(String),
    #[error("cannot drop non-empty schema '{0}'")]
    SchemaNotEmpty(String),
    #[error("non-temporary items cannot depend on temporary item '{0}'")]
    InvalidTemporaryDependency(String),
    #[error("cannot create temporary item in non-temporary schema")]
    InvalidTemporarySchema,
    #[error("catalog item '{depender_name}' depends on system logging, but logging is disabled")]
    UnsatisfiableLoggingDependency { depender_name: String },
    #[error("sqlite error: {0}")]
    Storage(#[from] rusqlite::Error),
    #[error(transparent)]
    AmbiguousRename(#[from] AmbiguousRename),
    #[error("cannot rename type: {0}")]
    TypeRename(String),
    #[error("cannot migrate from catalog version {last_seen_version} to version {this_version} (earlier versions might still work): {cause}")]
    FailedMigration {
        last_seen_version: String,
        this_version: &'static str,
        cause: String,
    },
    #[error("failpoint {0} reached)")]
    FailpointReached(String),
    #[error("{0}")]
    Unstructured(String),
    #[error(transparent)]
    Stash(#[from] mz_stash::StashError),
    #[error("stash in unexpected state")]
    UnexpectedStashState,
    #[error(transparent)]
    Uuid(#[from] uuid::Error),
}

impl Error {
    pub(crate) fn new(kind: ErrorKind) -> Error {
        Error { kind }
    }

    /// Reports additional details about the error, if any are available.
    pub fn detail(&self) -> Option<String> {
        match &self.kind {
            ErrorKind::ReservedSchemaName(_) => {
                Some("The prefixes \"mz_\" and \"pg_\" are reserved for system schemas.".into())
            }
            ErrorKind::ReservedRoleName(_) => {
                Some("The prefixes \"mz_\" and \"pg_\" are reserved for system roles.".into())
            }
            ErrorKind::ReservedClusterName(_) => {
                Some("The prefixes \"mz_\" and \"pg_\" are reserved for system clusters.".into())
            }
            _ => None,
        }
    }

    /// Reports a hint for the user about how the error could be fixed.
    pub fn hint(&self) -> Option<String> {
        None
    }
}

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

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

impl From<mz_stash::StashError> for Error {
    fn from(e: mz_stash::StashError) -> Error {
        Error::new(ErrorKind::from(e))
    }
}

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

#[derive(Debug)]
pub struct AmbiguousRename {
    pub depender: String,
    pub dependee: String,
    pub message: String,
}

// Implement `Display` for `MinMax`.
impl fmt::Display for AmbiguousRename {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.depender == self.dependee {
            write!(
                f,
                "renaming conflict: in {}, {}",
                self.dependee, self.message
            )
        } else {
            write!(
                f,
                "renaming conflict: in {}, which uses {}, {}",
                self.depender, self.dependee, self.message
            )
        }
    }
}

impl std::error::Error for AmbiguousRename {
    // Explicitly no source for this kind of error
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None
    }
}