mongodb / flask-pymongo

PyMongo support for Flask applications
BSD 2-Clause "Simplified" License
721 stars 175 forks source link

Error Handling #135

Closed npcode15 closed 5 years ago

npcode15 commented 5 years ago

Hello, I am having trouble with error handling.

The stack trace and web searches show that the following class location: "pymongo.errors.DuplicateKeyError" or "pymongo.errors.WriteError"

But, when I am using this, the application is giving me the following error: "TypeError: catching classes that do not inherit from BaseException is not allowed"

I have tried using

try: // code except mongo.errors.DuplicateKeyError except mongo.db.errors.DuplicateKeyError except mongo.cx.errors.DuplicateKeyError except mongo.errors.DuplicateKeyError

(where mongo is instantiated correctly in the app. I am able to write to the database.)

I would really appreciate any help regarding this issue. Thank you.

dcrosta commented 5 years ago

You'll need to import and use the Exception from pymongo itself, not from the mongo object that's created by Flask-PyMongo. Try:

from pymongo.errors import DuplicateKeyError

# later
try:
    # ...
except DuplicateKeyError as dke:
    # handle the exception
npcode15 commented 5 years ago

@dcrosta thank you for the response.