mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Better exception handling
This commit is contained in:
parent
a8bbc87c1c
commit
1557645e94
@ -1,5 +1,8 @@
|
|||||||
from bigchaindb.exceptions import BigchainDBError
|
from bigchaindb.exceptions import BigchainDBError
|
||||||
|
|
||||||
|
|
||||||
|
class ConnectionError(BigchainDBError):
|
||||||
|
"""Exception raised when the connection to the DataBase fails."""
|
||||||
|
|
||||||
class DatabaseOpFailedError(BigchainDBError):
|
class DatabaseOpFailedError(BigchainDBError):
|
||||||
"""Exception for database operation errors."""
|
"""Exception for database operation errors."""
|
||||||
|
@ -8,7 +8,8 @@ from bigchaindb import backend
|
|||||||
from bigchaindb.backend.changefeed import ChangeFeed
|
from bigchaindb.backend.changefeed import ChangeFeed
|
||||||
from bigchaindb.backend.utils import module_dispatch_registrar
|
from bigchaindb.backend.utils import module_dispatch_registrar
|
||||||
from bigchaindb.backend.mongodb.connection import MongoDBConnection
|
from bigchaindb.backend.mongodb.connection import MongoDBConnection
|
||||||
|
from bigchaindb.backend.exceptions import (DatabaseOpFailedError,
|
||||||
|
ConnectionError)
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
register_changefeed = module_dispatch_registrar(backend.changefeed)
|
register_changefeed = module_dispatch_registrar(backend.changefeed)
|
||||||
@ -31,7 +32,8 @@ class MongoDBChangeFeed(ChangeFeed):
|
|||||||
break
|
break
|
||||||
except (errors.ConnectionFailure, errors.OperationFailure,
|
except (errors.ConnectionFailure, errors.OperationFailure,
|
||||||
errors.AutoReconnect,
|
errors.AutoReconnect,
|
||||||
errors.ServerSelectionTimeoutError) as exc:
|
errors.ServerSelectionTimeoutError,
|
||||||
|
DatabaseOpFailedError, ConnectionError) as exc:
|
||||||
logger.exception(exc)
|
logger.exception(exc)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ from pymongo import errors
|
|||||||
import bigchaindb
|
import bigchaindb
|
||||||
from bigchaindb.utils import Lazy
|
from bigchaindb.utils import Lazy
|
||||||
from bigchaindb.common import exceptions
|
from bigchaindb.common import exceptions
|
||||||
|
from bigchaindb.backend import exceptions as backend_exceptions
|
||||||
from bigchaindb.backend.connection import Connection
|
from bigchaindb.backend.connection import Connection
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -51,7 +52,7 @@ class MongoDBConnection(Connection):
|
|||||||
@property
|
@property
|
||||||
def conn(self):
|
def conn(self):
|
||||||
if self.connection is None:
|
if self.connection is None:
|
||||||
self.connection = self._connect()
|
self._connect()
|
||||||
return self.connection
|
return self.connection
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -77,23 +78,24 @@ class MongoDBConnection(Connection):
|
|||||||
try:
|
try:
|
||||||
# FYI: this might raise a `ServerSelectionTimeoutError`,
|
# FYI: this might raise a `ServerSelectionTimeoutError`,
|
||||||
# that is a subclass of `ConnectionFailure`.
|
# that is a subclass of `ConnectionFailure`.
|
||||||
connection = MongoClient(self.host,
|
self.connection = MongoClient(self.host,
|
||||||
self.port,
|
self.port,
|
||||||
replicaset=self.replicaset,
|
replicaset=self.replicaset,
|
||||||
serverselectiontimeoutms=self.connection_timeout)
|
serverselectiontimeoutms=self.connection_timeout)
|
||||||
|
|
||||||
# we should only return a connection if the replica set is
|
# we should only return a connection if the replica set is
|
||||||
# initialized. initialize_replica_set will check if the
|
# initialized. initialize_replica_set will check if the
|
||||||
# replica set is initialized else it will initialize it.
|
# replica set is initialized else it will initialize it.
|
||||||
initialize_replica_set(self.host, self.port, self.connection_timeout)
|
initialize_replica_set(self.host, self.port, self.connection_timeout)
|
||||||
return connection
|
|
||||||
except (errors.ConnectionFailure, errors.AutoReconnect) as exc:
|
except (errors.ConnectionFailure, errors.AutoReconnect) as exc:
|
||||||
logger.warning('Attempt %s/%s. Connection to %s:%s failed after %sms.',
|
logger.warning('Attempt %s/%s. Connection to %s:%s failed after %sms.',
|
||||||
attempt, self.max_tries if self.max_tries != 0 else '∞',
|
attempt, self.max_tries if self.max_tries != 0 else '∞',
|
||||||
self.host, self.port, self.connection_timeout)
|
self.host, self.port, self.connection_timeout)
|
||||||
if attempt == self.max_tries:
|
if attempt == self.max_tries:
|
||||||
logger.exception('Cannot connect to the Database. Giving up.')
|
logger.critical('Cannot connect to the Database. Giving up.')
|
||||||
raise errors.ConnectionFailure() from exc
|
raise backend_exceptions.ConnectionError() from exc
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
def collection(name):
|
def collection(name):
|
||||||
|
@ -23,7 +23,8 @@ from bigchaindb.utils import ProcessGroup
|
|||||||
from bigchaindb import backend
|
from bigchaindb import backend
|
||||||
from bigchaindb.backend import schema
|
from bigchaindb.backend import schema
|
||||||
from bigchaindb.backend.admin import set_replicas, set_shards
|
from bigchaindb.backend.admin import set_replicas, set_shards
|
||||||
from bigchaindb.backend.exceptions import DatabaseOpFailedError
|
from bigchaindb.backend.exceptions import (DatabaseOpFailedError,
|
||||||
|
ConnectionError)
|
||||||
from bigchaindb.commands import utils
|
from bigchaindb.commands import utils
|
||||||
from bigchaindb import processes
|
from bigchaindb import processes
|
||||||
|
|
||||||
@ -157,6 +158,8 @@ def run_init(args):
|
|||||||
except DatabaseAlreadyExists:
|
except DatabaseAlreadyExists:
|
||||||
print('The database already exists.', file=sys.stderr)
|
print('The database already exists.', file=sys.stderr)
|
||||||
print('If you wish to re-initialize it, first drop it.', file=sys.stderr)
|
print('If you wish to re-initialize it, first drop it.', file=sys.stderr)
|
||||||
|
except ConnectionError:
|
||||||
|
print('Cannot connect to the database.', file=sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
def run_drop(args):
|
def run_drop(args):
|
||||||
@ -201,6 +204,8 @@ def run_start(args):
|
|||||||
_run_init()
|
_run_init()
|
||||||
except DatabaseAlreadyExists:
|
except DatabaseAlreadyExists:
|
||||||
pass
|
pass
|
||||||
|
except ConnectionError:
|
||||||
|
print('Cannot connect to the database.', file=sys.stderr)
|
||||||
except KeypairNotFoundException:
|
except KeypairNotFoundException:
|
||||||
sys.exit("Can't start BigchainDB, no keypair found. "
|
sys.exit("Can't start BigchainDB, no keypair found. "
|
||||||
'Did you run `bigchaindb configure`?')
|
'Did you run `bigchaindb configure`?')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user