Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit 43447da

Browse files
committed
Merge pull request #47 from ybznek/v1-1-error-2
Show LibXML errors in Diagnostics Dialog
2 parents d36af82 + 5d2e6cb commit 43447da

File tree

6 files changed

+56
-17
lines changed

6 files changed

+56
-17
lines changed

include/APIHelpers.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,11 @@ QString oscapItemGetReadableDescription(struct xccdf_item* item, struct xccdf_po
6666
*/
6767
QString oscapErrDesc();
6868

69+
/**
70+
* Returns QString containing utf8 contents of oscap_err_get_full_error()
71+
*
72+
* @exception nothrow This function is guaranteed to not throw any exceptions.
73+
*/
74+
QString oscapErrGetFullError();
75+
6976
#endif

include/DiagnosticsDialog.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ enum MessageSeverity
5050
MS_ERROR
5151
};
5252

53+
/**
54+
* @brief MessageFormat can be any subset of this flags
55+
*/
56+
enum MessageFormat
57+
{
58+
MF_STANDARD = 0x01,
59+
MF_PREFORMATTED = 0x02, // Preserve whitespaces to output
60+
MF_XML = 0x04, // Replace xml metacharacters with xml entities
61+
MF_PREFORMATTED_XML = MF_PREFORMATTED | MF_XML,
62+
};
63+
5364
/**
5465
* @brief Workbench displays errors and warnings, this dialog groups them
5566
*
@@ -85,31 +96,31 @@ class DiagnosticsDialog : public QDialog
8596
* The diagnostics dialog will not open when just these messages are
8697
* received.
8798
*/
88-
void infoMessage(const QString& message);
99+
void infoMessage(const QString& message, MessageFormat format = MF_STANDARD);
89100

90101
/**
91102
* @brief Scanner triggers this to show a warning message
92103
*
93104
* A warning message will open the diagnostics dialog if it isn't
94105
* being shown already.
95106
*/
96-
void warningMessage(const QString& message);
107+
void warningMessage(const QString& message, MessageFormat format = MF_STANDARD);
97108

98109
/**
99110
* @brief Scanner triggers this to show an error message
100111
*
101112
* An error message will open the diagnostics dialog if it isn't
102113
* being shown already.
103114
*/
104-
void errorMessage(const QString& message);
115+
void errorMessage(const QString& message, MessageFormat format = MF_STANDARD);
105116

106117
/**
107118
* @brief Report a caught exception.
108119
*/
109-
void exceptionMessage(const std::exception& e, const QString& context = "");
120+
void exceptionMessage(const std::exception& e, const QString& context = "", MessageFormat format = MF_STANDARD);
110121

111122
private:
112-
void pushMessage(MessageSeverity severity, const QString& fullMessage);
123+
void pushMessage(MessageSeverity severity, const QString& fullMessage, MessageFormat format = MF_STANDARD);
113124

114125
/**
115126
* @brief Pushes a single info message containing version info

src/APIHelpers.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,11 @@ QString oscapErrDesc()
7070
{
7171
return QString::fromUtf8(oscap_err_desc());
7272
}
73+
74+
QString oscapErrGetFullError()
75+
{
76+
char* fullErrorCstr = oscap_err_get_full_error();
77+
QString fullError = QString::fromUtf8(fullErrorCstr);
78+
free(fullErrorCstr);
79+
return fullError;
80+
}

src/DiagnosticsDialog.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,36 +63,37 @@ void DiagnosticsDialog::waitUntilHidden(unsigned int interval)
6363
}
6464
}
6565

66-
void DiagnosticsDialog::infoMessage(const QString& message)
66+
void DiagnosticsDialog::infoMessage(const QString& message, MessageFormat format)
6767
{
68-
pushMessage(MS_INFO, message);
68+
pushMessage(MS_INFO, message, format);
6969
}
7070

71-
void DiagnosticsDialog::warningMessage(const QString& message)
71+
void DiagnosticsDialog::warningMessage(const QString& message, MessageFormat format)
7272
{
73-
pushMessage(MS_WARNING, message);
73+
pushMessage(MS_WARNING, message, format);
7474

7575
// warning message is important, make sure the diagnostics are shown
7676
show();
7777
}
7878

79-
void DiagnosticsDialog::errorMessage(const QString& message)
79+
void DiagnosticsDialog::errorMessage(const QString& message, MessageFormat format)
8080
{
81-
pushMessage(MS_ERROR, message);
81+
pushMessage(MS_ERROR, message, format);
8282

8383
// error message is important, make sure the diagnostics are shown
8484
show();
8585
}
8686

87-
void DiagnosticsDialog::exceptionMessage(const std::exception& e, const QString& context)
87+
void DiagnosticsDialog::exceptionMessage(const std::exception& e, const QString& context, MessageFormat format)
8888
{
89-
pushMessage(MS_EXCEPTION, (context.isEmpty() ? "" : context + "\n\n" + QString::fromUtf8(e.what())));
89+
pushMessage(MS_EXCEPTION, (context.isEmpty() ? "" : context + "\n\n" + QString::fromUtf8(e.what())), format);
9090

9191
// error message is important, make sure the diagnostics are shown
9292
show();
9393
}
9494

95-
void DiagnosticsDialog::pushMessage(MessageSeverity severity, const QString& fullMessage)
95+
96+
void DiagnosticsDialog::pushMessage(MessageSeverity severity, const QString& fullMessage, MessageFormat format)
9697
{
9798
char stime[11];
9899
stime[10] = '\0';
@@ -132,9 +133,21 @@ void DiagnosticsDialog::pushMessage(MessageSeverity severity, const QString& ful
132133
strSeverity = strSeverity.leftJustified(8);
133134

134135
std::cerr << stime << " | " << strSeverity.toUtf8().constData() << " | " << fullMessage.toUtf8().constData() << std::endl;
136+
137+
QString outputMessage = fullMessage;
138+
if (format & MF_XML)
139+
{
140+
outputMessage = Qt::escape(outputMessage);
141+
}
142+
143+
if (format & MF_PREFORMATTED)
144+
{
145+
outputMessage = QString("<pre>%1</pre>").arg(outputMessage);
146+
}
147+
135148
mUI.messages->append(
136149
QString("<table><tr><td style=\"padding:5px 0px 0px 5px\"><pre>%1 </pre></td><td style=\"background: %2; padding:5px\"><pre>%3 </pre></td><td>%4</td></tr></table>\n")
137-
.arg(stime, bgCol, strSeverity, fullMessage)
150+
.arg(stime, bgCol, strSeverity, outputMessage)
138151
);
139152
}
140153

src/MainWindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ void MainWindow::openFile(const QString& path)
337337
setWindowTitle(QObject::tr("SCAP Workbench"));
338338
mUI.tailoringFileComboBox->clear();
339339

340-
mDiagnosticsDialog->exceptionMessage(e, QObject::tr("Error while opening file."));
340+
mDiagnosticsDialog->exceptionMessage(e, QObject::tr("Error while opening file."), MF_PREFORMATTED_XML);
341341
}
342342
}
343343

src/ScanningSession.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ void ScanningSession::reloadSession(bool forceReload) const
615615
{
616616
if (xccdf_session_load(mSession) != 0)
617617
throw ScanningSessionException(
618-
QString("Failed to reload session. OpenSCAP error message:\n%1").arg(oscapErrDesc()));
618+
QString("Failed to reload session. OpenSCAP error message:\n%1").arg(oscapErrGetFullError()));
619619

620620
struct xccdf_policy_model* policyModel = xccdf_session_get_policy_model(mSession);
621621

0 commit comments

Comments
 (0)