Skip to content
Permalink
Browse files

don't panick on unix connection failure

  • Loading branch information...
jesseduffield committed Jun 30, 2019
1 parent 51d9b14 commit ba426f05be2be7357f14356f71c9af28bafa6f1c
Showing with 36 additions and 1 deletion.
  1. +5 −0 main.go
  2. +29 −1 pkg/app/app.go
  3. +2 −0 pkg/i18n/english.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)
@@ -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
}
@@ -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",

0 comments on commit ba426f0

Please sign in to comment.
You can’t perform that action at this time.