Skip to content

Commit 98d9a3a

Browse files
committed
Create custom alert window to show Python exceptions with error message
1 parent 53f6568 commit 98d9a3a

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

Source/PythonProcessor.cpp

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ void PythonProcessor::initialize(bool signalChainIsLoading)
107107
if(!signalChainIsLoading
108108
&& Py_IsInitialized() == 0)
109109
{
110-
if( !initInterpreter() )
111-
LOGE("Unable to initialize python interpreter!!");
110+
initInterpreter();
112111
}
113112
}
114113

@@ -194,7 +193,7 @@ void PythonProcessor::handleTTLEvent(TTLEventPtr event)
194193
pyObject->attr("handle_ttl_event")(sourceNodeId, channelName.toRawUTF8(), sampleNumber, line, state);
195194
}
196195
catch (py::error_already_set& e) {
197-
handlePythonException(e);
196+
handlePythonException("Python Exception!", "Error when handling ttl event in Python:", e);
198197
}
199198
}
200199
}
@@ -230,7 +229,7 @@ void PythonProcessor::handleSpike(SpikePtr spike)
230229
(sourceNodeId, electrodeName.toRawUTF8(), numChans, numSamples, sampleNum, sortedId, spikeData);
231230
}
232231
catch (py::error_already_set& e) {
233-
handlePythonException(e);
232+
handlePythonException("Python Exception!", "Error when handling spike in Python:", e);
234233
}
235234
}
236235
}
@@ -277,7 +276,7 @@ bool PythonProcessor::startAcquisition()
277276
pyObject->attr("start_acquisition")();
278277
}
279278
catch (py::error_already_set& e) {
280-
handlePythonException(e);
279+
handlePythonException("Python Exception!", "Error when starting acquisition in Python:", e);
281280
}
282281
}
283282
return true;
@@ -296,7 +295,7 @@ bool PythonProcessor::stopAcquisition()
296295
pyObject->attr("stop_acquisition")();
297296
}
298297
catch (py::error_already_set& e) {
299-
handlePythonException(e);
298+
handlePythonException("Python Exception!", "Error when stopping acquisition in Python:", e);
300299
}
301300
}
302301
}
@@ -314,7 +313,7 @@ void PythonProcessor::startRecording()
314313
pyObject->attr("start_recording")(recordingDirectory.toRawUTF8());
315314
}
316315
catch (py::error_already_set& e) {
317-
handlePythonException(e);
316+
handlePythonException("Python Exception!", "Error when starting recording in Python:", e);
318317
}
319318
}
320319
}
@@ -330,7 +329,7 @@ void PythonProcessor::stopRecording()
330329
pyObject->attr("stop_recording")();
331330
}
332331
catch (py::error_already_set& e) {
333-
handlePythonException(e);
332+
handlePythonException("Python Exception!", "Error when stopping recording in Python:", e);
334333
}
335334
}
336335
}
@@ -447,8 +446,6 @@ bool PythonProcessor::initInterpreter(String pythonHome)
447446

448447
if(Py_IsInitialized() > 0)
449448
{
450-
LOGC("Python Interpreter initialized successfully! Python Home: ", String(Py_GetPythonHome()));
451-
452449
#if JUCE_WINDOWS
453450
py::module_ os = py::module_::import("os");
454451
os.attr("add_dll_directory")
@@ -464,13 +461,14 @@ bool PythonProcessor::initInterpreter(String pythonHome)
464461
LOGD(p.cast<std::string>());
465462
}
466463
}
467-
464+
LOGC("Python Interpreter initialized successfully! Python Home: ", String(Py_GetPythonHome()));
468465
return true;
469466
}
470-
catch(std::exception& e)
467+
catch(py::error_already_set& e)
471468
{
472-
PyErr_Print();
473-
LOGE(e.what());
469+
String errText = "Unable to initialize Python Interpreter!";
470+
LOGE(errText);
471+
handlePythonException(errText, "", e);
474472
return false;
475473
}
476474
}
@@ -515,13 +513,12 @@ bool PythonProcessor::importModule()
515513
return true;
516514
}
517515

518-
catch (std::exception& exc)
516+
catch (py::error_already_set& e)
519517
{
520-
LOGC("Failed to import Python module.");
521-
LOGC(exc.what());
518+
String errText = "Failed to import Python module " + moduleName;
519+
LOGE(errText);
520+
handlePythonException("Import Failed!", errText, e);
522521

523-
editorPtr->setPathLabelText("No Module Loaded");
524-
moduleReady = false;
525522
return false;
526523
}
527524
}
@@ -538,7 +535,7 @@ void PythonProcessor::reload()
538535
pyModule->reload();
539536
}
540537
catch (py::error_already_set& e) {
541-
handlePythonException(e);
538+
handlePythonException("Reloding failed!", "", e);
542539
return;
543540
}
544541

@@ -582,20 +579,41 @@ void PythonProcessor::initModule()
582579
pyObject = new py::object(pyModule->attr("PyProcessor")(this, numChans, sampleRate));
583580
}
584581

585-
catch (std::exception& exc)
582+
catch (py::error_already_set& e)
586583
{
587-
LOGC("Failed to initialize Python module.");
588-
LOGC(exc.what());
589-
590-
editorPtr->setPathLabelText("(ERROR) " + moduleName);
591-
moduleReady = false;
584+
String errText = "Failed to initialize Python module " + moduleName;
585+
LOGE(errText);
586+
handlePythonException("Python Exception!", errText, e);
592587
}
593588
}
594589
}
595590

596-
void PythonProcessor::handlePythonException(py::error_already_set e)
591+
void PythonProcessor::handlePythonException(const String& title, const String& msg, py::error_already_set e)
597592
{
598-
LOGE("Python Exception:", e.what());
593+
LOGE("Python Exception:\n", e.what());
594+
595+
TextEditor* customMsgBox = new TextEditor();
596+
customMsgBox->setReadOnly(true);
597+
customMsgBox->setMultiLine(true);
598+
customMsgBox->setFont(Font("Fira Code", "Regular", 14.0f));
599+
customMsgBox->setSize(400, 300);
600+
customMsgBox->setText(String(e.what()));
601+
602+
int textHeight = customMsgBox->getTextHeight();
603+
604+
if(textHeight < 300)
605+
customMsgBox->setSize(400, textHeight + 10);
606+
607+
AlertWindow exceptionWindow(title,
608+
msg,
609+
AlertWindow::WarningIcon);
610+
611+
KeyPress dismissKey(KeyPress::returnKey, 0, 0);
612+
exceptionWindow.addButton("OK", 1, dismissKey);
613+
exceptionWindow.addCustomComponent(customMsgBox);
614+
615+
exceptionWindow.runModalLoop();
616+
599617
moduleReady = false;
600618
editorPtr->setPathLabelText("(ERROR) " + moduleName);
601619
}

Source/PythonProcessor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class PythonProcessor : public GenericProcessor
148148
void initModule();
149149

150150
/** Deals with python exceptions (print and turn off module for now) */
151-
void handlePythonException(py::error_already_set e);
151+
void handlePythonException(const String& title, const String& msg, py::error_already_set e);
152152

153153
};
154154

0 commit comments

Comments
 (0)