@@ -1645,11 +1645,11 @@ static int write_raw_doc(buffer_t buffer, PyObject* raw, PyObject* _raw_str) {
16451645}
16461646
16471647
1648- /* Update Invalid Document error message to include doc.
1648+ /* Update Invalid Document error to include doc as a property .
16491649 */
16501650void handle_invalid_doc_error (PyObject * dict ) {
16511651 PyObject * etype = NULL , * evalue = NULL , * etrace = NULL ;
1652- PyObject * msg = NULL , * dict_str = NULL , * new_msg = NULL ;
1652+ PyObject * msg = NULL , * new_msg = NULL , * new_evalue = NULL ;
16531653 PyErr_Fetch (& etype , & evalue , & etrace );
16541654 PyObject * InvalidDocument = _error ("InvalidDocument" );
16551655 if (InvalidDocument == NULL ) {
@@ -1659,26 +1659,22 @@ void handle_invalid_doc_error(PyObject* dict) {
16591659 if (evalue && PyErr_GivenExceptionMatches (etype , InvalidDocument )) {
16601660 PyObject * msg = PyObject_Str (evalue );
16611661 if (msg ) {
1662- // Prepend doc to the existing message
1663- PyObject * dict_str = PyObject_Str (dict );
1664- if (dict_str == NULL ) {
1665- goto cleanup ;
1666- }
1667- const char * dict_str_utf8 = PyUnicode_AsUTF8 (dict_str );
1668- if (dict_str_utf8 == NULL ) {
1669- goto cleanup ;
1670- }
16711662 const char * msg_utf8 = PyUnicode_AsUTF8 (msg );
16721663 if (msg_utf8 == NULL ) {
16731664 goto cleanup ;
16741665 }
1675- PyObject * new_msg = PyUnicode_FromFormat ("Invalid document %s | %s" , dict_str_utf8 , msg_utf8 );
1666+ PyObject * new_msg = PyUnicode_FromFormat ("Invalid document: %s" , msg_utf8 );
1667+ if (new_msg == NULL ) {
1668+ goto cleanup ;
1669+ }
1670+ // Add doc to the error instance as a property.
1671+ PyObject * new_evalue = PyObject_CallFunctionObjArgs (InvalidDocument , new_msg , dict , NULL );
16761672 Py_DECREF (evalue );
16771673 Py_DECREF (etype );
16781674 etype = InvalidDocument ;
16791675 InvalidDocument = NULL ;
1680- if (new_msg ) {
1681- evalue = new_msg ;
1676+ if (new_evalue ) {
1677+ evalue = new_evalue ;
16821678 } else {
16831679 evalue = msg ;
16841680 }
@@ -1689,7 +1685,7 @@ void handle_invalid_doc_error(PyObject* dict) {
16891685 PyErr_Restore (etype , evalue , etrace );
16901686 Py_XDECREF (msg );
16911687 Py_XDECREF (InvalidDocument );
1692- Py_XDECREF (dict_str );
1688+ Py_XDECREF (new_evalue );
16931689 Py_XDECREF (new_msg );
16941690}
16951691
0 commit comments