From 98d4f771c21c6b05d154cfa12830eea32bb103a3 Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Fri, 7 May 2021 01:14:51 +0100 Subject: [PATCH] Report failed sub-process exit statuses then exit gracefully --- map65/mainwindow.cpp | 52 +++++++++++++++++++++++++++++--------------- map65/mainwindow.h | 5 +++-- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/map65/mainwindow.cpp b/map65/mainwindow.cpp index 1ea278965..04edfa0c2 100644 --- a/map65/mainwindow.cpp +++ b/map65/mainwindow.cpp @@ -1,6 +1,7 @@ //------------------------------------------------------------------ MainWindow #include "mainwindow.h" #include "revision_utils.hpp" +#include "widgets/MessageBox.hpp" #include "ui_mainwindow.h" #include "devsetup.h" #include "plotter.h" @@ -101,14 +102,15 @@ MainWindow::MainWindow(QWidget *parent) : SLOT(showStatusMessage(QString))); createStatusBar(); - connect(&proc_m65, SIGNAL(readyReadStandardOutput()), - this, SLOT(readFromStdout())); - - connect(&proc_m65, SIGNAL(error(QProcess::ProcessError)), - this, SLOT(m65_error())); - - connect(&proc_m65, SIGNAL(readyReadStandardError()), - this, SLOT(readFromStderr())); + connect(&proc_m65, SIGNAL(readyReadStandardOutput()), this, SLOT(readFromStdout())); + connect(&proc_m65, &QProcess::errorOccurred, this, &MainWindow::m65_error); + connect(&proc_m65, static_cast (&QProcess::finished), + [this] (int exitCode, QProcess::ExitStatus status) { + if (subProcessFailed (&proc_m65, exitCode, status)) + { + QTimer::singleShot (0, this, SLOT (close ())); + } + }); connect(&proc_editor, SIGNAL(error(QProcess::ProcessError)), this, SLOT(editor_error())); @@ -1341,11 +1343,33 @@ void MainWindow::decode() //decode() decodeBusy(true); } -void MainWindow::m65_error() //m65_error +bool MainWindow::subProcessFailed (QProcess * process, int exit_code, QProcess::ExitStatus status) +{ + if (exit_code || QProcess::NormalExit != status) + { + QStringList arguments; + for (auto argument: process->arguments ()) + { + if (argument.contains (' ')) argument = '"' + argument + '"'; + arguments << argument; + } + MessageBox::critical_message (this, tr ("Subprocess Error") + , tr ("Subprocess failed with exit code %1") + .arg (exit_code) + , tr ("Running: %1\n%2") + .arg (process->program () + ' ' + arguments.join (' ')) + .arg (QString {process->readAllStandardError()})); + return true; + } + return false; +} + +void MainWindow::m65_error (QProcess::ProcessError) { if(!m_killAll) { - msgBox("Error starting or running\n" + m_appDir + "/m65 -s"); - exit(1); + msgBox("Error starting or running\n" + m_appDir + "/m65 -s\n\n" + + proc_m65.errorString ()); + QTimer::singleShot (0, this, SLOT (close ())); } } @@ -1356,12 +1380,6 @@ void MainWindow::editor_error() //editor_error } } -void MainWindow::readFromStderr() //readFromStderr -{ - QByteArray t=proc_m65.readAllStandardError(); - msgBox(t); -} - void MainWindow::readFromStdout() //readFromStdout { while(proc_m65.canReadLine()) diff --git a/map65/mainwindow.h b/map65/mainwindow.h index 862623f45..a93a9431a 100644 --- a/map65/mainwindow.h +++ b/map65/mainwindow.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "getfile.h" #include "soundin.h" #include "soundout.h" @@ -39,8 +40,7 @@ public slots: void diskWriteFinished(); void freezeDecode(int n); void readFromStdout(); - void readFromStderr(); - void m65_error(); + void m65_error (QProcess::ProcessError); void editor_error(); void guiUpdate(); void doubleClickOnCall(QString hiscall, bool ctrl); @@ -277,6 +277,7 @@ private: void ba2msg(QByteArray ba, char* message); void msgtype(QString t, QLineEdit* tx); void stub(); + bool subProcessFailed (QProcess *, int exit_code, QProcess::ExitStatus); }; extern void getfile(QString fname, bool xpol, int idInt);