Skip to content

Commit db61060

Browse files
committed
Fix exceptions serialization
1 parent d372ee3 commit db61060

13 files changed

+216
-85
lines changed

src/NHibernate.Test/MappingExceptions/PropertyNotFoundExceptionFixture.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using NHibernate.Cfg;
3+
using NHibernate.Util;
34
using NUnit.Framework;
45

56
namespace NHibernate.Test.MappingExceptions
@@ -44,5 +45,21 @@ public void ConstructWithNullType()
4445
new PropertyNotFoundException(null, "someField");
4546
new PropertyNotFoundException(null, "SomeProperty", "getter");
4647
}
48+
49+
[Test]
50+
public void IsSerializable()
51+
{
52+
NHAssert.IsSerializable(new PropertyNotFoundException(null, "someField"));
53+
NHAssert.IsSerializable(new PropertyNotFoundException(null, "SomeProperty", "getter"));
54+
}
55+
56+
[Test]
57+
public void SerializeWithType()
58+
{
59+
var bytes = SerializationHelper.Serialize(new PropertyNotFoundException(typeof(PropertyNotFoundExceptionFixture), "SomeProperty", "getter"));
60+
var pnfe = (PropertyNotFoundException) SerializationHelper.Deserialize(bytes);
61+
62+
Assert.That(pnfe.TargetType, Is.EqualTo(typeof(PropertyNotFoundExceptionFixture)));
63+
}
4764
}
48-
}
65+
}
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
using System;
22
using System.Runtime.Serialization;
3+
using System.Security;
34

45
namespace NHibernate.Bytecode
56
{
67
[Serializable]
78
public class UnableToLoadProxyFactoryFactoryException : HibernateByteCodeException
89
{
9-
private readonly string typeName;
10+
1011
public UnableToLoadProxyFactoryFactoryException(string typeName, Exception inner)
1112
: base("", inner)
1213
{
13-
this.typeName = typeName;
14+
TypeName = typeName;
15+
}
16+
17+
protected UnableToLoadProxyFactoryFactoryException(SerializationInfo info, StreamingContext context)
18+
: base(info, context)
19+
{
20+
foreach (var entry in info)
21+
{
22+
if (entry.Name == "TypeName")
23+
{
24+
TypeName = entry.Value?.ToString();
25+
}
26+
}
27+
}
28+
29+
[SecurityCritical]
30+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
31+
{
32+
base.GetObjectData(info, context);
33+
info.AddValue("TypeName", TypeName);
1434
}
1535

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

3352
return msg;
3453
}
3554
}
3655
}
37-
}
56+
}
Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
using System;
22
using System.Runtime.Serialization;
3+
using System.Security;
34

45
namespace NHibernate
56
{
67
[Serializable]
78
public class DuplicateMappingException : MappingException
89
{
9-
private readonly string type;
10-
private readonly string name;
11-
12-
/// <summary>
13-
/// The type of the duplicated object
14-
/// </summary>
15-
public string Type
16-
{
17-
get { return type; }
18-
}
19-
20-
/// <summary>
21-
/// The name of the duplicated object
22-
/// </summary>
23-
public string Name
24-
{
25-
get { return name; }
26-
}
2710

2811
/// <summary>
2912
/// Initializes a new instance of the <see cref="MappingException"/> class.
@@ -34,8 +17,8 @@ public string Name
3417
public DuplicateMappingException(string customMessage, string type, string name)
3518
: base(customMessage)
3619
{
37-
this.type = type;
38-
this.name = name;
20+
Type = type;
21+
Name = name;
3922
}
4023

4124
/// <summary>
@@ -44,7 +27,7 @@ public DuplicateMappingException(string customMessage, string type, string name)
4427
/// <param name="name">The name of the duplicate object</param>
4528
/// <param name="type">The type of the duplicate object</param>
4629
public DuplicateMappingException(string type, string name)
47-
: this(string.Format("Duplicate {0} mapping {1}", type, name), type, name)
30+
: this($"Duplicate {type} mapping {name}", type, name)
4831
{
4932
}
5033

@@ -62,6 +45,35 @@ public DuplicateMappingException(string type, string name)
6245
public DuplicateMappingException(SerializationInfo info, StreamingContext context)
6346
: base(info, context)
6447
{
48+
foreach (var entry in info)
49+
{
50+
if (entry.Name == "Type")
51+
{
52+
Type = entry.Value?.ToString();
53+
}
54+
else if (entry.Name == "Name")
55+
{
56+
Name = entry.Value?.ToString();
57+
}
58+
}
6559
}
60+
61+
[SecurityCritical]
62+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
63+
{
64+
base.GetObjectData(info, context);
65+
info.AddValue("Type", Type);
66+
info.AddValue("Name", Name);
67+
}
68+
69+
/// <summary>
70+
/// The type of the duplicated object
71+
/// </summary>
72+
public string Type { get; }
73+
74+
/// <summary>
75+
/// The name of the duplicated object
76+
/// </summary>
77+
public string Name { get; }
6678
}
67-
}
79+
}
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.Serialization;
3+
using System.Security;
34

45
namespace NHibernate.Exceptions
56
{
@@ -10,29 +11,41 @@ namespace NHibernate.Exceptions
1011
[Serializable]
1112
public class ConstraintViolationException : ADOException
1213
{
13-
private readonly string constraintName;
14-
public ConstraintViolationException(SerializationInfo info, StreamingContext context)
15-
: base(info, context) {}
16-
1714
public ConstraintViolationException(string message, Exception innerException, string sql, string constraintName)
1815
: base(message, innerException, sql)
1916
{
20-
this.constraintName = constraintName;
17+
ConstraintName = constraintName;
2118
}
2219

2320
public ConstraintViolationException(string message, Exception innerException, string constraintName)
2421
: base(message, innerException)
2522
{
26-
this.constraintName = constraintName;
23+
ConstraintName = constraintName;
24+
}
25+
26+
public ConstraintViolationException(SerializationInfo info, StreamingContext context)
27+
: base(info, context)
28+
{
29+
foreach (var entry in info)
30+
{
31+
if (entry.Name == "ConstraintName")
32+
{
33+
ConstraintName = entry.Value?.ToString();
34+
}
35+
}
36+
}
37+
38+
[SecurityCritical]
39+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
40+
{
41+
base.GetObjectData(info, context);
42+
info.AddValue("ConstraintName", ConstraintName);
2743
}
2844

2945
/// <summary>
3046
/// Returns the name of the violated constraint, if known.
3147
/// </summary>
3248
/// <returns> The name of the violated constraint, or null if not known. </returns>
33-
public string ConstraintName
34-
{
35-
get { return constraintName; }
36-
}
49+
public string ConstraintName { get; }
3750
}
3851
}
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
using System;
2+
using System.Runtime.Serialization;
23

34
namespace NHibernate.Exceptions
45
{
5-
public class SqlParseException : Exception
6-
{
6+
[Serializable]
7+
public class SqlParseException : Exception
8+
{
79

8-
public SqlParseException(string Message) : base(Message)
9-
{
10-
}
10+
public SqlParseException(string message) : base(message)
11+
{
12+
}
1113

12-
}
14+
protected SqlParseException(SerializationInfo info, StreamingContext context) : base(info, context)
15+
{
16+
}
17+
}
1318
}

src/NHibernate/Hql/Ast/ANTLR/DetailedSemanticException.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Runtime.Serialization;
23

34
namespace NHibernate.Hql.Ast.ANTLR
45
{
6+
[Serializable]
57
public class DetailedSemanticException : SemanticException
68
{
79
public DetailedSemanticException(string message) : base(message)
@@ -12,5 +14,9 @@ public DetailedSemanticException(string message, Exception inner)
1214
: base(message, inner)
1315
{
1416
}
17+
18+
protected DetailedSemanticException(SerializationInfo info, StreamingContext context) : base(info, context)
19+
{
20+
}
1521
}
1622
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
namespace NHibernate.Hql.Ast.ANTLR
1+
using System;
2+
using System.Runtime.Serialization;
3+
4+
namespace NHibernate.Hql.Ast.ANTLR
25
{
36
/// <summary>
47
/// Exception thrown when an invalid path is found in a query.
58
/// Author: josh
69
/// Ported by: Steve Strong
710
/// </summary>
11+
[Serializable]
812
public class InvalidPathException : SemanticException
913
{
1014
public InvalidPathException(string s) : base(s)
1115
{
1216
}
17+
18+
protected InvalidPathException(SerializationInfo info, StreamingContext context) : base(info, context)
19+
{
20+
}
1321
}
1422
}

src/NHibernate/Hql/Ast/ANTLR/SemanticException.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System;
2-
using Antlr.Runtime;
2+
using System.Runtime.Serialization;
33

44
namespace NHibernate.Hql.Ast.ANTLR
55
{
6+
[Serializable]
67
public class SemanticException : QueryException
78
{
89
public SemanticException(string message) : base(message)
@@ -13,5 +14,9 @@ public SemanticException(string message, Exception inner)
1314
: base(message, inner)
1415
{
1516
}
17+
18+
protected SemanticException(SerializationInfo info, StreamingContext context) : base(info, context)
19+
{
20+
}
1621
}
1722
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
using System;
2+
using System.Runtime.Serialization;
23

34
namespace NHibernate.Hql
45
{
6+
[Serializable]
57
public class QueryExecutionRequestException : QueryException
68
{
79
public QueryExecutionRequestException(string message, string queryString) : base(message, queryString)
810
{
911
}
12+
13+
protected QueryExecutionRequestException(SerializationInfo info, StreamingContext context) : base(info, context)
14+
{
15+
}
1016
}
11-
}
17+
}

src/NHibernate/LazyInitializationException.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.Serialization;
3+
using System.Security;
34

45

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

27-
public string EntityName { get; private set; }
28-
public object EntityId { get; private set; }
28+
public string EntityName { get; }
29+
public object EntityId { get; }
2930

3031
/// <summary>
3132
/// Initializes a new instance of the <see cref="LazyInitializationException"/> class.
@@ -76,6 +77,25 @@ public LazyInitializationException(string message, Exception innerException) : b
7677
/// </param>
7778
protected LazyInitializationException(SerializationInfo info, StreamingContext context) : base(info, context)
7879
{
80+
foreach (var entry in info)
81+
{
82+
if (entry.Name == "EntityName")
83+
{
84+
EntityName = entry.Value?.ToString();
85+
}
86+
else if (entry.Name == "EntityId")
87+
{
88+
EntityId = entry.Value;
89+
}
90+
}
91+
}
92+
93+
[SecurityCritical]
94+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
95+
{
96+
base.GetObjectData(info, context);
97+
info.AddValue("EntityName", EntityName);
98+
info.AddValue("EntityId", EntityId, typeof(object));
7999
}
80100
}
81-
}
101+
}

0 commit comments

Comments
 (0)