From 25858fc42ac14e8530fe37f046926d697e6db883 Mon Sep 17 00:00:00 2001 From: William Hammond Date: Sun, 13 Dec 2020 15:06:19 -0500 Subject: [PATCH] Improves error handling in the case of missing type --- sqlx-core/src/error.rs | 4 ++++ sqlx-core/src/postgres/connection/describe.rs | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/sqlx-core/src/error.rs b/sqlx-core/src/error.rs index 7ad3638e..5b2f4780 100644 --- a/sqlx-core/src/error.rs +++ b/sqlx-core/src/error.rs @@ -57,6 +57,10 @@ pub enum Error { #[error("no rows returned by a query that expected to return at least one row")] RowNotFound, + /// Type in query doesn't exist. Likely due to typo or missing user type. + #[error("type named {type_name} not found")] + TypeNotFound { type_name: String }, + /// Column index was out of bounds. #[error("column index out of bounds: the len is {len}, but the index is {index}")] ColumnIndexOutOfBounds { index: usize, len: usize }, diff --git a/sqlx-core/src/postgres/connection/describe.rs b/sqlx-core/src/postgres/connection/describe.rs index 97ce8734..b4bfa062 100644 --- a/sqlx-core/src/postgres/connection/describe.rs +++ b/sqlx-core/src/postgres/connection/describe.rs @@ -235,11 +235,13 @@ SELECT oid FROM pg_catalog.pg_type WHERE typname ILIKE $1 ", ) .bind(name) - .fetch_one(&mut *self) - .await?; + .fetch_optional(&mut *self) + .await? + .ok_or_else(|| Error::TypeNotFound { + type_name: String::from(name), + })?; self.cache_type_oid.insert(name.to_string().into(), oid); - Ok(oid) }