{"record":{"id":"abd8fc57f5bd149e","repo":"SeaQL/sea-orm","slug":"fail-to-parse-database-url","errorCode":null,"errorMessage":"Fail to parse database URL","messagePattern":"Fail to parse database URL","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sea-orm-sync/src/database/db_connection.rs","lineNumber":821,"sourceCode":"    /// Panics if [DbConn] is not a SQLite connection.\n    #[cfg(feature = \"sqlx-sqlite\")]\n    pub fn get_sqlite_connection_pool(&self) -> &sqlx::SqlitePool {\n        match &self.inner {\n            DatabaseConnectionType::SqlxSqlitePoolConnection(conn) => &conn.pool,\n            _ => panic!(\"Not SQLite Connection\"),\n        }\n    }\n}\n\nimpl DbBackend {\n    /// Check if the URI is the same as the specified database backend.\n    /// Returns true if they match.\n    ///\n    /// # Panics\n    ///\n    /// Panics if `base_url` cannot be parsed as `Url`.\n    pub fn is_prefix_of(self, base_url: &str) -> bool {\n        let base_url_parsed = Url::parse(base_url).expect(\"Fail to parse database URL\");\n        match self {\n            Self::Postgres => {\n                base_url_parsed.scheme() == \"postgres\" || base_url_parsed.scheme() == \"postgresql\"\n            }\n            Self::MySql => base_url_parsed.scheme() == \"mysql\",\n            Self::Sqlite => base_url_parsed.scheme() == \"sqlite\",\n        }\n    }\n\n    /// Build an SQL [Statement]\n    pub fn build<S>(&self, statement: &S) -> Statement\n    where\n        S: StatementBuilder,\n    {\n        statement.build(self)\n    }\n\n    /// Check if the database supports `RETURNING` syntax on insert and update","sourceCodeStart":803,"sourceCodeEnd":839,"githubUrl":"https://github.com/SeaQL/sea-orm/blob/e29bcd1b417c41a553b386fe94511d7c64a1c8ec/sea-orm-sync/src/database/db_connection.rs#L803-L839","documentation":"DbBackend::is_prefix_of parses base_url as a Url to compare its scheme against the backend, and uses .expect(\"Fail to parse database URL\") — so any base_url string that is not a valid URL causes a panic. The method's contract requires a parseable base_url.","triggerScenarios":"Calling db_backend.is_prefix_of(base_url) with a string that Url::parse rejects — empty string, missing scheme (\"localhost/db\"), malformed scheme, or invalid characters.","commonSituations":"Reading a database URL from env/config where it may be unset or relative (scheme omitted); typos in the URL; passing a filesystem path instead of a URL.","solutions":["Ensure base_url includes a full scheme (postgres://, postgresql://, mysql:// or sqlite://)","Validate with Url::parse(base_url).is_ok() before calling is_prefix_of","Fix the env/config value supplying the URL (e.g. append the missing scheme)"],"exampleFix":"// before\nif DbBackend::Postgres.is_prefix_of(&db_url) { ... } // panics if db_url malformed\n// after\nfn is_pg(url: &str) -> bool {\n    Url::parse(url).map(|u| matches!(u.scheme(), \"postgres\" | \"postgresql\")).unwrap_or(false)\n}","handlingStrategy":"validation","validationCode":"use url::Url;\nfn valid_db_url(s: &str) -> bool {\n    Url::parse(s).map(|u| matches!(u.scheme(), \"postgres\" | \"postgresql\" | \"mysql\" | \"sqlite\")).unwrap_or(false)\n}\n// if !valid_db_url(&base_url) { return Err(...); }","typeGuard":"fn parse_db_url(s: &str) -> Option<url::Url> { Url::parse(s).ok() }","tryCatchPattern":"// avoid the expect by parsing first\nmatch Url::parse(base_url) {\n    Ok(u) => { /* proceed with scheme comparison */ }\n    Err(e) => return Err(MyError::BadBaseUrl(e)),\n}","preventionTips":["Always include an explicit scheme in database URLs","Validate env/config URLs with Url::parse at startup","Reject empty or relative URL values before use","Write tests covering malformed URL inputs"],"tags":["panic","url","parsing","backend"],"backgroundTag":"invalid-url-format","analyzedSha":"e29bcd1b417c41a553b386fe94511d7c64a1c8ec","analyzedAt":"2026-09-10T11:31:52.468Z","contentChangedAt":"2026-09-10T11:31:52.468Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}