diff --git a/main.go b/main.go index ad02e85f..7dbadb95 100644 --- a/main.go +++ b/main.go @@ -55,6 +55,11 @@ func main() { } if err != nil { + if errMessage, known := app.KnownError(err); known { + log.Println(errMessage) + os.Exit(0) + } + if client.IsErrConnectionFailed(err) { log.Println(app.Tr.ConnectionFailed) os.Exit(0) diff --git a/pkg/app/app.go b/pkg/app/app.go index 96d9503b..57015918 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -2,6 +2,7 @@ package app import ( "io" + "strings" "github.com/jesseduffield/lazydocker/pkg/commands" "github.com/jesseduffield/lazydocker/pkg/config" @@ -50,7 +51,9 @@ func NewApp(config *config.AppConfig) (*App, error) { } func (app *App) Run() error { - return app.Gui.RunWithSubprocesses() + err := app.Gui.RunWithSubprocesses() + + return err } // Close closes any resources @@ -63,3 +66,28 @@ func (app *App) Close() error { } return nil } + +type errorMapping struct { + originalError string + newError string +} + +// KnownError takes an error and tells us whether it's an error that we know about where we can print a nicely formatted version of it rather than panicking with a stack trace +func (app *App) KnownError(err error) (string, bool) { + errorMessage := err.Error() + + mappings := []errorMapping{ + { + originalError: "Got permission denied while trying to connect to the Docker daemon socket", + newError: app.Tr.CannotAccessDockerSocketError, + }, + } + + for _, mapping := range mappings { + if strings.Contains(errorMessage, mapping.originalError) { + return mapping.newError, true + } + } + + return "", false +} diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 130f3674..5117c791 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -25,6 +25,7 @@ type TranslationSet struct { ConnectionFailed string UnattachableContainerError string CannotAttachStoppedContainerError string + CannotAccessDockerSocketError string Donate string Cancel string @@ -98,6 +99,7 @@ func englishSet() TranslationSet { ConnectionFailed: "connection to docker client failed. You may need to restart the docker client", UnattachableContainerError: "Container does not support attaching. You must either run the service with the '-it' flag or use `stdin_open: true, tty: true` in the docker-compose.yml file", CannotAttachStoppedContainerError: "You cannot attach to a stopped container, you need to start it first (which you can actually do with the 'r' key) (yes I'm too lazy to do this automatically for you) (pretty cool that I get to communicate one-on-one with you in the form of an error message though)", + CannotAccessDockerSocketError: "Can't access docker socket at: unix:///var/run/docker.sock\nRun lazydocker as root or read https://docs.docker.com/install/linux/linux-postinstall/", Donate: "Donate", Confirm: "Confirm",