libct/cg/sd: add isDbusError
Generalize isUnitExists as isDbusError, and use errors.As while at it
(which can handle wrapped errors as well).
@@ -302,16 +302,22 @@ func getUnitName(c *configs.Cgroup) string {
return c.Name
}
-// isUnitExists returns true if the error is that a systemd unit already exists.
-func isUnitExists(err error) bool {
+// isDbusError returns true if the error is a specific dbus error.
+func isDbusError(err error, name string) bool {
if err != nil {
- if dbusError, ok := err.(dbus.Error); ok {
- return strings.Contains(dbusError.Name, "org.freedesktop.systemd1.UnitExists")
+ var derr *dbus.Error
+ if errors.As(err, &derr) {
+ return strings.Contains(derr.Name, name)
}
}
return false
}
+// isUnitExists returns true if the error is that a systemd unit already exists.
+func isUnitExists(err error) bool {
+ return isDbusError(err, "org.freedesktop.systemd1.UnitExists")
+}
+
func startUnit(dbusConnection *systemdDbus.Conn, unitName string, properties []systemdDbus.Property) error {
statusChan := make(chan string, 1)
if _, err := dbusConnection.StartTransientUnit(unitName, "replace", properties, statusChan); err == nil {
