Commit af9a16b5 by Diana Huang

Catch BSON errors as well as PyMongo errors.

parent 3efbae60
...@@ -7,6 +7,7 @@ import logging ...@@ -7,6 +7,7 @@ import logging
import pymongo import pymongo
from pymongo import MongoClient from pymongo import MongoClient
from pymongo.errors import PyMongoError from pymongo.errors import PyMongoError
from bson.errors import BSONError
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -87,8 +88,9 @@ class MongoBackend(object): ...@@ -87,8 +88,9 @@ class MongoBackend(object):
"""Insert the event in to the Mongo collection""" """Insert the event in to the Mongo collection"""
try: try:
self.collection.insert(event, manipulate=False) self.collection.insert(event, manipulate=False)
except PyMongoError: except (PyMongoError, BSONError):
# The event will be lost in case of a connection error. # The event will be lost in case of a connection error or any error
# that occurs when trying to insert the event into Mongo.
# pymongo will re-connect/re-authenticate automatically # pymongo will re-connect/re-authenticate automatically
# during the next event. # during the next event.
msg = 'Error inserting to MongoDB event tracker backend' msg = 'Error inserting to MongoDB event tracker backend'
......
...@@ -6,6 +6,7 @@ from mock import patch ...@@ -6,6 +6,7 @@ from mock import patch
from mock import sentinel from mock import sentinel
from pymongo.errors import PyMongoError from pymongo.errors import PyMongoError
from bson.errors import BSONError
from eventtracking.backends.mongodb import MongoBackend from eventtracking.backends.mongodb import MongoBackend
...@@ -47,8 +48,14 @@ class TestMongoBackend(TestCase): ...@@ -47,8 +48,14 @@ class TestMongoBackend(TestCase):
backend = MongoBackend(user=sentinel.user, password=sentinel.password) backend = MongoBackend(user=sentinel.user, password=sentinel.password)
backend.database.authenticate.assert_called_once_with(sentinel.user, sentinel.password) backend.database.authenticate.assert_called_once_with(sentinel.user, sentinel.password)
def test_mongo_insertion_error(self): def test_mongo_pymongo_insertion_error(self):
self.backend.collection.insert.side_effect = PyMongoError self.backend.collection.insert.side_effect = PyMongoError
self.backend.send({'test': 1}) self.backend.send({'test': 1})
# Ensure this error is caught # Ensure this error is caught
def test_mongo_bson_insertion_error(self):
self.backend.collection.insert.side_effect = BSONError
self.backend.send({'test': 1})
# Ensure this error is caught
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment