2015-12-13 14:04:09 +08:00
|
|
|
use std::error;
|
|
|
|
use std::fmt;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::str;
|
|
|
|
use libc::c_int;
|
|
|
|
use {ffi, errmsg_to_string};
|
2016-05-27 03:03:05 +08:00
|
|
|
use types::Type;
|
2015-12-13 14:04:09 +08:00
|
|
|
|
|
|
|
/// Old name for `Error`. `SqliteError` is deprecated.
|
2016-05-30 08:36:20 +08:00
|
|
|
#[deprecated(since = "0.6.0", note = "Use Error instead")]
|
2015-12-13 14:04:09 +08:00
|
|
|
pub type SqliteError = Error;
|
|
|
|
|
2015-12-17 12:46:39 +08:00
|
|
|
/// Enum listing possible errors from rusqlite.
|
2015-12-13 14:04:09 +08:00
|
|
|
#[derive(Debug)]
|
2016-08-08 21:23:55 +08:00
|
|
|
#[allow(enum_variant_names)]
|
2015-12-13 14:04:09 +08:00
|
|
|
pub enum Error {
|
2015-12-17 12:46:39 +08:00
|
|
|
/// An error from an underlying SQLite call.
|
2015-12-13 14:04:09 +08:00
|
|
|
SqliteFailure(ffi::Error, Option<String>),
|
2015-12-17 12:46:39 +08:00
|
|
|
|
2015-12-17 12:33:56 +08:00
|
|
|
/// Error reported when attempting to open a connection when SQLite was configured to
|
|
|
|
/// allow single-threaded use only.
|
|
|
|
SqliteSingleThreadedMode,
|
|
|
|
|
2016-06-03 03:03:25 +08:00
|
|
|
/// Error when the value of a particular column is requested, but it cannot be converted to
|
|
|
|
/// the requested Rust type.
|
|
|
|
FromSqlConversionFailure(usize, Type, Box<error::Error + Send + Sync>),
|
2015-12-17 12:46:39 +08:00
|
|
|
|
2017-01-23 08:26:19 +08:00
|
|
|
/// Error when SQLite gives us an integral value outside the range of the requested type (e.g.,
|
|
|
|
/// trying to get the value 1000 into a `u8`). The associated `c_int` is the column index, and
|
|
|
|
/// the associated `i64` is the value returned by SQLite.
|
|
|
|
IntegralValueOutOfRange(c_int, i64),
|
|
|
|
|
2015-12-17 12:46:39 +08:00
|
|
|
/// Error converting a string to UTF-8.
|
2015-12-13 14:04:09 +08:00
|
|
|
Utf8Error(str::Utf8Error),
|
2015-12-17 12:46:39 +08:00
|
|
|
|
|
|
|
/// Error converting a string to a C-compatible string because it contained an embedded nul.
|
2015-12-13 14:04:09 +08:00
|
|
|
NulError(::std::ffi::NulError),
|
2015-12-17 12:46:39 +08:00
|
|
|
|
|
|
|
/// Error when using SQL named parameters and passing a parameter name not present in the SQL.
|
2015-12-13 14:04:09 +08:00
|
|
|
InvalidParameterName(String),
|
2015-12-17 12:46:39 +08:00
|
|
|
|
|
|
|
/// Error converting a file path to a string.
|
2015-12-13 14:04:09 +08:00
|
|
|
InvalidPath(PathBuf),
|
2015-12-17 12:46:39 +08:00
|
|
|
|
2016-01-08 00:36:01 +08:00
|
|
|
/// Error returned when an `execute` call returns rows.
|
2015-12-13 14:04:09 +08:00
|
|
|
ExecuteReturnedResults,
|
2015-12-17 12:46:39 +08:00
|
|
|
|
|
|
|
/// Error when a query that was expected to return at least one row (e.g., for `query_row`)
|
|
|
|
/// did not return any.
|
2015-12-13 14:04:09 +08:00
|
|
|
QueryReturnedNoRows,
|
2015-12-17 12:46:39 +08:00
|
|
|
|
|
|
|
/// Error when the value of a particular column is requested, but the index is out of range
|
|
|
|
/// for the statement.
|
2015-12-13 14:04:09 +08:00
|
|
|
InvalidColumnIndex(c_int),
|
2015-12-17 12:46:39 +08:00
|
|
|
|
2016-01-02 19:13:37 +08:00
|
|
|
/// Error when the value of a named column is requested, but no column matches the name
|
|
|
|
/// for the statement.
|
|
|
|
InvalidColumnName(String),
|
|
|
|
|
2015-12-17 12:46:39 +08:00
|
|
|
/// Error when the value of a particular column is requested, but the type of the result in
|
|
|
|
/// that column cannot be converted to the requested Rust type.
|
2016-06-03 03:03:25 +08:00
|
|
|
InvalidColumnType(c_int, Type),
|
2016-05-31 02:35:56 +08:00
|
|
|
|
2016-05-17 00:02:56 +08:00
|
|
|
/// Error when a query that was expected to insert one row did not insert any or insert many.
|
2016-05-17 00:15:07 +08:00
|
|
|
StatementChangedRows(c_int),
|
|
|
|
|
2015-12-17 12:46:39 +08:00
|
|
|
/// Error returned by `functions::Context::get` when the function argument cannot be converted
|
|
|
|
/// to the requested type.
|
2015-12-13 14:04:09 +08:00
|
|
|
#[cfg(feature = "functions")]
|
2016-05-31 02:35:56 +08:00
|
|
|
InvalidFunctionParameterType(usize, Type),
|
2015-12-17 12:46:39 +08:00
|
|
|
|
|
|
|
/// An error case available for implementors of custom user functions (e.g.,
|
|
|
|
/// `create_scalar_function`).
|
2015-12-13 14:04:09 +08:00
|
|
|
#[cfg(feature = "functions")]
|
|
|
|
#[allow(dead_code)]
|
2015-12-16 03:34:42 +08:00
|
|
|
UserFunctionError(Box<error::Error + Send + Sync>),
|
2015-12-13 14:04:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<str::Utf8Error> for Error {
|
|
|
|
fn from(err: str::Utf8Error) -> Error {
|
|
|
|
Error::Utf8Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<::std::ffi::NulError> for Error {
|
|
|
|
fn from(err: ::std::ffi::NulError) -> Error {
|
|
|
|
Error::NulError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-02-14 23:11:59 +08:00
|
|
|
match *self {
|
|
|
|
Error::SqliteFailure(ref err, None) => err.fmt(f),
|
|
|
|
Error::SqliteFailure(_, Some(ref s)) => write!(f, "{}", s),
|
|
|
|
Error::SqliteSingleThreadedMode => {
|
2016-02-03 02:12:00 +08:00
|
|
|
write!(f,
|
|
|
|
"SQLite was compiled or configured for single-threaded use only")
|
|
|
|
}
|
2016-06-03 03:03:25 +08:00
|
|
|
Error::FromSqlConversionFailure(i, ref t, ref err) => {
|
|
|
|
write!(f,
|
|
|
|
"Conversion error from type {} at index: {}, {}",
|
|
|
|
t,
|
|
|
|
i,
|
|
|
|
err)
|
|
|
|
}
|
2017-01-23 08:26:19 +08:00
|
|
|
Error::IntegralValueOutOfRange(col, val) => {
|
|
|
|
write!(f, "Integer {} out of range at index {}", val, col)
|
|
|
|
}
|
2016-02-14 23:11:59 +08:00
|
|
|
Error::Utf8Error(ref err) => err.fmt(f),
|
|
|
|
Error::NulError(ref err) => err.fmt(f),
|
|
|
|
Error::InvalidParameterName(ref name) => write!(f, "Invalid parameter name: {}", name),
|
|
|
|
Error::InvalidPath(ref p) => write!(f, "Invalid path: {}", p.to_string_lossy()),
|
|
|
|
Error::ExecuteReturnedResults => {
|
2016-02-03 02:12:00 +08:00
|
|
|
write!(f, "Execute returned results - did you mean to call query?")
|
|
|
|
}
|
2016-02-14 23:11:59 +08:00
|
|
|
Error::QueryReturnedNoRows => write!(f, "Query returned no rows"),
|
|
|
|
Error::InvalidColumnIndex(i) => write!(f, "Invalid column index: {}", i),
|
|
|
|
Error::InvalidColumnName(ref name) => write!(f, "Invalid column name: {}", name),
|
2016-05-27 03:03:05 +08:00
|
|
|
Error::InvalidColumnType(i, ref t) => {
|
|
|
|
write!(f, "Invalid column type {} at index: {}", t, i)
|
|
|
|
}
|
2016-05-17 00:15:07 +08:00
|
|
|
Error::StatementChangedRows(i) => write!(f, "Query changed {} rows", i),
|
2015-12-13 14:04:09 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "functions")]
|
2016-05-27 03:03:05 +08:00
|
|
|
Error::InvalidFunctionParameterType(i, ref t) => {
|
|
|
|
write!(f, "Invalid function parameter type {} at index {}", t, i)
|
|
|
|
}
|
2015-12-13 14:04:09 +08:00
|
|
|
#[cfg(feature = "functions")]
|
2016-02-14 23:11:59 +08:00
|
|
|
Error::UserFunctionError(ref err) => err.fmt(f),
|
2015-12-13 14:04:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for Error {
|
|
|
|
fn description(&self) -> &str {
|
2016-02-14 23:11:59 +08:00
|
|
|
match *self {
|
|
|
|
Error::SqliteFailure(ref err, None) => err.description(),
|
|
|
|
Error::SqliteFailure(_, Some(ref s)) => s,
|
|
|
|
Error::SqliteSingleThreadedMode => {
|
2016-02-03 02:12:00 +08:00
|
|
|
"SQLite was compiled or configured for single-threaded use only"
|
|
|
|
}
|
2016-06-03 03:03:25 +08:00
|
|
|
Error::FromSqlConversionFailure(_, _, ref err) => err.description(),
|
2017-02-09 04:11:15 +08:00
|
|
|
Error::IntegralValueOutOfRange(_, _) => "integral value out of range of requested type",
|
2016-02-14 23:11:59 +08:00
|
|
|
Error::Utf8Error(ref err) => err.description(),
|
|
|
|
Error::InvalidParameterName(_) => "invalid parameter name",
|
|
|
|
Error::NulError(ref err) => err.description(),
|
|
|
|
Error::InvalidPath(_) => "invalid path",
|
|
|
|
Error::ExecuteReturnedResults => {
|
2016-02-03 02:12:00 +08:00
|
|
|
"execute returned results - did you mean to call query?"
|
|
|
|
}
|
2016-02-14 23:11:59 +08:00
|
|
|
Error::QueryReturnedNoRows => "query returned no rows",
|
|
|
|
Error::InvalidColumnIndex(_) => "invalid column index",
|
|
|
|
Error::InvalidColumnName(_) => "invalid column name",
|
2016-05-27 03:03:05 +08:00
|
|
|
Error::InvalidColumnType(_, _) => "invalid column type",
|
2016-05-17 00:15:07 +08:00
|
|
|
Error::StatementChangedRows(_) => "query inserted zero or more than one row",
|
2015-12-13 14:04:09 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "functions")]
|
2016-05-27 03:03:05 +08:00
|
|
|
Error::InvalidFunctionParameterType(_, _) => "invalid function parameter type",
|
2015-12-13 14:04:09 +08:00
|
|
|
#[cfg(feature = "functions")]
|
2016-02-14 23:11:59 +08:00
|
|
|
Error::UserFunctionError(ref err) => err.description(),
|
2015-12-13 14:04:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&error::Error> {
|
2016-02-14 23:11:59 +08:00
|
|
|
match *self {
|
|
|
|
Error::SqliteFailure(ref err, _) => Some(err),
|
2016-06-03 03:03:25 +08:00
|
|
|
Error::FromSqlConversionFailure(_, _, ref err) => Some(&**err),
|
2016-02-14 23:11:59 +08:00
|
|
|
Error::Utf8Error(ref err) => Some(err),
|
|
|
|
Error::NulError(ref err) => Some(err),
|
2016-03-29 23:54:02 +08:00
|
|
|
|
2017-01-23 08:26:19 +08:00
|
|
|
Error::IntegralValueOutOfRange(_, _) |
|
2016-03-29 23:54:02 +08:00
|
|
|
Error::SqliteSingleThreadedMode |
|
|
|
|
Error::InvalidParameterName(_) |
|
|
|
|
Error::ExecuteReturnedResults |
|
|
|
|
Error::QueryReturnedNoRows |
|
|
|
|
Error::InvalidColumnIndex(_) |
|
|
|
|
Error::InvalidColumnName(_) |
|
2016-05-27 03:03:05 +08:00
|
|
|
Error::InvalidColumnType(_, _) |
|
2016-05-20 04:02:06 +08:00
|
|
|
Error::InvalidPath(_) |
|
2016-06-02 08:52:22 +08:00
|
|
|
Error::StatementChangedRows(_) => None,
|
2015-12-13 14:04:09 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "functions")]
|
2016-05-27 03:03:05 +08:00
|
|
|
Error::InvalidFunctionParameterType(_, _) => None,
|
2016-02-02 03:42:50 +08:00
|
|
|
|
2015-12-13 14:04:09 +08:00
|
|
|
#[cfg(feature = "functions")]
|
2016-02-14 23:11:59 +08:00
|
|
|
Error::UserFunctionError(ref err) => Some(&**err),
|
2015-12-13 14:04:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are public but not re-exported by lib.rs, so only visible within crate.
|
|
|
|
|
|
|
|
pub fn error_from_sqlite_code(code: c_int, message: Option<String>) -> Error {
|
|
|
|
Error::SqliteFailure(ffi::Error::new(code), message)
|
|
|
|
}
|
|
|
|
|
2017-02-08 10:09:20 +08:00
|
|
|
pub fn error_from_handle(db: *mut ffi::sqlite3, code: c_int) -> Error {
|
2015-12-13 14:04:09 +08:00
|
|
|
let message = if db.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(unsafe { errmsg_to_string(ffi::sqlite3_errmsg(db)) })
|
|
|
|
};
|
|
|
|
error_from_sqlite_code(code, message)
|
|
|
|
}
|