Skip to content

Fix exceptions serialization #1514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2018
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using NHibernate.Cfg;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.MappingExceptions
Expand Down Expand Up @@ -44,5 +45,21 @@ public void ConstructWithNullType()
new PropertyNotFoundException(null, "someField");
new PropertyNotFoundException(null, "SomeProperty", "getter");
}

[Test]
public void IsSerializable()
{
NHAssert.IsSerializable(new PropertyNotFoundException(null, "someField"));
NHAssert.IsSerializable(new PropertyNotFoundException(null, "SomeProperty", "getter"));
}

[Test]
public void SerializeWithType()
{
var bytes = SerializationHelper.Serialize(new PropertyNotFoundException(typeof(PropertyNotFoundExceptionFixture), "SomeProperty", "getter"));
var pnfe = (PropertyNotFoundException) SerializationHelper.Deserialize(bytes);

Assert.That(pnfe.TargetType, Is.EqualTo(typeof(PropertyNotFoundExceptionFixture)));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
using System;
using System.Runtime.Serialization;
using System.Security;

namespace NHibernate.Bytecode
{
[Serializable]
public class UnableToLoadProxyFactoryFactoryException : HibernateByteCodeException
{
private readonly string typeName;

public UnableToLoadProxyFactoryFactoryException(string typeName, Exception inner)
: base("", inner)
{
this.typeName = typeName;
TypeName = typeName;
}

protected UnableToLoadProxyFactoryFactoryException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
foreach (var entry in info)
{
if (entry.Name == "TypeName")
{
TypeName = entry.Value?.ToString();
}
}
}

[SecurityCritical]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("TypeName", TypeName);
}

protected UnableToLoadProxyFactoryFactoryException(SerializationInfo info,
StreamingContext context) : base(info, context) {}
public string TypeName { get; }
public override string Message
{
get
Expand All @@ -28,10 +47,10 @@ public override string Message
Confirm that your deployment folder contains one of the following assemblies:
NHibernate.ByteCode.LinFu.dll
NHibernate.ByteCode.Castle.dll";
string msg = "Unable to load type '" + typeName + "' during configuration of proxy factory class." + causes;
string msg = "Unable to load type '" + TypeName + "' during configuration of proxy factory class." + causes;

return msg;
}
}
}
}
}
56 changes: 34 additions & 22 deletions src/NHibernate/DuplicateMappingException.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
using System;
using System.Runtime.Serialization;
using System.Security;

namespace NHibernate
{
[Serializable]
public class DuplicateMappingException : MappingException
{
private readonly string type;
private readonly string name;

/// <summary>
/// The type of the duplicated object
/// </summary>
public string Type
{
get { return type; }
}

/// <summary>
/// The name of the duplicated object
/// </summary>
public string Name
{
get { return name; }
}

/// <summary>
/// Initializes a new instance of the <see cref="MappingException"/> class.
Expand All @@ -34,8 +17,8 @@ public string Name
public DuplicateMappingException(string customMessage, string type, string name)
: base(customMessage)
{
this.type = type;
this.name = name;
Type = type;
Name = name;
}

/// <summary>
Expand All @@ -44,7 +27,7 @@ public DuplicateMappingException(string customMessage, string type, string name)
/// <param name="name">The name of the duplicate object</param>
/// <param name="type">The type of the duplicate object</param>
public DuplicateMappingException(string type, string name)
: this(string.Format("Duplicate {0} mapping {1}", type, name), type, name)
: this($"Duplicate {type} mapping {name}", type, name)
{
}

Expand All @@ -62,6 +45,35 @@ public DuplicateMappingException(string type, string name)
public DuplicateMappingException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
foreach (var entry in info)
{
if (entry.Name == "Type")
{
Type = entry.Value?.ToString();
}
else if (entry.Name == "Name")
{
Name = entry.Value?.ToString();
}
}
}

[SecurityCritical]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("Type", Type);
info.AddValue("Name", Name);
}

/// <summary>
/// The type of the duplicated object
/// </summary>
public string Type { get; }

/// <summary>
/// The name of the duplicated object
/// </summary>
public string Name { get; }
}
}
}
33 changes: 23 additions & 10 deletions src/NHibernate/Exceptions/ConstraintViolationException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.Serialization;
using System.Security;

namespace NHibernate.Exceptions
{
Expand All @@ -10,29 +11,41 @@ namespace NHibernate.Exceptions
[Serializable]
public class ConstraintViolationException : ADOException
{
private readonly string constraintName;
public ConstraintViolationException(SerializationInfo info, StreamingContext context)
: base(info, context) {}

public ConstraintViolationException(string message, Exception innerException, string sql, string constraintName)
: base(message, innerException, sql)
{
this.constraintName = constraintName;
ConstraintName = constraintName;
}

public ConstraintViolationException(string message, Exception innerException, string constraintName)
: base(message, innerException)
{
this.constraintName = constraintName;
ConstraintName = constraintName;
}

public ConstraintViolationException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
foreach (var entry in info)
{
if (entry.Name == "ConstraintName")
{
ConstraintName = entry.Value?.ToString();
}
}
}

[SecurityCritical]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("ConstraintName", ConstraintName);
}

/// <summary>
/// Returns the name of the violated constraint, if known.
/// </summary>
/// <returns> The name of the violated constraint, or null if not known. </returns>
public string ConstraintName
{
get { return constraintName; }
}
public string ConstraintName { get; }
}
}
17 changes: 11 additions & 6 deletions src/NHibernate/Exceptions/SqlParseException.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using System;
using System.Runtime.Serialization;

namespace NHibernate.Exceptions
{
public class SqlParseException : Exception
{
[Serializable]
public class SqlParseException : Exception
{

public SqlParseException(string Message) : base(Message)
{
}
public SqlParseException(string message) : base(message)
{
}

}
protected SqlParseException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
6 changes: 6 additions & 0 deletions src/NHibernate/Hql/Ast/ANTLR/DetailedSemanticException.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Runtime.Serialization;

namespace NHibernate.Hql.Ast.ANTLR
{
[Serializable]
public class DetailedSemanticException : SemanticException
{
public DetailedSemanticException(string message) : base(message)
Expand All @@ -12,5 +14,9 @@ public DetailedSemanticException(string message, Exception inner)
: base(message, inner)
{
}

protected DetailedSemanticException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
10 changes: 9 additions & 1 deletion src/NHibernate/Hql/Ast/ANTLR/InvalidPathException.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
namespace NHibernate.Hql.Ast.ANTLR
using System;
using System.Runtime.Serialization;

namespace NHibernate.Hql.Ast.ANTLR
{
/// <summary>
/// Exception thrown when an invalid path is found in a query.
/// Author: josh
/// Ported by: Steve Strong
/// </summary>
[Serializable]
public class InvalidPathException : SemanticException
{
public InvalidPathException(string s) : base(s)
{
}

protected InvalidPathException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
7 changes: 6 additions & 1 deletion src/NHibernate/Hql/Ast/ANTLR/SemanticException.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using Antlr.Runtime;
using System.Runtime.Serialization;

namespace NHibernate.Hql.Ast.ANTLR
{
[Serializable]
public class SemanticException : QueryException
{
public SemanticException(string message) : base(message)
Expand All @@ -13,5 +14,9 @@ public SemanticException(string message, Exception inner)
: base(message, inner)
{
}

protected SemanticException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
8 changes: 7 additions & 1 deletion src/NHibernate/Hql/QueryExecutionRequestException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using System;
using System.Runtime.Serialization;

namespace NHibernate.Hql
{
[Serializable]
public class QueryExecutionRequestException : QueryException
{
public QueryExecutionRequestException(string message, string queryString) : base(message, queryString)
{
}

protected QueryExecutionRequestException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
}
28 changes: 24 additions & 4 deletions src/NHibernate/LazyInitializationException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.Serialization;
using System.Security;


namespace NHibernate
Expand All @@ -18,14 +19,14 @@ public class LazyInitializationException : HibernateException
/// <param name="entityId">The id of the entity where the exception was thrown</param>
/// <param name="message">The message that describes the error. </param>
public LazyInitializationException(string entityName, object entityId, string message)
: this(string.Format("Initializing[{0}#{1}]-{2}", entityName, entityId, message))
: this($"Initializing[{entityName}#{entityId}]-{message}")
{
EntityName = entityName;
EntityId = entityId;
}

public string EntityName { get; private set; }
public object EntityId { get; private set; }
public string EntityName { get; }
public object EntityId { get; }

/// <summary>
/// Initializes a new instance of the <see cref="LazyInitializationException"/> class.
Expand Down Expand Up @@ -76,6 +77,25 @@ public LazyInitializationException(string message, Exception innerException) : b
/// </param>
protected LazyInitializationException(SerializationInfo info, StreamingContext context) : base(info, context)
{
foreach (var entry in info)
{
if (entry.Name == "EntityName")
{
EntityName = entry.Value?.ToString();
}
else if (entry.Name == "EntityId")
{
EntityId = entry.Value;
}
}
}

[SecurityCritical]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("EntityName", EntityName);
info.AddValue("EntityId", EntityId, typeof(object));
}
}
}
}
Loading