From db99329d9c977788fca7ded91ec609ce572b6cf4 Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Wed, 19 Apr 2023 09:19:27 +0200 Subject: [PATCH] selinux: Fix error reporting Exception objects are not directly printable, so trying to render it in an Alert or with `console.warn()` just results in an empty string. That makes debugging really hard, as there is no corresponding entry in the journal either. Use `.toString()` on the exception to render them properly. Also show the actual exception text in the Alert in the UI. Still keep the console.warn()s, so that we can do naughty matching on them. While at it, eliminate the unnecessary Promise wrappers, and use chaining. --- pkg/selinux/setroubleshoot-client.js | 97 ++++++++++++---------------- 1 file changed, 42 insertions(+), 55 deletions(-) diff --git a/pkg/selinux/setroubleshoot-client.js b/pkg/selinux/setroubleshoot-client.js index dfdb92f8ba7c..5d221f85f422 100644 --- a/pkg/selinux/setroubleshoot-client.js +++ b/pkg/selinux/setroubleshoot-client.js @@ -107,69 +107,56 @@ client.init = function(capabilitiesChangedCallback) { lastSeen: when the alert was seen for the last time, timestamp in ms level: "green", "yellow" or "red" */ - client.getAlert = function(localId) { - return new Promise((resolve, reject) => { - client.proxy.call("get_alert", [localId]) - .then(result => { - const details = { - localId: result[0], - summary: result[1], - reportCount: result[2], - auditEvent: result[3], - pluginAnalysis: result[4], - firstSeen: result[5] / 1000, - lastSeen: result[6] / 1000, - level: result[7], - }; - // cleanup analysis - details.pluginAnalysis = details.pluginAnalysis.map(itm => ({ - ifText: itm[0], - thenText: itm[1], - doText: itm[2], - analysisId: itm[3], - fixable: itm[4], - reportBug: itm[5], - })); - resolve(details); - }) - .catch(ex => { - console.warn("Unable to get alert for id " + localId); - console.warn(ex); - reject(new Error(cockpit.format(_("Unable to get alert: $0"), localId))); - }); - }); - }; + client.getAlert = localId => client.proxy.call("get_alert", [localId]) + .then(result => { + const details = { + localId: result[0], + summary: result[1], + reportCount: result[2], + auditEvent: result[3], + pluginAnalysis: result[4], + firstSeen: result[5] / 1000, + lastSeen: result[6] / 1000, + level: result[7], + }; + // cleanup analysis + details.pluginAnalysis = details.pluginAnalysis.map(itm => ({ + ifText: itm[0], + thenText: itm[1], + doText: itm[2], + analysisId: itm[3], + fixable: itm[4], + reportBug: itm[5], + })); + return details; + }) + .catch(ex => { + console.warn("Unable to get alert for id", localId, ":", JSON.stringify(ex)); + return Promise.reject(new Error(`Unable to get alert for id ${localId}: ${ex.toString()}`)); + }); /* Run a fix via SetroubleshootFixit The analysisId is given as part of pluginAnalysis entries in alert details */ - client.runFix = function(alertId, analysisId) { - return new Promise((resolve, reject) => { - client.proxyFixit.call("run_fix", [alertId, analysisId]) - .then(result => resolve(result[0])) - .catch(ex => reject(new Error(cockpit.format(_("Unable to run fix: $0"), ex)))); - }); - }; + client.runFix = (alertId, analysisId) => client.proxyFixit.call("run_fix", [alertId, analysisId]) + .then(result => result[0]) + .catch(ex => { + console.warn("Unable to run fix:", JSON.stringify(ex)); + return Promise.reject(new Error(cockpit.format(_("Unable to run fix: $0"), ex.toString()))); + }); /* Delete an alert from the database (will be removed for all users), returns true on success * Only assign this to the client variable if the dbus interface actually supports the operation */ - const deleteAlert = function(localId) { - return new Promise((resolve, reject) => { - client.proxy.call("delete_alert", [localId]) - .then(success => { - if (success) - resolve(); - else - reject(new Error(cockpit.format(_("Failed to delete alert: $0"), localId))); - }) - .catch(ex => { - console.warn("Unable to delete alert with id " + localId); - console.warn(ex); - reject(new Error(cockpit.format(_("Error while deleting alert: $0"), localId))); - }); - }); - }; + const deleteAlert = localId => client.proxy.call("delete_alert", [localId]) + .then(success => { + if (!success) + return Promise.reject(new Error(cockpit.format(_("Failed to delete alert: $0"), localId))); + }) + .catch(ex => { + console.warn("Unable to delete alert with id", localId, ":", JSON.stringify(ex)); + return Promise.reject(new Error(cockpit.format(_("Failed to delete alert: $0"), ex.toString()))); + }); // earlier versions of the dbus interface don't support alert deletion/dismissal // HACK https://bugzilla.redhat.com/show_bug.cgi?id=1306700