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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use crate::UserFacingError;
use serde::Serialize;
use std::fmt::Display;
use user_facing_error_macros::*;

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(
    code = "P1000",
    message = "\
Authentication failed against database server at `{database_host}`, the provided database credentials for `{database_user}` are not valid.

Please make sure to provide valid database credentials for the database server at `{database_host}`."
)]
// **Notes**: Might vary for different data source, For example, SQLite has no concept of user accounts, and instead relies on the file system for all database permissions. This makes enforcing storage quotas difficult and enforcing user permissions impossible.
pub struct IncorrectDatabaseCredentials {
    /// Database host URI
    pub database_user: String,

    /// Database user name
    pub database_host: String,
}

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(
    code = "P1001",
    message = "\
Can't reach database server at `{database_host}`:`{database_port}`

Please make sure your database server is running at `{database_host}`:`{database_port}`."
)]
pub struct DatabaseNotReachable {
    /// Database host URI
    pub database_host: String,

    /// Database port
    pub database_port: u16,
}

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(
    code = "P1002",
    message = "\
The database server at `{database_host}`:`{database_port}` was reached but timed out.

Please try again.

Please make sure your database server is running at `{database_host}`:`{database_port}`.

Context: {context}
"
)]
pub struct DatabaseTimeout {
    /// Database host URI
    pub database_host: String,

    /// Database port
    pub database_port: String,

    /// Extra context
    pub context: String,
}

#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum DatabaseDoesNotExist {
    Sqlite {
        database_file_name: String,
        database_file_path: String,
    },
    Postgres {
        database_name: String,
        database_host: String,
        database_port: u16,
    },
    Mysql {
        database_name: String,
        database_host: String,
        database_port: u16,
    },

    Mssql {
        database_name: String,
        database_host: String,
        database_port: u16,
    },
}

impl UserFacingError for DatabaseDoesNotExist {
    const ERROR_CODE: &'static str = "P1003";

    fn message(&self) -> String {
        match self {
            DatabaseDoesNotExist::Sqlite {
                database_file_name,
                database_file_path,
            } => format!(
                "Database {database_file_name} does not exist at {database_file_path}"
            ),
            DatabaseDoesNotExist::Postgres {
                database_name,
                database_host,
                database_port,
            } => format!(
                "Database `{database_name}` does not exist on the database server at `{database_host}:{database_port}`.",
            ),
            DatabaseDoesNotExist::Mysql {
                database_name,
                database_host,
                database_port,
            } => format!(
                "Database `{database_name}` does not exist on the database server at `{database_host}:{database_port}`.",
            ),
            DatabaseDoesNotExist::Mssql {
                database_name,
                database_host,
                database_port,
            } => format!(
                "Database `{database_name}` does not exist on the database server at `{database_host}:{database_port}`.",
            ),
        }
    }
}

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(code = "P1008", message = "Operations timed out after `{time}`. Context: {context}")]
pub struct DatabaseOperationTimeout {
    /// Operation time in s or ms (if <1000ms)
    pub time: String,

    /// Extra context
    pub context: String,
}

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(
    code = "P1009",
    message = "Database `{database_name}` already exists on the database server at `{database_host}:{database_port}`"
)]
pub struct DatabaseAlreadyExists {
    /// Database name, append `database_schema_name` when applicable
    /// `database_schema_name`: Database schema name (For Postgres for example)
    pub database_name: String,

    /// Database host URI
    pub database_host: String,

    /// Database port
    pub database_port: u16,
}

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(
    code = "P1010",
    message = "User `{database_user}` was denied access on the database `{database_name}`"
)]
pub struct DatabaseAccessDenied {
    /// Database user name
    pub database_user: String,

    /// Database name, append `database_schema_name` when applicable
    /// `database_schema_name`: Database schema name (For Postgres for example)
    pub database_name: String,
}

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(code = "P1011", message = "Error opening a TLS connection: {message}")]
pub struct TlsConnectionError {
    pub message: String,
}

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(code = "P1012", message = "{full_error}")]
pub struct SchemaParserError {
    pub full_error: String,
}

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(code = "P1013", message = "The provided database string is invalid. {details}")]
pub struct InvalidConnectionString {
    pub details: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub enum ModelKind {
    Table,
}

impl Display for ModelKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Table => write!(f, "table"),
        }
    }
}

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(
    code = "P1014",
    message = "The underlying {kind} for model `{model}` does not exist."
)]
pub struct InvalidModel {
    pub model: String,
    pub kind: ModelKind,
}

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(
    code = "P1015",
    message = "Your Prisma schema is using features that are not supported for the version of the database.\nDatabase version: {database_version}\nErrors:\n{errors}"
)]
pub struct DatabaseVersionIncompatibility {
    pub database_version: String,
    pub errors: String,
}

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(
    code = "P1016",
    message = "Your raw query had an incorrect number of parameters. Expected: `{expected}`, actual: `{actual}`."
)]
pub struct IncorrectNumberOfParameters {
    pub expected: usize,
    pub actual: usize,
}

#[derive(Debug, SimpleUserFacingError)]
#[user_facing(code = "P1017", message = "Server has closed the connection.")]
pub struct ConnectionClosed;

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(code = "P1018", message = "{message}")]
pub struct TransactionAlreadyClosed {
    pub message: String,
}

#[derive(Debug, UserFacingError, Serialize)]
#[user_facing(code = "P1019", message = "{message}")]
pub struct UnsupportedFeatureError {
    pub message: String,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::UserFacingError;

    #[test]
    fn database_does_not_exist_formats_properly() {
        let sqlite_err = DatabaseDoesNotExist::Sqlite {
            database_file_path: "/tmp/dev.db".into(),
            database_file_name: "dev.db".into(),
        };

        assert_eq!(sqlite_err.message(), "Database dev.db does not exist at /tmp/dev.db");

        let mysql_err = DatabaseDoesNotExist::Mysql {
            database_name: "root".into(),
            database_host: "localhost".into(),
            database_port: 8888,
        };

        assert_eq!(
            mysql_err.message(),
            "Database `root` does not exist on the database server at `localhost:8888`."
        );
    }
}