@@ -35,27 +35,28 @@ internal static class ExceptionMapper
35
35
/// </returns>
36
36
public static Exception Map ( ConnectionId connectionId , BsonDocument response )
37
37
{
38
- BsonValue code ;
39
- if ( response . TryGetValue ( "code" , out code ) && code . IsNumeric )
38
+ if ( response != null )
40
39
{
41
- switch ( code . ToInt32 ( ) )
40
+ if ( response . TryGetValue ( "code" , out var code ) && code . IsNumeric )
42
41
{
43
- case 50 :
44
- case 13475 :
45
- case 16986 :
46
- case 16712 :
47
- return new MongoExecutionTimeoutException ( connectionId , message : "Operation exceeded time limit." , response ) ;
42
+ switch ( code . ToInt32 ( ) )
43
+ {
44
+ case 50 :
45
+ case 13475 :
46
+ case 16986 :
47
+ case 16712 :
48
+ return new MongoExecutionTimeoutException ( connectionId , message : "Operation exceeded time limit." , response ) ;
49
+ }
48
50
}
49
- }
50
51
51
- // the server sometimes sends a response that is missing the "code" field but does have an "errmsg" field
52
- BsonValue errmsg ;
53
- if ( response . TryGetValue ( "errmsg" , out errmsg ) && errmsg . IsString )
54
- {
55
- if ( errmsg . AsString . Contains ( "exceeded time limit" ) ||
56
- errmsg . AsString . Contains ( "execution terminated" ) )
52
+ // the server sometimes sends a response that is missing the "code" field but does have an "errmsg" field
53
+ if ( response . TryGetValue ( "errmsg" , out var errmsg ) && errmsg . IsString )
57
54
{
58
- return new MongoExecutionTimeoutException ( connectionId , message : "Operation exceeded time limit." ) ;
55
+ if ( errmsg . AsString . Contains ( "exceeded time limit" ) ||
56
+ errmsg . AsString . Contains ( "execution terminated" ) )
57
+ {
58
+ return new MongoExecutionTimeoutException ( connectionId , message : "Operation exceeded time limit." ) ;
59
+ }
59
60
}
60
61
}
61
62
@@ -72,38 +73,40 @@ public static Exception Map(ConnectionId connectionId, BsonDocument response)
72
73
/// </returns>
73
74
public static Exception Map ( ConnectionId connectionId , WriteConcernResult writeConcernResult )
74
75
{
75
- var code = GetCode ( writeConcernResult . Response ) ;
76
- if ( code . HasValue )
76
+ if ( writeConcernResult != null )
77
77
{
78
- switch ( code . Value )
78
+ var code = GetCode ( writeConcernResult . Response ) ;
79
+ if ( code . HasValue )
79
80
{
80
- case 11000 :
81
- case 11001 :
82
- case 12582 :
83
- var errorMessage = string . Format (
84
- "WriteConcern detected an error '{0}'. (Response was {1})." ,
85
- writeConcernResult . LastErrorMessage , writeConcernResult . Response . ToJson ( ) ) ;
86
- return new MongoDuplicateKeyException ( connectionId , errorMessage , writeConcernResult ) ;
81
+ switch ( code . Value )
82
+ {
83
+ case 11000 :
84
+ case 11001 :
85
+ case 12582 :
86
+ var errorMessage = string . Format (
87
+ "WriteConcern detected an error '{0}'. (Response was {1})." ,
88
+ writeConcernResult . LastErrorMessage , writeConcernResult . Response . ToJson ( ) ) ;
89
+ return new MongoDuplicateKeyException ( connectionId , errorMessage , writeConcernResult ) ;
90
+ }
87
91
}
88
- }
89
-
90
- bool ok = writeConcernResult . Response . GetValue ( "ok" , false ) . ToBoolean ( ) ;
91
92
92
- if ( ! ok )
93
- {
94
- var errorMessage = string . Format (
95
- "WriteConcern detected an error '{0}'. (Response was {1})." ,
96
- writeConcernResult . LastErrorMessage , writeConcernResult . Response . ToJson ( ) ) ;
97
- return new MongoWriteConcernException ( connectionId , errorMessage , writeConcernResult ) ;
98
- }
93
+ var ok = writeConcernResult . Response . GetValue ( "ok" , false ) . ToBoolean ( ) ;
94
+ if ( ! ok )
95
+ {
96
+ var errorMessage = string . Format (
97
+ "WriteConcern detected an error '{0}'. (Response was {1})." ,
98
+ writeConcernResult . LastErrorMessage , writeConcernResult . Response . ToJson ( ) ) ;
99
+ return new MongoWriteConcernException ( connectionId , errorMessage , writeConcernResult ) ;
100
+ }
99
101
100
- if ( writeConcernResult . HasLastErrorMessage )
101
- {
102
- var errorMessage = string . Format (
103
- "WriteConcern detected an error '{0}'. (Response was {1})." ,
104
- writeConcernResult . LastErrorMessage ,
105
- writeConcernResult . Response . ToJson ( ) ) ;
106
- return new MongoWriteConcernException ( connectionId , errorMessage , writeConcernResult ) ;
102
+ if ( writeConcernResult . HasLastErrorMessage )
103
+ {
104
+ var errorMessage = string . Format (
105
+ "WriteConcern detected an error '{0}'. (Response was {1})." ,
106
+ writeConcernResult . LastErrorMessage ,
107
+ writeConcernResult . Response . ToJson ( ) ) ;
108
+ return new MongoWriteConcernException ( connectionId , errorMessage , writeConcernResult ) ;
109
+ }
107
110
}
108
111
109
112
return null ;
0 commit comments