Skip to content

Commit

Permalink
Added position and detail to an error text
Browse files Browse the repository at this point in the history
Closes #2151
Closes #2662
  • Loading branch information
vonzshik committed Jan 7, 2021
1 parent b2c4a44 commit 7cd6689
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/Npgsql/PostgresException.cs
Expand Up @@ -4,8 +4,6 @@
using System.Text;
using Npgsql.BackendMessages;

#pragma warning disable CA1032

namespace Npgsql
{
/// <summary>
Expand Down Expand Up @@ -39,7 +37,7 @@ public PostgresException(string messageText, string severity, string invariantSe
string? internalQuery = null, string? where = null, string? schemaName = null, string? tableName = null,
string? columnName = null, string? dataTypeName = null, string? constraintName = null, string? file = null,
string? line = null, string? routine = null)
: base(sqlState + ": " + messageText)
: base(GetMessage(sqlState, messageText, position, detail))
{
MessageText = messageText;
Severity = severity;
Expand Down Expand Up @@ -87,6 +85,19 @@ void AddData<T>(string key, T value)
}
}

static string GetMessage(string sqlState, string messageText, int position, string? detail)
{
var baseMessage = sqlState + ": " + messageText;
var additionalMessage =
TryAddString("POSITION", position == 0 ? null : position.ToString()) +
TryAddString("DETAIL", detail);
return string.IsNullOrEmpty(additionalMessage)
? baseMessage
: baseMessage + Environment.NewLine + additionalMessage;
}

static string TryAddString(string text, string? value) => !string.IsNullOrWhiteSpace(value) ? $"{Environment.NewLine}{text}: {value}" : string.Empty;

PostgresException(ErrorOrNoticeMessage msg)
: this(
msg.Message, msg.Severity, msg.InvariantSeverity, msg.SqlState,
Expand Down
13 changes: 12 additions & 1 deletion test/Npgsql.Tests/ExceptionTests.cs
Expand Up @@ -43,7 +43,7 @@ public void Basic()
Assert.That(ex.InvariantSeverity, Is.EqualTo("ERROR"));
Assert.That(ex.SqlState, Is.EqualTo("12345"));
Assert.That(ex.Position, Is.EqualTo(0));
Assert.That(ex.Message, Is.EqualTo("12345: testexception"));
Assert.That(ex.Message, Does.StartWith("12345: testexception"));

var data = ex.Data;
Assert.That(data[nameof(PostgresException.Severity)], Is.EqualTo("ERROR"));
Expand Down Expand Up @@ -80,6 +80,7 @@ public async Task ErrorDetailsAreRedacted()

var ex = Assert.ThrowsAsync<PostgresException>(() => conn.ExecuteNonQueryAsync($"SELECT * FROM {raiseExceptionFunc}()"));
Assert.That(ex.Detail, Does.Not.Contain("secret"));
Assert.That(ex.Message, Does.Not.Contain("secret"));
Assert.That(ex.Data[nameof(PostgresException.Detail)], Does.Not.Contain("secret"));
Assert.That(ex.ToString(), Does.Not.Contain("secret"));

Expand Down Expand Up @@ -113,6 +114,7 @@ public async Task IncludeErrorDetails()

var ex = Assert.ThrowsAsync<PostgresException>(() => conn.ExecuteNonQueryAsync($"SELECT * FROM {raiseExceptionFunc}()"));
Assert.That(ex.Detail, Does.Contain("secret"));
Assert.That(ex.Message, Does.Contain("secret"));
Assert.That(ex.Data[nameof(PostgresException.Detail)], Does.Contain("secret"));
Assert.That(ex.ToString(), Does.Contain("secret"));

Expand All @@ -122,6 +124,15 @@ public async Task IncludeErrorDetails()
Assert.That(notice!.Detail, Does.Contain("secret"));
}

[Test]
public async Task ErrorPosition()
{
await using var conn = await OpenConnectionAsync();

var ex = Assert.ThrowsAsync<PostgresException>(() => conn.ExecuteNonQueryAsync("SELECT 1; SELECT * FROM \"NonExistingTable\""));
Assert.That(ex.Message, Does.Contain("POSITION: 15"));
}

[Test]
public void ExceptionFieldsArePopulated()
{
Expand Down

0 comments on commit 7cd6689

Please sign in to comment.