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 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: error - no module named bson - M220P: MongoDB for Python Developers - MongoDB University Discussion Forum


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

mflix\ in get_movie
id = ObjectId(id)
envmflix\lib\site-packages\bson\ in init
envmflix\lib\site-packages\bson\ in __validate

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\ 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.