Skip to content

Commit

Permalink
Removed public setters
Browse files Browse the repository at this point in the history
  • Loading branch information
YohDeadfall committed Mar 2, 2019
1 parent 5cd622d commit 0af8c1d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 40 deletions.
41 changes: 18 additions & 23 deletions src/Npgsql/PostgresException.cs
Expand Up @@ -29,11 +29,6 @@ public sealed class PostgresException : NpgsqlException
[CanBeNull]
bool _dataInitialized;

/// <summary>
/// Creates a new instance.
/// </summary>
public PostgresException() {}

internal PostgresException(NpgsqlReadBuffer buf)
{
var msg = new ErrorOrNoticeMessage(buf);
Expand All @@ -56,7 +51,7 @@ internal PostgresException(NpgsqlReadBuffer buf)
Routine = msg.Routine;
}

PostgresException(SerializationInfo info, StreamingContext context)
internal PostgresException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Severity = GetValue<string>(nameof(Severity));
Expand Down Expand Up @@ -233,7 +228,7 @@ void AddData<T>(string key, T value)
/// Always present.
/// </summary>
[PublicAPI]
public string Severity { get; set; }
public string Severity { get; }

/// <summary>
/// The SQLSTATE code for the error.
Expand All @@ -244,7 +239,7 @@ void AddData<T>(string key, T value)
/// See http://www.postgresql.org/docs/current/static/errcodes-appendix.html
/// </remarks>
[PublicAPI]
public string SqlState { get; set; }
public string SqlState { get; }

/// <summary>
/// The SQLSTATE code for the error.
Expand All @@ -264,84 +259,84 @@ void AddData<T>(string key, T value)
/// Always present.
/// </remarks>
[PublicAPI]
public string MessageText { get; set; }
public string MessageText { get; }

/// <summary>
/// An optional secondary error message carrying more detail about the problem.
/// May run to multiple lines.
/// </summary>
[PublicAPI]
public string Detail { get; set; }
public string Detail { get; }

/// <summary>
/// An optional suggestion what to do about the problem.
/// This is intended to differ from Detail in that it offers advice (potentially inappropriate) rather than hard facts.
/// May run to multiple lines.
/// </summary>
[PublicAPI]
public string Hint { get; set; }
public string Hint { get; }

/// <summary>
/// The field value is a decimal ASCII integer, indicating an error cursor position as an index into the original query string.
/// The first character has index 1, and positions are measured in characters not bytes.
/// 0 means not provided.
/// </summary>
[PublicAPI]
public int Position { get; set; }
public int Position { get; }

/// <summary>
/// This is defined the same as the <see cref="Position"/> field, but it is used when the cursor position refers to an internally generated command rather than the one submitted by the client.
/// The <see cref="InternalQuery" /> field will always appear when this field appears.
/// 0 means not provided.
/// </summary>
[PublicAPI]
public int InternalPosition { get; set; }
public int InternalPosition { get; }

/// <summary>
/// The text of a failed internally-generated command.
/// This could be, for example, a SQL query issued by a PL/pgSQL function.
/// </summary>
[PublicAPI]
public string InternalQuery { get; set; }
public string InternalQuery { get; }

/// <summary>
/// An indication of the context in which the error occurred.
/// Presently this includes a call stack traceback of active PL functions.
/// The trace is one entry per line, most recent first.
/// </summary>
[PublicAPI]
public string Where { get; set; }
public string Where { get; }

/// <summary>
/// If the error was associated with a specific database object, the name of the schema containing that object, if any.
/// </summary>
/// <remarks>PostgreSQL 9.3 and up.</remarks>
[PublicAPI]
public string SchemaName { get; set; }
public string SchemaName { get; }

/// <summary>
/// Table name: if the error was associated with a specific table, the name of the table.
/// (Refer to the schema name field for the name of the table's schema.)
/// </summary>
/// <remarks>PostgreSQL 9.3 and up.</remarks>
[PublicAPI]
public string TableName { get; set; }
public string TableName { get; }

/// <summary>
/// If the error was associated with a specific table column, the name of the column.
/// (Refer to the schema and table name fields to identify the table.)
/// </summary>
/// <remarks>PostgreSQL 9.3 and up.</remarks>
[PublicAPI]
public string ColumnName { get; set; }
public string ColumnName { get; }

/// <summary>
/// If the error was associated with a specific data type, the name of the data type.
/// (Refer to the schema name field for the name of the data type's schema.)
/// </summary>
/// <remarks>PostgreSQL 9.3 and up.</remarks>
[PublicAPI]
public string DataTypeName { get; set; }
public string DataTypeName { get; }

/// <summary>
/// If the error was associated with a specific constraint, the name of the constraint.
Expand All @@ -350,26 +345,26 @@ void AddData<T>(string key, T value)
/// </summary>
/// <remarks>PostgreSQL 9.3 and up.</remarks>
[PublicAPI]
public string ConstraintName { get; set; }
public string ConstraintName { get; }

/// <summary>
/// The file name of the source-code location where the error was reported.
/// </summary>
/// <remarks>PostgreSQL 9.3 and up.</remarks>
[PublicAPI]
public string File { get; set; }
public string File { get; }

/// <summary>
/// The line number of the source-code location where the error was reported.
/// </summary>
[PublicAPI]
public string Line { get; set; }
public string Line { get; }

/// <summary>
/// The name of the source-code routine reporting the error.
/// </summary>
[PublicAPI]
public string Routine { get; set; }
public string Routine { get; }

#endregion
}
Expand Down
95 changes: 78 additions & 17 deletions test/Npgsql.Tests/ExceptionTests.cs
Expand Up @@ -2,6 +2,7 @@
using System.Data;
using System.IO;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using NUnit.Framework;

Expand Down Expand Up @@ -154,32 +155,92 @@ public void NpgsqlExceptionTransience()
[Test]
public void PostgresExceptionTransience()
{
Assert.True(new PostgresException { SqlState = "53300" }.IsTransient);
Assert.False(new PostgresException { SqlState = "0" }.IsTransient);
Assert.True(CreateWithSqlState("53300").IsTransient);
Assert.False(CreateWithSqlState("0").IsTransient);

PostgresException CreateWithSqlState(string sqlState)
{
var info = CreateSerializationInfo();

info.AddValue(nameof(PostgresException.Severity), null);
info.AddValue(nameof(PostgresException.SqlState), sqlState);
info.AddValue(nameof(PostgresException.MessageText), null);
info.AddValue(nameof(PostgresException.Detail), null);
info.AddValue(nameof(PostgresException.Hint), null);
info.AddValue(nameof(PostgresException.Position), 0);
info.AddValue(nameof(PostgresException.InternalPosition), 0);
info.AddValue(nameof(PostgresException.InternalQuery), null);
info.AddValue(nameof(PostgresException.Where), null);
info.AddValue(nameof(PostgresException.SchemaName), null);
info.AddValue(nameof(PostgresException.TableName), null);
info.AddValue(nameof(PostgresException.ColumnName), null);
info.AddValue(nameof(PostgresException.DataTypeName), null);
info.AddValue(nameof(PostgresException.ConstraintName), null);
info.AddValue(nameof(PostgresException.File), null);
info.AddValue(nameof(PostgresException.Line), null);
info.AddValue(nameof(PostgresException.Routine), null);

return new PostgresException(info, default);
}
}

#if NET452
[Test]
[Ignore("DbException doesn't support serialization in .NET Core 2.0 (PlatformNotSupportedException)")]
public void Serialization()
{
var e = new PostgresException
{
Severity = "High",
TableName = "foo",
Position = 18
};

var info = CreateSerializationInfo();

info.AddValue(nameof(PostgresException.Severity), "high");
info.AddValue(nameof(PostgresException.SqlState), "53300");
info.AddValue(nameof(PostgresException.MessageText), "message");
info.AddValue(nameof(PostgresException.Detail), "detail");
info.AddValue(nameof(PostgresException.Hint), "hint");
info.AddValue(nameof(PostgresException.Position), 18);
info.AddValue(nameof(PostgresException.InternalPosition), 42);
info.AddValue(nameof(PostgresException.InternalQuery), "query");
info.AddValue(nameof(PostgresException.Where), "where");
info.AddValue(nameof(PostgresException.SchemaName), "schema");
info.AddValue(nameof(PostgresException.TableName), "table");
info.AddValue(nameof(PostgresException.ColumnName), "column");
info.AddValue(nameof(PostgresException.DataTypeName), "type");
info.AddValue(nameof(PostgresException.ConstraintName), "constraint");
info.AddValue(nameof(PostgresException.File), "file");
info.AddValue(nameof(PostgresException.Line), "line");
info.AddValue(nameof(PostgresException.Routine), "routine");

var actual = new PostgresException(info, default);
var formatter = new BinaryFormatter();
var stream = new MemoryStream();
formatter.Serialize(stream, e);

formatter.Serialize(stream, actual);
stream.Seek(0, SeekOrigin.Begin);
var e2 = (PostgresException)formatter.Deserialize(stream);

Assert.That(e2.Severity, Is.EqualTo(e.Severity));
Assert.That(e2.TableName, Is.EqualTo(e.TableName));
Assert.That(e2.Position, Is.EqualTo(e.Position));
var expected = (PostgresException)formatter.Deserialize(stream);

Assert.That(expected.Severity, Is.EqualTo(actual.Severity));
Assert.That(expected.SqlState, Is.EqualTo(actual.SqlState));
Assert.That(expected.MessageText, Is.EqualTo(actual.MessageText));
Assert.That(expected.Detail, Is.EqualTo(actual.Detail));
Assert.That(expected.Hint, Is.EqualTo(actual.Hint));
Assert.That(expected.Position, Is.EqualTo(actual.Position));
Assert.That(expected.InternalPosition, Is.EqualTo(actual.InternalPosition));
Assert.That(expected.InternalQuery, Is.EqualTo(actual.InternalQuery));
Assert.That(expected.Where, Is.EqualTo(actual.Where));
Assert.That(expected.SchemaName, Is.EqualTo(actual.SchemaName));
Assert.That(expected.TableName, Is.EqualTo(actual.TableName));
Assert.That(expected.ColumnName, Is.EqualTo(actual.ColumnName));
Assert.That(expected.DataTypeName, Is.EqualTo(actual.DataTypeName));
Assert.That(expected.ConstraintName, Is.EqualTo(actual.ConstraintName));
Assert.That(expected.File, Is.EqualTo(actual.File));
Assert.That(expected.Line, Is.EqualTo(actual.Line));
Assert.That(expected.Routine, Is.EqualTo(actual.Routine));
}

SerializationInfo CreateSerializationInfo()
{
var info = new SerializationInfo(typeof(PostgresException), new FormatterConverter());
new Exception().GetObjectData(info, default);

return info;
}
#endif
}
}

0 comments on commit 0af8c1d

Please sign in to comment.