Sonarr/Sonarr · critical · SonarrStartupException

Unable to determine database connection string for type {0}.

Error message

Unable to determine database connection string for type {0}.

What it means

Thrown by the ConnectionStringFactory constructor's switch default as a SonarrStartupException. It fires only if GetConnectionStringType returned an enum value not handled by the three cases. Because the private ConnectionStringType enum has exactly three members (Sqlite, PostgreSqlVars, PostgreSqlConnectionString) and GetConnectionStringType only ever returns those, this branch is effectively unreachable defensive code. Also note the message uses a literal '{0}' that the args-based formatter should interpolate but the wording is misleading.

Source

Thrown at src/NzbDrone.Core/Datastore/ConnectionStringFactory.cs:43

            var connectionStringType = GetConnectionStringType();

            switch (connectionStringType)
            {
                case ConnectionStringType.PostgreSqlVars:
                    MainDbConnection = GetPostgresConnectionString(_configFileProvider.PostgresMainDb);
                    LogDbConnection = GetPostgresConnectionString(_configFileProvider.PostgresLogDb);
                    break;
                case ConnectionStringType.PostgreSqlConnectionString:
                    MainDbConnection = GetPostgresConnectionInfoFromConnectionString(_configFileProvider.PostgresMainDbConnectionString);
                    LogDbConnection = GetPostgresConnectionInfoFromConnectionString(_configFileProvider.PostgresLogDbConnectionString);
                    break;
                case ConnectionStringType.Sqlite:
                    MainDbConnection = GetConnectionString(appFolderInfo.GetDatabase());
                    LogDbConnection = GetConnectionString(appFolderInfo.GetLogDatabase());
                    break;
                default:
                    throw new SonarrStartupException("Unable to determine database connection string for type {0}.", connectionStringType.ToString());
            }
        }

        public DatabaseConnectionInfo MainDbConnection { get; private set; }
        public DatabaseConnectionInfo LogDbConnection { get; private set; }

        public string GetDatabasePath(string connectionString)
        {
            var connectionBuilder = new SQLiteConnectionStringBuilder(connectionString);

            return connectionBuilder.DataSource;
        }

        private static DatabaseConnectionInfo GetConnectionString(string dbPath)
        {
            var connectionBuilder = new SQLiteConnectionStringBuilder
            {
                DataSource = dbPath,

View on GitHub (pinned to da2284d7ea)

Solutions

  1. Treat as a code defect: ensure every ConnectionStringType enum member has a case in the switch.
  2. Replace the default with an explicit exhaustive switch or switch expression so the compiler flags missing cases.
  3. If hit in production, review recent source changes to the enum or GetConnectionStringType.
  4. Report as a bug; no user configuration produces this.

Example fix

// before: default branch masks a missing case
switch (connectionStringType) {
  case PostgreSqlVars: ...
  case PostgreSqlConnectionString: ...
  case Sqlite: ...
  default: throw new SonarrStartupException("Unable to determine ... {0}", connectionStringType.ToString());
}

// after: exhaustive switch expression, compiler enforces completeness
MainDbConnection = connectionStringType switch {
  ConnectionStringType.PostgreSqlVars => GetPostgresConnectionString(_configFileProvider.PostgresMainDb),
  ConnectionStringType.PostgreSqlConnectionString => GetPostgresConnectionInfoFromConnectionString(_configFileProvider.PostgresMainDbConnectionString),
  ConnectionStringType.Sqlite => GetConnectionString(appFolderInfo.GetDatabase()),
  _ => throw new SonarrStartupException($"Unable to determine database connection string for type {connectionStringType}.")
};
Defensive patterns

Strategy: validation

Validate before calling

// Unreachable in practice; defensive compile-time check instead.
// Use an exhaustive switch expression so a missing enum case is a build error:
_ = connectionStringType switch
{
    ConnectionStringType.Sqlite => true,
    ConnectionStringType.PostgreSqlVars => true,
    ConnectionStringType.PostgreSqlConnectionString => true,
    _ => throw new InvalidOperationException($"Unhandled {nameof(ConnectionStringType)}: {connectionStringType}")
};

Prevention

When it happens

Trigger: Practically unreachable: would require a future enum member added without a matching case, or reflection/serialization forcing an out-of-range enum value into the switch. Triggering it indicates a code bug rather than a configuration issue.

Common situations: Only seen if the source is patched to add a new ConnectionStringType without updating the switch, or an invalid enum value is injected via test tooling.

Related errors


AI-assisted analysis of Sonarr/Sonarr@da2284d7ea (2026-08-13). Data as JSON: /api/errors/a94fe998150fd95a. Report an issue: GitHub.