Skip to content

Commit b9c7c70

Browse files
committed
fix #2140 add serializable to all exceptions
+ tests for all of these
1 parent 1fb56d4 commit b9c7c70

File tree

11 files changed

+238
-35
lines changed

11 files changed

+238
-35
lines changed
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Net;
4+
using System.Runtime.Serialization;
5+
using System.Security.Permissions;
36

47
namespace Elasticsearch.Net.Connection
58
{
6-
public class ConnectionException : System.Exception
9+
[Serializable]
10+
public class ConnectionException : Exception
711
{
812
public int HttpStatusCode { get; private set; }
913
public ConnectionException(int statusCode = -1, string response = null) : base(Enum.GetName(typeof(HttpStatusCode), statusCode))
1014
{
1115
this.HttpStatusCode = statusCode;
1216
}
17+
18+
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
19+
protected ConnectionException(SerializationInfo info, StreamingContext context) : base(info, context)
20+
{
21+
this.HttpStatusCode = info.GetInt32("HttpStatusCode");
22+
}
23+
24+
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
25+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
26+
{
27+
if (info == null) throw new ArgumentNullException(nameof(info));
28+
29+
info.AddValue("HttpStatusCode", this.HttpStatusCode);
30+
base.GetObjectData(info, context);
31+
}
1332
}
1433
}
Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,84 @@
11
using System;
22
using System.IO;
33
using System.Net;
4+
using System.Runtime.Serialization;
5+
using System.Security.Permissions;
46

57
namespace Elasticsearch.Net.Connection
68
{
9+
[Serializable]
710
public abstract class ElasticsearchAuthException : Exception
811
{
912
protected abstract string ExceptionType { get; }
1013
protected abstract int StatusCode { get; }
1114

15+
public ElasticsearchResponse<Stream> Response { get; private set; }
16+
1217
protected ElasticsearchAuthException(ElasticsearchResponse<Stream> response)
1318
{
1419
this.Response = response;
1520
}
1621

17-
internal ElasticsearchServerException ToElasticsearchServerException()
18-
{
19-
if (this.Response == null)
20-
return null;
21-
return new ElasticsearchServerException(this.StatusCode, this.ExceptionType);
22-
}
23-
public ElasticsearchResponse<Stream> Response { get; private set; }
22+
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
23+
protected ElasticsearchAuthException(SerializationInfo info, StreamingContext context) : base(info, context)
24+
{
25+
}
26+
27+
internal ElasticsearchServerException ToElasticsearchServerException() =>
28+
this.Response == null ? null : new ElasticsearchServerException(this.StatusCode, this.ExceptionType);
29+
30+
2431
}
2532

33+
[Serializable]
2634
public class ElasticsearchAuthorizationException : ElasticsearchAuthException
2735
{
2836
public ElasticsearchAuthorizationException(ElasticsearchResponse<Stream> response) : base(response) { }
2937

30-
protected override string ExceptionType { get { return "AuthorizationException"; } }
38+
protected override string ExceptionType => "AuthorizationException";
39+
40+
protected override int StatusCode => 403;
41+
42+
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
43+
protected ElasticsearchAuthorizationException(SerializationInfo info, StreamingContext context) : base(info, context)
44+
{
45+
}
46+
47+
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
48+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
49+
{
50+
if (info == null) throw new ArgumentNullException(nameof(info));
3151

32-
protected override int StatusCode { get { return 403; } }
52+
info.AddValue("ExceptionType", this.ExceptionType);
53+
info.AddValue("StatusCode", this.StatusCode);
54+
base.GetObjectData(info, context);
55+
}
3356
}
3457

3558

59+
[Serializable]
3660
public class ElasticsearchAuthenticationException : ElasticsearchAuthException
3761
{
38-
protected override string ExceptionType { get { return "AuthenticationException"; } }
62+
protected override string ExceptionType => "AuthenticationException";
3963

40-
protected override int StatusCode { get { return 401; } }
64+
protected override int StatusCode => 401;
4165

4266
public ElasticsearchAuthenticationException(ElasticsearchResponse<Stream> response) : base(response) { }
4367

68+
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
69+
protected ElasticsearchAuthenticationException(SerializationInfo info, StreamingContext context) : base(info, context)
70+
{
71+
}
72+
73+
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
74+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
75+
{
76+
if (info == null) throw new ArgumentNullException(nameof(info));
77+
78+
info.AddValue("ExceptionType", this.ExceptionType);
79+
info.AddValue("StatusCode", this.StatusCode);
80+
base.GetObjectData(info, context);
81+
}
82+
4483
}
4584
}

src/Elasticsearch.Net/Exceptions/ElasticsearchServerException.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Runtime.Serialization;
5+
using System.Security.Permissions;
46
using System.Text;
57
using System.Text.RegularExpressions;
68

79
namespace Elasticsearch.Net
810
{
11+
[Serializable]
912
public class ElasticsearchServerException : Exception
1013
{
1114
private static readonly Regex ExceptionSplitter = new Regex(@"^([^\[]*?)\[(.*)\]", RegexOptions.Singleline);
@@ -35,5 +38,23 @@ private static string ParseError(ElasticsearchServerError error)
3538
error.ExceptionType = matches.Groups[1].Value;
3639
return matches.Groups[2].Value;
3740
}
41+
42+
43+
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
44+
protected ElasticsearchServerException(SerializationInfo info, StreamingContext context) : base(info, context)
45+
{
46+
this.Status = info.GetInt32("Status");
47+
this.ExceptionType = info.GetString("ExceptionType");
48+
}
49+
50+
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
51+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
52+
{
53+
if (info == null) throw new ArgumentNullException(nameof(info));
54+
55+
info.AddValue("Status", this.Status);
56+
info.AddValue("ExceptionType", this.ExceptionType);
57+
base.GetObjectData(info, context);
58+
}
3859
}
3960
}
Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Runtime.Serialization;
45
using System.Text;
56

67
namespace Elasticsearch.Net.Exceptions
@@ -9,38 +10,46 @@ namespace Elasticsearch.Net.Exceptions
910
/// <summary>
1011
/// Thrown when a request has depleeded its max retry setting
1112
/// </summary>
13+
[Serializable]
1214
public class MaxRetryException : Exception
1315
{
1416

1517
public MaxRetryException(string message) : base(message) { }
1618

1719
public MaxRetryException(string message, Exception innerException) : base(message, innerException) { }
1820
public MaxRetryException(Exception innerException) : base(innerException.Message, innerException) { }
21+
22+
protected MaxRetryException(SerializationInfo info, StreamingContext context)
23+
: base(info, context) { }
24+
1925
}
2026

2127
/// <summary>
2228
/// Thrown when a sniff operation itself caused a maxrety exception
2329
/// </summary>
30+
[Serializable]
2431
public class SniffException : Exception
2532
{
26-
2733
public SniffException(MaxRetryException innerException)
28-
: base("Sniffing known nodes in the cluster caused a maxretry exception of its own", innerException)
29-
{
30-
31-
}
34+
: base("Sniffing known nodes in the cluster caused a maxretry exception of its own", innerException) { }
35+
36+
protected SniffException(SerializationInfo info, StreamingContext context)
37+
: base(info, context) { }
38+
3239
}
3340

3441
/// <summary>
3542
/// Thrown when a ping operation itself caused a maxrety exception
3643
/// </summary>
44+
[Serializable]
3745
public class PingException : Exception
3846
{
3947

4048
public PingException(Uri baseURi, Exception innerException)
41-
: base("Pinging {0} caused an exception".F(baseURi.ToString()), innerException)
42-
{
43-
44-
}
49+
: base("Pinging {0} caused an exception".F(baseURi.ToString()), innerException) { }
50+
51+
protected PingException(SerializationInfo info, StreamingContext context)
52+
: base(info, context) { }
53+
4554
}
4655
}
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
14
namespace Nest
25
{
36
/// <summary>
47
/// Occurs when an IElasticClient call does not have
58
/// enough information to dispatch into the raw client.
69
/// </summary>
7-
public class DispatchException : System.Exception
10+
[Serializable]
11+
public class DispatchException : Exception
812
{
9-
public DispatchException(string msg) : base(msg)
10-
{
11-
}
13+
public DispatchException(string msg) : base(msg) { }
14+
15+
public DispatchException(string msg, System.Exception exp) : base(msg, exp) { }
16+
17+
protected DispatchException(SerializationInfo info, StreamingContext context)
18+
: base(info, context) { }
1219

13-
public DispatchException(string msg, System.Exception exp) : base(msg, exp)
14-
{
15-
}
1620
}
1721
}

src/Nest/Exception/DslException.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
14
namespace Nest
25
{
3-
public class DslException : System.Exception
6+
[Serializable]
7+
public class DslException : Exception
48
{
5-
public DslException(string msg) : base(msg)
6-
{
7-
}
9+
public DslException(string msg) : base(msg) { }
10+
11+
public DslException(string msg, System.Exception exp) : base(msg, exp) { }
812

9-
public DslException(string msg, System.Exception exp) : base(msg, exp)
10-
{
11-
}
13+
protected DslException(SerializationInfo info, StreamingContext context) : base(info, context) { }
1214
}
1315
}

src/Nest/Exception/ReindexException.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
2+
using System.Runtime.Serialization;
3+
using System.Security.Permissions;
24
using Elasticsearch.Net;
35

46
namespace Nest
57
{
8+
[Serializable]
69
public class ReindexException: Exception
710
{
811
public IElasticsearchResponse Status { get; private set; }
@@ -11,5 +14,9 @@ public ReindexException(IElasticsearchResponse status, string message = null) :
1114
{
1215
this.Status = status;
1316
}
17+
18+
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
19+
protected ReindexException(SerializationInfo info, StreamingContext context) : base(info, context) { }
20+
1421
}
1522
}

src/Nest/Exception/RestoreException.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
2+
using System.Runtime.Serialization;
3+
using System.Security.Permissions;
24
using Elasticsearch.Net;
35

46
namespace Nest
57
{
8+
[Serializable]
69
public class RestoreException : Exception
710
{
811
public IElasticsearchResponse Status { get; private set; }
@@ -12,5 +15,9 @@ public RestoreException(IElasticsearchResponse status, string message = null)
1215
{
1316
this.Status = status;
1417
}
18+
19+
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
20+
protected RestoreException(SerializationInfo info, StreamingContext context) : base(info, context) { }
21+
1522
}
1623
}

src/Nest/Exception/SnapshotException.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
2+
using System.Runtime.Serialization;
3+
using System.Security.Permissions;
24
using Elasticsearch.Net;
35

46
namespace Nest
57
{
8+
[Serializable]
69
public class SnapshotException : Exception
710
{
811
public IElasticsearchResponse Status { get; private set; }
@@ -12,5 +15,9 @@ public SnapshotException(IElasticsearchResponse status, string message)
1215
{
1316
Status = status;
1417
}
18+
19+
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
20+
protected SnapshotException(SerializationInfo info, StreamingContext context) : base(info, context) { }
21+
1522
}
1623
}

0 commit comments

Comments
 (0)