Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Npgsql/Internal/NpgsqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3115,7 +3115,6 @@ enum ConnectorState

/// <summary>
/// The connection was broken because an unexpected error occurred which left it in an unknown state.
/// This state isn't implemented yet.
/// </summary>
Broken,

Expand Down
10 changes: 8 additions & 2 deletions src/Npgsql/NpgsqlConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,8 +1487,11 @@ void CheckOpen()
case ConnectionState.Open | ConnectionState.Fetching:
case ConnectionState.Connecting:
return;
case ConnectionState.Closed:
case ConnectionState.Broken:
ThrowHelper.ThrowNpgsqlException(
"The connection was previously broken because of an exception (see the logs for details).");
return;
case ConnectionState.Closed:
ThrowHelper.ThrowInvalidOperationException("Connection is not open");
return;
default:
Expand Down Expand Up @@ -1518,8 +1521,11 @@ internal void CheckReady()
case ConnectionState.Open:
case ConnectionState.Connecting: // We need to do type loading as part of connecting
return;
case ConnectionState.Closed:
case ConnectionState.Broken:
ThrowHelper.ThrowNpgsqlException(
"The connection was previously broken because of an exception (see the logs for details).");
return;
case ConnectionState.Closed:
ThrowHelper.ThrowInvalidOperationException("Connection is not open");
return;
case ConnectionState.Open | ConnectionState.Executing:
Expand Down
15 changes: 15 additions & 0 deletions test/Npgsql.Tests/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ public async Task Break_while_open()
Assert.That(conn.FullState, Is.EqualTo(ConnectionState.Broken));
}

[Test]
[IssueLink("https://github.com/npgsql/npgsql/issues/5605")]
public async Task Broken_connection_throws_NpgsqlException_not_InvalidOperationException()
{
await using var dataSource = CreateDataSource();
await using var conn = await dataSource.OpenConnectionAsync();

conn.Connector!.Break(new IOException("Simulated connection break"));
Assert.That(conn.FullState, Is.EqualTo(ConnectionState.Broken));

// A broken connection must surface as NpgsqlException (catchable as a connection error)
// rather than InvalidOperationException. See #5605.
Assert.Throws<NpgsqlException>(() => conn.BeginTransaction());
}

#region Connection Errors

#if IGNORE
Expand Down