Skip to content

Commit 8e50ffb

Browse files
Michael Schurtermdirolf
authored andcommitted
Added base exception class to all MongoDB exceptions
1 parent ad07b2e commit 8e50ffb

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

pymongo/errors.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
"""Exceptions raised by the Mongo driver."""
1616

17+
class BaseMongoDBException(Exception):
18+
"""Common base class for all MongoDB exceptions"""
1719

18-
class ConnectionFailure(IOError):
20+
class ConnectionFailure(BaseMongoDBException, IOError):
1921
"""Raised when a connection to the database cannot be made or is lost.
2022
"""
2123

@@ -32,12 +34,12 @@ class AutoReconnect(ConnectionFailure):
3234
"""
3335

3436

35-
class ConfigurationError(Exception):
37+
class ConfigurationError(BaseMongoDBException):
3638
"""Raised when something is incorrectly configured.
3739
"""
3840

3941

40-
class OperationFailure(Exception):
42+
class OperationFailure(BaseMongoDBException):
4143
"""Raised when a database operation fails.
4244
"""
4345

@@ -49,36 +51,36 @@ class DuplicateKeyError(OperationFailure):
4951
"""
5052

5153

52-
class InvalidOperation(Exception):
54+
class InvalidOperation(BaseMongoDBException):
5355
"""Raised when a client attempts to perform an invalid operation.
5456
"""
5557

5658

57-
class CollectionInvalid(Exception):
59+
class CollectionInvalid(BaseMongoDBException):
5860
"""Raised when collection validation fails.
5961
"""
6062

6163

62-
class InvalidName(ValueError):
64+
class InvalidName(BaseMongoDBException, ValueError):
6365
"""Raised when an invalid name is used.
6466
"""
6567

6668

67-
class InvalidBSON(ValueError):
69+
class InvalidBSON(BaseMongoDBException, ValueError):
6870
"""Raised when trying to create a BSON object from invalid data.
6971
"""
7072

7173

72-
class InvalidStringData(ValueError):
74+
class InvalidStringData(BaseMongoDBException, ValueError):
7375
"""Raised when trying to encode a string containing non-UTF8 data.
7476
"""
7577

7678

77-
class InvalidDocument(ValueError):
79+
class InvalidDocument(BaseMongoDBException, ValueError):
7880
"""Raised when trying to create a BSON object from an invalid document.
7981
"""
8082

8183

82-
class InvalidId(ValueError):
84+
class InvalidId(BaseMongoDBException, ValueError):
8385
"""Raised when trying to create an ObjectId from invalid data.
8486
"""

test/test_errors.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""Test the errors module."""
4+
5+
import unittest
6+
7+
import pymongo
8+
from pymongo.errors import BaseMongoDBException
9+
10+
11+
class TestErrors(unittest.TestCase):
12+
def test_base_exception(self):
13+
self.assertRaises(BaseMongoDBException, pymongo.Connection, port=0)
14+
15+
if __name__ == '__main__':
16+
unittest.main()

0 commit comments

Comments
 (0)