Error Handling
npcode15 opened this issue · comments
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.
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@dcrosta thank you for the response.