From 79301c7fa61e1dae2651fa10a5841c3a8375341d Mon Sep 17 00:00:00 2001 From: mpilar Date: Tue, 12 Apr 2011 14:50:47 -0400 Subject: [PATCH] Wrap miscellaneous communication exceptions in MongoConnectionException --- Driver/Core/MongoServer.cs | 8 ++++---- Driver/Internal/DirectConnector.cs | 4 ++-- Driver/Internal/MongoConnection.cs | 11 +++++------ Driver/Internal/ReplicaSetConnector.cs | 4 ++-- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/Driver/Core/MongoServer.cs b/Driver/Core/MongoServer.cs index 0895c12..7a56b07 100644 --- a/Driver/Core/MongoServer.cs +++ b/Driver/Core/MongoServer.cs @@ -392,9 +392,9 @@ namespace MongoDB.Driver { throw new MongoInternalException("Invalid ConnectionMode"); } state = MongoServerState.Connected; - } catch { + } catch (Exception ex) { state = MongoServerState.Disconnected; - throw; + throw new MongoConnectionException("Problem Connecting to the Server", ex); } } } @@ -808,10 +808,10 @@ namespace MongoDB.Driver { try { connection.CheckAuthentication(this, database); // will authenticate if necessary - } catch (MongoAuthenticationException) { + } catch (MongoAuthenticationException ex) { // don't let the connection go to waste just because authentication failed connectionPool.ReleaseConnection(connection); - throw; + throw new MongoConnectionException("Problem Acquiring Connection from ConnectionPool", ex); } return connection; diff --git a/Driver/Internal/DirectConnector.cs b/Driver/Internal/DirectConnector.cs index b86c391..c211baa 100644 --- a/Driver/Internal/DirectConnector.cs +++ b/Driver/Internal/DirectConnector.cs @@ -100,9 +100,9 @@ namespace MongoDB.Driver.Internal { maxDocumentSize = isMasterResult.Response["maxBsonObjectSize", server.MaxDocumentSize].ToInt32(); maxMessageLength = Math.Max(MongoDefaults.MaxMessageLength, maxDocumentSize + 1024); // derived from maxDocumentSize - } catch { + } catch (Exception ex){ try { connection.Close(); } catch { } // ignore exceptions - throw; + throw new MongoConnectionException("Error Connecting to the Server.", ex); } this.connection = connection; diff --git a/Driver/Internal/MongoConnection.cs b/Driver/Internal/MongoConnection.cs index bb3f377..c05ddfd 100644 --- a/Driver/Internal/MongoConnection.cs +++ b/Driver/Internal/MongoConnection.cs @@ -334,8 +334,7 @@ namespace MongoDB.Driver.Internal { return reply; } } catch (Exception ex) { - HandleException(ex); - throw; + throw HandleException(ex); } } } @@ -377,8 +376,7 @@ namespace MongoDB.Driver.Internal { message.Buffer.WriteTo(networkStream); messageCounter++; } catch (Exception ex) { - HandleException(ex); - throw; + throw HandleException(ex); } SafeModeResult safeModeResult = null; @@ -411,7 +409,7 @@ namespace MongoDB.Driver.Internal { return tcpClient.GetStream(); } - private void HandleException( + private MongoConnectionException HandleException( Exception ex ) { // TODO: figure out which exceptions are more serious than others @@ -421,7 +419,7 @@ namespace MongoDB.Driver.Internal { // 3. the whole connection pool needs to be discarded // for now the only exception we know affects only one connection is FileFormatException // and there are no cases where the connection can continue to be used - + state = MongoConnectionState.Damaged; if (!(ex is FileFormatException)) { if (connectionPool != null) { @@ -430,6 +428,7 @@ namespace MongoDB.Driver.Internal { } catch { } // ignore any further exceptions } } + return new MongoConnectionException("Connection error", ex); } #endregion diff --git a/Driver/Internal/ReplicaSetConnector.cs b/Driver/Internal/ReplicaSetConnector.cs index 93a68cd..3ae462a 100644 --- a/Driver/Internal/ReplicaSetConnector.cs +++ b/Driver/Internal/ReplicaSetConnector.cs @@ -202,9 +202,9 @@ namespace MongoDB.Driver.Internal { throw new MongoConnectionException(message); } } - } catch { + } catch (Exception ex) { try { connection.Close(); } catch { } // ignore exceptions - throw; + throw new MongoConnectionException("Error Connecting to the Server.", ex); } } catch (Exception ex) { response.Exception = ex; -- 1.7.3.1.msysgit.0