@@ -35,27 +35,28 @@ internal static class ExceptionMapper
3535 /// </returns>
3636 public static Exception Map ( ConnectionId connectionId , BsonDocument response )
3737 {
38- BsonValue code ;
39- if ( response . TryGetValue ( "code" , out code ) && code . IsNumeric )
38+ if ( response != null )
4039 {
41- switch ( code . ToInt32 ( ) )
40+ if ( response . TryGetValue ( "code" , out var code ) && code . IsNumeric )
4241 {
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+ }
4850 }
49- }
5051
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 )
5754 {
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+ }
5960 }
6061 }
6162
@@ -72,38 +73,40 @@ public static Exception Map(ConnectionId connectionId, BsonDocument response)
7273 /// </returns>
7374 public static Exception Map ( ConnectionId connectionId , WriteConcernResult writeConcernResult )
7475 {
75- var code = GetCode ( writeConcernResult . Response ) ;
76- if ( code . HasValue )
76+ if ( writeConcernResult != null )
7777 {
78- switch ( code . Value )
78+ var code = GetCode ( writeConcernResult . Response ) ;
79+ if ( code . HasValue )
7980 {
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+ }
8791 }
88- }
89-
90- bool ok = writeConcernResult . Response . GetValue ( "ok" , false ) . ToBoolean ( ) ;
9192
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+ }
99101
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+ }
107110 }
108111
109112 return null ;
0 commit comments