Commit e118682d by Diana Huang

Merge pull request #32 from edx/diana/catch-more-errors

Catch BSON errors as well as PyMongo errors.
parents 3efbae60 af9a16b5
......@@ -7,6 +7,7 @@ import logging
import pymongo
from pymongo import MongoClient
from pymongo.errors import PyMongoError
from bson.errors import BSONError
log = logging.getLogger(__name__)
......@@ -87,8 +88,9 @@ class MongoBackend(object):
"""Insert the event in to the Mongo collection"""
try:
self.collection.insert(event, manipulate=False)
except PyMongoError:
# The event will be lost in case of a connection error.
except (PyMongoError, BSONError):
# 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
# during the next event.
msg = 'Error inserting to MongoDB event tracker backend'
......
......@@ -6,6 +6,7 @@ from mock import patch
from mock import sentinel
from pymongo.errors import PyMongoError
from bson.errors import BSONError
from eventtracking.backends.mongodb import MongoBackend
......@@ -47,8 +48,14 @@ class TestMongoBackend(TestCase):
backend = MongoBackend(user=sentinel.user, password=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.send({'test': 1})
# 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