Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maybe should return a error detail for connection failure #310

Closed
ghost opened this issue Nov 11, 2021 · 14 comments
Closed

maybe should return a error detail for connection failure #310

ghost opened this issue Nov 11, 2021 · 14 comments

Comments

@ghost
Copy link

ghost commented Nov 11, 2021

I am a beginner, when i use sea-orm with mysql, The first problem I encountered was the connection problem, it only told me: "Connection Error: Failed to connect". I am very confused. em.. maybe we can return error detail for fix code.

impl SqlxMySqlConnector {
    pub fn accepts(string: &str) -> bool {
        string.starts_with("mysql://") && string.parse::<MySqlConnectOptions>().is_ok()
    }

    pub async fn connect(options: ConnectOptions) -> Result<DatabaseConnection, DbErr> {
        let mut opt = options
            .url
            .parse::<MySqlConnectOptions>()
            .map_err(|e| DbErr::Conn(e.to_string()))?;
        if !options.sqlx_logging {
            use sqlx::ConnectOptions;
            opt.disable_statement_logging();
        }

        // maybe we can return error detail for fix code
        match options.pool_options().connect_with(opt).await {
            Ok(pool) => {
                Ok(DatabaseConnection::SqlxMySqlPoolConnection(
                    SqlxMySqlPoolConnection { pool },
                ))
            },
            Err(err) =>  Err(DbErr::Conn(err.to_string()))
        }

        // if let Ok(pool) = options.pool_options().connect_with(opt).await {
        //     Ok(DatabaseConnection::SqlxMySqlPoolConnection(
        //         SqlxMySqlPoolConnection { pool },
        //     ))
        // } else {
        //     Err(DbErr::Conn("Failed to connect.".to_owned()))
        // }
    }
}
@billy1624
Copy link
Member

Hey @yamowner, welcome!! Can you show me how you setup the connection? Remember to mask the credentials in connection string.

@billy1624
Copy link
Member

It should be something like...

let db: DatabaseConnection = Database::connect("mysql://username:password@host/database").await?;

@ghost
Copy link
Author

ghost commented Nov 11, 2021

@billy1624 yes, i did like this:

async fn main() {
    let conn_str = "mysql://username:password@host:port/test";

    match connect_db(conn_str).await {
        Ok(db) => {
            let admin = Account::find_by_id(1).one(&db).await.unwrap();
            println!("{:#?}", &admin);
        }
        Err(err) => {
            println!("{}", &err);
        }
    }
}

async fn connect_db(conn_str: &str) -> Result<DatabaseConnection, DbErr> {
    let mut opt = ConnectOptions::new(conn_str.to_owned());
    opt.max_connections(10)
        .min_connections(5)
        .connect_timeout(Duration::from_secs(8))
        .idle_timeout(Duration::from_secs(8));
    let db = Database::connect(opt).await;
    db
}

then, it print "Connection Error: Failed to connect.", this mistake confused me。thanks for your reply ^_^

@ghost
Copy link
Author

ghost commented Nov 11, 2021

my Cargo.toml like this:

[dependencies]
sea-orm = { version = "^0", features = [
	"sqlx-mysql",
	"runtime-tokio-rustls",   // it's ok when change to runtime-tokio-native-tls
	"macros",
	"debug-print",
], default-features = false }
chrono = "*"
tokio = { version = "1", features = ["full"] }

@billy1624 billy1624 mentioned this issue Nov 11, 2021
@billy1624
Copy link
Member

my Cargo.toml like this:

[dependencies]
sea-orm = { version = "^0", features = [
	"sqlx-mysql",
	"runtime-tokio-rustls",   // it's ok when change to runtime-tokio-native-tls
	"macros",
	"debug-print",
], default-features = false }
chrono = "*"
tokio = { version = "1", features = ["full"] }

Hmmmm... this is weird

@billy1624
Copy link
Member

I can't reproduce the error in #311

@billy1624
Copy link
Member

Hey @yamowner, can you try #312? And see what error thrown out of SQLx

[dependencies]
sea-orm = { version = "^0", git="https://github.com/SeaQL/sea-orm.git", branch = "detailed-conn-errors", features = [
	"sqlx-mysql",
	"runtime-tokio-rustls",
	"macros",
	"debug-print",
], default-features = false }

@ghost
Copy link
Author

ghost commented Nov 11, 2021

@billy1624 I found answer in this sqlx issue#846, it looks like to be rustls lack support of ipaddress. my error detail is this:

error occurred while attempting to establish a TLS connection: InvalidDNSNameError

@billy1624
Copy link
Member

Oh, interesting issue

@ghost
Copy link
Author

ghost commented Nov 11, 2021

i edit my /etc/hosts, and change my connect string, then it works~

@billy1624
Copy link
Member

Crazy... then you have to convert your address to IPv6 and quote it loll

@billy1624
Copy link
Member

Anyways, we will improve the error messages #312

@ghost
Copy link
Author

ghost commented Nov 11, 2021

Thank you for your kind help!!, next time i'll provide more useful information

@billy1624
Copy link
Member

Welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant