Skip to content

Commit

Permalink
Clean up errors and messages
Browse files Browse the repository at this point in the history
  • Loading branch information
taigrr committed May 16, 2021
1 parent 7325dda commit 3fcd88d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
45 changes: 25 additions & 20 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@ import (
)

var (
// ErrSystemctlNotInstalled means that upon trying to manually locate systemctl in the user's path,
// it was not found. If this error occurs, the library isn't entirely useful.
ErrNotInstalled = errors.New("systemd binary was not found")

// ErrExecTimeout means that the provided context was done before the command finished execution.
// $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR were not defined
// This usually is the result of running in usermode as root
ErrBusFailure = errors.New("bus connection failure")
// The unit specified doesn't exist or can't be found
ErrDoesNotExist = errors.New("unit does not exist")
// The provided context was cancelled before the command finished execution
ErrExecTimeout = errors.New("command timed out")
// The executable was invoked without enough permissions to run the selected command
// Running as superuser or adding the correct PolicyKit definitions can fix this
// See https://wiki.debian.org/PolicyKit for more information
ErrInsufficientPermissions = errors.New("insufficient permissions")
// Masked units can only be unmasked, but something else was attempted
// Unmask the unit before enabling or disabling it
ErrMasked = errors.New("unit masked")
// If this error occurs, the library isn't entirely useful, as it causes a panic
// Make sure systemctl is in the PATH before calling again
ErrNotInstalled = errors.New("systemctl not in $PATH")
// A unit was expected to be running but was found inactive
// This can happen when calling GetStartTime on a dead unit, for example
ErrUnitNotActive = errors.New("unit not active")
// An expected value is unavailable, but the unit may be running
// This can happen when calling GetMemoryUsage on systemd itself, for example
ErrValueNotSet = errors.New("value not set")

// ErrInsufficientPermissions means the calling executable was invoked without enough permissions to run the selected command
ErrInsufficientPermissions = errors.New("insufficient permissions for action")

// ErrDoesNotExist means the unit specified doesn't exist or can't be found
ErrDoesNotExist = errors.New("Unit does not exist")
// ErrUnspecified means something in the stderr output contains the word `Failed`, but not a known case
ErrUnspecified = errors.New("Unknown error")
// ErrUnitNotRunning means a function which requires a unit to be run (such as GetStartTime) was run against an inactive unit
ErrUnitNotRunning = errors.New("Unit isn't running")
// ErrBusFailure means $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR were not defined
ErrBusFailure = errors.New("Failure to connect to bus, did you run in usermode as root?")
// ErrValueNotSet means an expected value is unavailable, but the unit may be running
ErrValueNotSet = errors.New("Value not set or unavailable")
// ErrMasked means a unit cannot be enabled because it is masked
ErrMasked = errors.New("Unit is masked")
// Something in the stderr output contains the word `Failed`, but it is not a known case
// This is a catch-all, and if it's ever seen in the wild, please submit a PR
ErrUnspecified = errors.New("Unknown error, please submit an issue at github.com/taigrr/systemctl")
)
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func GetStartTime(ctx context.Context, unit string, opts Options) (time.Time, er
}
// ExecMainStartTimestamp returns an empty string if the unit is not running
if value == "" {
return time.Unix(0, 0), ErrUnitNotRunning
return time.Unix(0, 0), ErrUnitNotActive
}
return time.Parse(dateFormat, value)
}
Expand Down
6 changes: 3 additions & 3 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ func TestGetStartTime(t *testing.T) {
}{
// Run these tests only as a user
//try nonexistant unit in user mode as user
{"nonexistant", ErrUnitNotRunning, Options{UserMode: false}, true},
{"nonexistant", ErrUnitNotActive, Options{UserMode: false}, true},
// try existing unit in user mode as user
{"syncthing", ErrUnitNotRunning, Options{UserMode: true}, true},
{"syncthing", ErrUnitNotActive, Options{UserMode: true}, true},
// try existing unit in system mode as user
{"nginx", nil, Options{UserMode: false}, true},

// Run these tests only as a superuser

// try nonexistant unit in system mode as system
{"nonexistant", ErrUnitNotRunning, Options{UserMode: false}, false},
{"nonexistant", ErrUnitNotActive, Options{UserMode: false}, false},
// try existing unit in system mode as system
{"nginx", ErrBusFailure, Options{UserMode: true}, false},
// try existing unit in system mode as system
Expand Down

0 comments on commit 3fcd88d

Please sign in to comment.