Skip to main content

tauri_plugin_sql/
error.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5use serde::{Serialize, Serializer};
6
7/// Errors that can happen while connecting to a database, running migrations
8/// or executing a query.
9///
10/// Serializes to its [`std::fmt::Display`] representation, which is what the
11/// frontend receives when a command fails.
12#[derive(Debug, thiserror::Error)]
13pub enum Error {
14    /// An error reported by [`sqlx`], such as a failed connection or a query the database rejected.
15    #[error(transparent)]
16    Sql(#[from] sqlx::Error),
17    /// A migration registered with [`crate::Builder::add_migrations`] could not be resolved or applied.
18    #[error(transparent)]
19    Migration(#[from] sqlx::migrate::MigrateError),
20    /// The connection string is missing its `scheme:` prefix, or the scheme does not
21    /// match any of the enabled database drivers. Contains the offending connection string.
22    #[error("invalid connection url: {0}")]
23    InvalidDbUrl(String),
24    /// The requested database has not been connected to with the `load` command
25    /// and is not listed in the plugin's `preload` configuration.
26    /// Contains the connection string of the database.
27    #[error("database {0} not loaded")]
28    DatabaseNotLoaded(String),
29    /// A value selected from the database has a SQL type that cannot be converted
30    /// to JSON. Contains the name of that SQL type.
31    #[error("unsupported datatype: {0}")]
32    UnsupportedDatatype(String),
33}
34
35impl Serialize for Error {
36    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
37    where
38        S: Serializer,
39    {
40        serializer.serialize_str(self.to_string().as_ref())
41    }
42}