Skip to content

Commit

Permalink
fix(mssql): handle errors without breaking the stream
Browse files Browse the repository at this point in the history
  • Loading branch information
mehcode committed Jun 28, 2020
1 parent cfa833f commit e4005bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
3 changes: 1 addition & 2 deletions sqlx-core/src/mssql/connection/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ impl MssqlStream {
}

pub(crate) fn handle_error<T>(&mut self, error: ProtocolError) -> Result<T, Error> {
// error is sent _instead_ of a done
self.pending_done_count -= 1;
// NOTE: [error] is sent IN ADDITION TO [done]
Err(MssqlDatabaseError(error).into())
}

Expand Down
28 changes: 27 additions & 1 deletion tests/mssql/mssql.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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::<Mssql>().await?;

let res: Result<u64, sqlx::Error> = 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::<Mssql>().await?;
Expand Down

0 comments on commit e4005bb

Please sign in to comment.