3
3
using System . IO ;
4
4
using System . Linq ;
5
5
using System . Net ;
6
+ using System . Runtime . InteropServices ;
6
7
using System . Text ;
7
8
using System . Text . RegularExpressions ;
8
9
@@ -12,28 +13,33 @@ public class ElasticsearchServerError
12
13
{
13
14
public int Status { get ; set ; }
14
15
public string Error { get ; set ; }
16
+ public string ExceptionType { get ; set ; }
15
17
16
18
}
17
19
18
20
public class ElasticsearchServerException : Exception
19
21
{
20
- private static Regex ExceptionSplitter = new Regex ( @"^([^\[]*?)\[(.*)\]" , RegexOptions . Singleline ) ;
22
+ private static readonly Regex ExceptionSplitter = new Regex ( @"^([^\[]*?)\[(.*)\]" , RegexOptions . Singleline ) ;
23
+ private static readonly string _couldNotParseServerException = "Could not parse server exception" ;
21
24
22
25
public int Status { get ; set ; }
23
- public string Error { get ; set ; }
24
26
public string ExceptionType { get ; set ; }
25
- public ElasticsearchServerException ( ElasticsearchServerError error )
27
+ public ElasticsearchServerException ( ElasticsearchServerError error ) : base ( ParseError ( error ) )
26
28
{
27
- if ( error == null ) return ;
28
29
this . Status = error . Status ;
29
- if ( error . Error . IsNullOrEmpty ( ) ) return ;
30
+ this . ExceptionType = error . ExceptionType ;
31
+ }
32
+ //iffy side effect assignment to exceptionType needed so that we simply return message to the
33
+ //base constructor.
34
+ private static string ParseError ( ElasticsearchServerError error )
35
+ {
36
+ if ( error == null ) return _couldNotParseServerException ;
37
+ if ( error . Error . IsNullOrEmpty ( ) ) return _couldNotParseServerException ;
30
38
var matches = ExceptionSplitter . Match ( error . Error ) ;
31
- if ( matches . Groups . Count == 3 )
32
- {
33
- this . Error = matches . Groups [ 2 ] . Value ;
34
- this . ExceptionType = matches . Groups [ 1 ] . Value ;
35
- }
36
- else this . Error = error . Error ;
39
+ if ( matches . Groups . Count != 3 ) return _couldNotParseServerException ;
40
+
41
+ error . ExceptionType = matches . Groups [ 1 ] . Value ;
42
+ return matches . Groups [ 2 ] . Value ;
37
43
}
38
44
}
39
45
}
0 commit comments