Error handling - bson import but not used pyflakes warning

Not sure why I get this error
NameError:name ‘bson’ is not defined

It’s imported in the db.py file at the top. Have not altered it.

‘’’

If an invalid ID is passed to get_movie, it should return None.

except (bson.errors.InvalidId) as _:

    """
    Ticket: Error Handling

    Handle the InvalidId exception from the BSON library the same way as the
    StopIteration exception is handled. Both exceptions should result in
    `get_movie` returning None.
    """

    return None

except Exception as e:
    return {}

‘’’

Hi @Chris_Duff ,

Please look at the following post:

Run.py error - no module named bson - M220P: MongoDB for Python Developers - MongoDB University Discussion Forum

Kanika

Hi,
That page is marked private. :frowning:
I am in the python version of this course. Are you the person to 'talk to" or is it Eoin?
I seem to be having a problem where the except is not being executed. I have tried multiple permutations of
except InvalidId:
except errors.InvalidId as id:
except InvalidId as id:
except bson.errors.Invalidid:
etc, but I always get the error message from pytest -m error_handling
tests\test_error_handling.py:13:


mflix\db.py:266: in get_movie
id = ObjectId(id)
envmflix\lib\site-packages\bson\objectid.py:125: in init
self.__validate(oid)
envmflix\lib\site-packages\bson\objectid.py:221: in __validate
_raise_invalid_id(oid)


oid = ‘foobar’

def _raise_invalid_id(oid):
  raise InvalidId(
        "%r is not a valid ObjectId, it must be a 12-byte input"
        " or a 24-character hex string 1" % oid)

E bson.errors.InvalidId: ‘foobar’ is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string 1

envmflix\lib\site-packages\bson\objectid.py:65: InvalidId
---------------------------- Captured stdout call -----------------------------
Movie collection id foobar
============================= 42 tests deselected =============================
=================== 1 failed, 42 deselected in 0.53 seconds ===================

I don’t know if this is a version problem or what. Please advise.
Thanks.