Skip to content

Commit c8e64f4

Browse files
author
Mike Dirolf
committed
Use PyMongoError instead of BaseMongoDBException
Note that this is changing the exception hierarchy somewhat as well, as I would rather not muck things up with multiple inheritance. This is a breaking change if you depend on ConnectionFailure being an IOError or InvalidBSON being a ValueError, for example. If this creates issues for anybody please let us know and we can figure out a better workaround. Also add Michael to contributors list.
1 parent 8e50ffb commit c8e64f4

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

doc/contributors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ The following is a list of people who have contributed to
1616
- Andrey Fedorov (andreyf)
1717
- Joshua Roesslein (joshthecoder)
1818
- Gregg Lind (gregglind)
19+
- Michael Schurter (schmichael)

pymongo/errors.py

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

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

17-
class BaseMongoDBException(Exception):
18-
"""Common base class for all MongoDB exceptions"""
17+
class PyMongoError(Exception):
18+
"""Base class for all PyMongo exceptions.
1919
20-
class ConnectionFailure(BaseMongoDBException, IOError):
20+
.. versionadded:: 1.3+
21+
"""
22+
23+
class ConnectionFailure(PyMongoError):
2124
"""Raised when a connection to the database cannot be made or is lost.
2225
"""
2326

@@ -34,12 +37,12 @@ class AutoReconnect(ConnectionFailure):
3437
"""
3538

3639

37-
class ConfigurationError(BaseMongoDBException):
40+
class ConfigurationError(PyMongoError):
3841
"""Raised when something is incorrectly configured.
3942
"""
4043

4144

42-
class OperationFailure(BaseMongoDBException):
45+
class OperationFailure(PyMongoError):
4346
"""Raised when a database operation fails.
4447
"""
4548

@@ -51,36 +54,36 @@ class DuplicateKeyError(OperationFailure):
5154
"""
5255

5356

54-
class InvalidOperation(BaseMongoDBException):
57+
class InvalidOperation(PyMongoError):
5558
"""Raised when a client attempts to perform an invalid operation.
5659
"""
5760

5861

59-
class CollectionInvalid(BaseMongoDBException):
62+
class CollectionInvalid(PyMongoError):
6063
"""Raised when collection validation fails.
6164
"""
6265

6366

64-
class InvalidName(BaseMongoDBException, ValueError):
67+
class InvalidName(PyMongoError):
6568
"""Raised when an invalid name is used.
6669
"""
6770

6871

69-
class InvalidBSON(BaseMongoDBException, ValueError):
72+
class InvalidBSON(PyMongoError):
7073
"""Raised when trying to create a BSON object from invalid data.
7174
"""
7275

7376

74-
class InvalidStringData(BaseMongoDBException, ValueError):
77+
class InvalidStringData(PyMongoError):
7578
"""Raised when trying to encode a string containing non-UTF8 data.
7679
"""
7780

7881

79-
class InvalidDocument(BaseMongoDBException, ValueError):
82+
class InvalidDocument(PyMongoError):
8083
"""Raised when trying to create a BSON object from an invalid document.
8184
"""
8285

8386

84-
class InvalidId(BaseMongoDBException, ValueError):
87+
class InvalidId(PyMongoError):
8588
"""Raised when trying to create an ObjectId from invalid data.
8689
"""

test/test_collection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
"""Test the collection module."""
16+
1617
import warnings
1718
import unittest
1819
import re

test/test_errors.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1-
# -*- coding: utf-8 -*-
1+
# Copyright 2009 10gen, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
214

315
"""Test the errors module."""
416

517
import unittest
18+
import sys
19+
sys.path[0:0] = [""]
620

7-
import pymongo
8-
from pymongo.errors import BaseMongoDBException
21+
from pymongo import Connection
22+
from pymongo.errors import PyMongoError
923

1024

1125
class TestErrors(unittest.TestCase):
26+
1227
def test_base_exception(self):
13-
self.assertRaises(BaseMongoDBException, pymongo.Connection, port=0)
28+
self.assertRaises(PyMongoError, Connection, port=0)
29+
1430

1531
if __name__ == '__main__':
1632
unittest.main()

0 commit comments

Comments
 (0)