mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-10 06:24:16 +00:00
fix(mssql): handle errors without breaking the stream
This commit is contained in:
parent
cfa833fa0d
commit
e4005bb53d
2 changed files with 28 additions and 3 deletions
|
@ -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())
|
||||
}
|
||||
|
||||
|
|
|
@ -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::<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?;
|
||||
|
|
Loading…
Reference in a new issue