From e4005bb53d02ec99a50f040df3f7e73ab66f11c7 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Sat, 27 Jun 2020 20:46:25 -0700 Subject: [PATCH] fix(mssql): handle errors without breaking the stream --- sqlx-core/src/mssql/connection/stream.rs | 3 +-- tests/mssql/mssql.rs | 28 +++++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/sqlx-core/src/mssql/connection/stream.rs b/sqlx-core/src/mssql/connection/stream.rs index 82daf3c2ff..f308853013 100644 --- a/sqlx-core/src/mssql/connection/stream.rs +++ b/sqlx-core/src/mssql/connection/stream.rs @@ -177,8 +177,7 @@ impl MssqlStream { } pub(crate) fn handle_error(&mut self, error: ProtocolError) -> Result { - // error is sent _instead_ of a done - self.pending_done_count -= 1; + // NOTE: [error] is sent IN ADDITION TO [done] Err(MssqlDatabaseError(error).into()) } diff --git a/tests/mssql/mssql.rs b/tests/mssql/mssql.rs index 8083b9350b..14b1fb258c 100644 --- a/tests/mssql/mssql.rs +++ b/tests/mssql/mssql.rs @@ -1,6 +1,6 @@ use futures::TryStreamExt; use sqlx::mssql::Mssql; -use sqlx::{Connection, Executor, Row}; +use sqlx::{Connect, Connection, Executor, MssqlConnection, Row}; use sqlx_core::mssql::MssqlRow; use sqlx_test::new; @@ -27,6 +27,32 @@ async fn it_can_select_expression() -> anyhow::Result<()> { Ok(()) } +#[sqlx_macros::test] +async fn it_can_fail_to_connect() -> anyhow::Result<()> { + let res = MssqlConnection::connect("mssql://sa@localhost").await; + let err = res.unwrap_err(); + let err = err.into_database_error().unwrap(); + + assert_eq!(err.message(), "Login failed for user \'sa\'."); + + Ok(()) +} + +#[sqlx_macros::test] +async fn it_can_inspect_errors() -> anyhow::Result<()> { + let mut conn = new::().await?; + + let res: Result = sqlx::query("select f").execute(&mut conn).await; + let err = res.unwrap_err(); + + // can also do [as_database_error] or use `match ..` + let err = err.into_database_error().unwrap(); + + assert_eq!(err.message(), "Invalid column name 'f'."); + + Ok(()) +} + #[sqlx_macros::test] async fn it_maths() -> anyhow::Result<()> { let mut conn = new::().await?;