Skip to content

Commit

Permalink
fix: handle mssql authentication error (#4908)
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky authored Jun 6, 2024
1 parent 12e25d8 commit a3ecb20
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
8 changes: 7 additions & 1 deletion libs/user-facing-errors/src/quaint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn render_quaint_error(kind: &ErrorKind, connection_info: &ConnectionInfo) -
},

(ErrorKind::AuthenticationFailed { .. }, ConnectionInfo::External(_)) => default_value,
#[cfg(any(feature = "mysql-native", feature = "postgresql-native"))]
#[cfg(any(feature = "mysql-native", feature = "postgresql-native", feature = "mssql-native"))]
(ErrorKind::AuthenticationFailed { user }, _) => match connection_info {
ConnectionInfo::Native(NativeConnectionInfo::Postgres(url)) => {
Some(KnownError::new(common::IncorrectDatabaseCredentials {
Expand All @@ -133,6 +133,12 @@ pub fn render_quaint_error(kind: &ErrorKind, connection_info: &ConnectionInfo) -
database_host: url.host().to_owned(),
}))
}
ConnectionInfo::Native(NativeConnectionInfo::Mssql(url)) => {
Some(KnownError::new(common::IncorrectDatabaseCredentials {
database_user: format!("{user}"),
database_host: url.host().to_owned(),
}))
}
_ => unreachable!(),
},

Expand Down
37 changes: 37 additions & 0 deletions schema-engine/sql-migration-tests/tests/errors/error_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use connection_string::JdbcString;
use indoc::{formatdoc, indoc};
use pretty_assertions::assert_eq;
use quaint::prelude::Insert;
Expand All @@ -7,6 +8,7 @@ use schema_core::{
};
use serde_json::json;
use sql_migration_tests::test_api::*;
use std::str::FromStr;
use url::Url;

pub(crate) async fn connection_error(schema: String) -> ConnectorError {
Expand Down Expand Up @@ -95,6 +97,41 @@ fn authentication_failure_must_return_a_known_error_on_mysql(api: TestApi) {
assert_eq!(json_error, expected);
}

#[test_connector(tags(Mssql))]
fn authentication_failure_must_return_a_known_error_on_mssql(api: TestApi) {
let mut url = JdbcString::from_str(&format!("jdbc:{}", api.connection_string())).unwrap();
let host = url.server_name().unwrap().to_string();
let properties = url.properties_mut();
let user = properties.get("user").cloned().unwrap();

*properties.get_mut("password").unwrap() = "obviously-not-right".to_string();

let dm = format!(
r#"
datasource db {{
provider = "sqlserver"
url = "{}"
}}
"#,
url.to_string().replace("jdbc:", "")
);

let error = tok(connection_error(dm));

let json_error = serde_json::to_value(&error.to_user_facing()).unwrap();
let expected = json!({
"is_panic": false,
"message": format!("Authentication failed against database server at `{host}`, the provided database credentials for `{user}` are not valid.\n\nPlease make sure to provide valid database credentials for the database server at `{host}`."),
"meta": {
"database_user": user,
"database_host": host,
},
"error_code": "P1000"
});

assert_eq!(json_error, expected);
}

// TODO(tech-debt): get rid of provider-specific PSL `dm` declaration, and use `test_api::datamodel_with_provider` utility instead.
// See: https://github.com/prisma/team-orm/issues/835.
// This issue also currently prevents us from defining an `Mssql`-specific copy of this `unreachable_database_*` test case,
Expand Down

0 comments on commit a3ecb20

Please sign in to comment.