Skip to content

Commit c2ce430

Browse files
committedJul 15, 2019
[processing] Don't append full traceback when a QgsProcessingException is
raised by a Python algorithm This is too noisy for these expected exceptions -- instead, only show the traceback for other exceptions (Which are likely a result of Python coding errors, so they are useful for debugging)
1 parent e45b62c commit c2ce430

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed
 

‎python/core/core.sip.in

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,40 @@ done:
107107
%Include core_auto.sip
108108

109109
%VirtualErrorHandler processing_exception_handler
110-
QString trace = getTraceback();
111-
QgsLogger::critical( trace );
110+
// if an explicit QgsProcessingException was raised, we don't retrieve
111+
// and append the trace. It's too noisy for these "expected" type errors.
112+
// For all other exceptions, it's likely a coding error in the algorithm,
113+
// so we DO retrieve the full traceback for debugging.
114+
PyGILState_STATE gstate;
115+
gstate = PyGILState_Ensure();
116+
PyTypeObject* err = reinterpret_cast< PyTypeObject* >( PyErr_Occurred() );
117+
const bool isProcessingException = err && QString( err->tp_name ) == QStringLiteral( "QgsProcessingException" );
118+
119+
QString what;
120+
if ( isProcessingException )
121+
{
122+
PyObject *type, *value, *traceback;
123+
PyErr_Fetch( &type, &value, &traceback );
124+
// check whether the object is already a unicode string
125+
if ( PyUnicode_Check( value) )
126+
{
127+
what = QString::fromUtf8( PyUnicode_AsUTF8( value ) );
128+
}
129+
else
130+
{
131+
PyObject* str = PyObject_Str( value );
132+
what = QString::fromUtf8( PyUnicode_AsUTF8( str ) );
133+
Py_XDECREF( str );
134+
}
135+
PyGILState_Release( gstate );
136+
}
137+
else
138+
{
139+
PyGILState_Release( gstate );
140+
QString trace = getTraceback();
141+
QgsLogger::critical( trace );
142+
what = trace;
143+
}
112144
SIP_RELEASE_GIL( sipGILState );
113-
throw QgsProcessingException( trace );
145+
throw QgsProcessingException( what );
114146
%End

0 commit comments

Comments
 (0)
Please sign in to comment.