Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Invalid Document error message to include doc #1854

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

navjots18
Copy link

@navjots18 navjots18 commented Sep 12, 2024

Updated bson and cbson code to add the doc in the Invalid Document error message and added Unit test cases.

Note: Make sure to run to python _setup.py build_ext --inplace to install C file changes

@navjots18 navjots18 changed the title Updated Invalid Document error message to have doc Updated Invalid Document error message to include doc Sep 13, 2024
@blink1073
Copy link
Member

Thanks @navjots18! It looks like there is a segmentation fault in the C implementation. Are you able to reproduce that failure locally? I can take a closer look on Monday, but https://dnmtechs.com/handling-segmentation-fault-errors-in-python-3-programming/ is a good resource.

@navjots18
Copy link
Author

Hi @blink1073 , it was happening because i was assigning some variables to eType and eValue and then using Py_DECREF on the variables. Changed this which fixed the segmentation error on my local

@blink1073 blink1073 requested review from blink1073 and removed request for Jibola September 13, 2024 16:29
PyErr_Fetch(&etype, &evalue, &etrace);
PyObject *InvalidDocument = _error("InvalidDocument");

if (PyErr_GivenExceptionMatches(etype, InvalidDocument)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please reduce the nesting by using if (InvalidDocument && PyErr_GivenExceptionMatches(etype, InvalidDocument))?

try:
elements.append(_element_to_bson(key, value, check_keys, opts))
except InvalidDocument as err:
raise InvalidDocument(f"Invalid document {doc} | {err}") from err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is the pattern we want, in the C version of this code we recursively call write_dict() for subdocuments which means if we end up erroring in a nested field the error will be something like:
Invalid document {"a": {"b": {"c": ...}}} | Invalid document {"b": {"c": ...}} | Invalid document {"c": ...} | ...

Is that the intended behavior?

Copy link
Author

@navjots18 navjots18 Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ShaneHarvey You're right, we can check the top_level param to prevent this from happening

if (PyErr_Occurred()) {
PyObject *etype, *evalue, *etrace;
PyErr_Fetch(&etype, &evalue, &etrace);
PyObject *InvalidDocument = _error("InvalidDocument");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of missing error/NULL checks in this code. I'm not comfortable merging this for 4.9 so it will need to wait for the next release.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blink1073 @ShaneHarvey I took context from code written in the same file, would appreciate if can you point out some existing code from where i can see what all error/Nulls check to put here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants