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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// 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_proto::TryFromProtoError;
use mz_sql::catalog::CatalogError as SqlCatalogError;
use mz_sql::session::vars::VarError;

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

#[derive(Debug, thiserror::Error)]
pub enum ErrorKind {
    #[error("corrupt catalog: {detail}")]
    Corruption { detail: String },
    #[error("oid counter overflows i64")]
    OidExhaustion,
    #[error(transparent)]
    Sql(#[from] SqlCatalogError),
    #[error("unacceptable schema name '{0}'")]
    ReservedSchemaName(String),
    #[error("role name {} is reserved", .0.quoted())]
    ReservedRoleName(String),
    #[error("role name {} is reserved", .0.quoted())]
    ReservedSystemRoleName(String),
    #[error("role name {} cannot be granted", .0.quoted())]
    UngrantableRoleName(String),
    #[error("cluster name {} is reserved", .0.quoted())]
    ReservedClusterName(String),
    #[error("network policy name {} is reserved", .0.quoted())]
    ReservedNetworkPolicyName(String),
    #[error("system network policy '{0}' cannot be modified")]
    ReadOnlyNetworkPolicy(String),
    #[error("replica name {} is reserved", .0.quoted())]
    ReservedReplicaName(String),
    #[error("system cluster '{0}' cannot be modified")]
    ReadOnlyCluster(String),
    #[error("system cluster replica '{0}' cannot be modified")]
    ReadOnlyClusterReplica(String),
    #[error("system database '{0}' cannot be modified")]
    ReadOnlyDatabase(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 },
    /// Attempted to create an object that has unstable dependencies.
    #[error("cannot create {object_type} with unstable dependencies")]
    UnstableDependency {
        object_type: String,
        unstable_dependencies: Vec<String>,
    },
    #[error(transparent)]
    AmbiguousRename(#[from] AmbiguousRename),
    #[error("cannot rename type: {0}")]
    TypeRename(String),
    #[error("cannot rename schemas in the ambient database: {}", .0.quoted())]
    AmbientSchemaRename(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)]
    Durable(#[from] crate::durable::DurableCatalogError),
    #[error(transparent)]
    Uuid(#[from] uuid::Error),
    #[error("role \"{role_name}\" is a member of role \"{member_name}\"")]
    CircularRoleMembership {
        role_name: String,
        member_name: String,
    },
    #[error("cluster '{0}' is managed and cannot be directly modified")]
    ManagedCluster(String),
    #[error(transparent)]
    VarError(#[from] VarError),
    #[error("unknown cluster replica size {size}")]
    InvalidClusterReplicaSize { size: String, expected: Vec<String> },
    #[error("internal error: {0}")]
    Internal(String),
}

impl Error {
    pub 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 role \"public\" and the prefixes \"mz_\" and \"pg_\" are reserved for system roles.".into())
            }
            ErrorKind::ReservedSystemRoleName(_) => {
                Some("The role prefixes \"mz_\" and \"pg_\" are reserved for system roles.".into())
            }
            ErrorKind::ReservedClusterName(_) => {
                Some("The prefixes \"mz_\" and \"pg_\" are reserved for system clusters.".into())
            }
            ErrorKind::UnstableDependency {unstable_dependencies, ..} => Some(
                format!(
                    "The object depends on the following unstable objects:\n    {}",
                    unstable_dependencies.join("\n    "),
                )
            ),
            ErrorKind::VarError(e) => e.detail(),
            _ => None,
        }
    }

    /// Reports a hint for the user about how the error could be fixed.
    pub fn hint(&self) -> Option<String> {
        match &self.kind {
            ErrorKind::VarError(e) => e.hint(),
            ErrorKind::InvalidClusterReplicaSize { expected, .. } => Some(format!(
                "Valid cluster replica sizes are: {}",
                expected.join(", ")
            )),
            _ => None,
        }
    }
}

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

impl From<TryFromProtoError> for Error {
    fn from(e: TryFromProtoError) -> Error {
        Error::from(crate::durable::CatalogError::from(e))
    }
}

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

impl From<crate::durable::CatalogError> for Error {
    fn from(e: crate::durable::CatalogError) -> Self {
        match e {
            crate::durable::CatalogError::Catalog(e) => Error::new(ErrorKind::from(e)),
            crate::durable::CatalogError::Durable(e) => Error::new(ErrorKind::from(e)),
        }
    }
}

impl From<VarError> for Error {
    fn from(e: VarError) -> Self {
        let kind: ErrorKind = e.into();
        kind.into()
    }
}

#[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
    }
}