Merge pull request #14 from jesseduffield/12_unix-permission-error

don't panic on unix permission failure
This commit is contained in:
Jesse Duffield 2019-06-30 20:56:10 +10:00 committed by GitHub
commit da03778cf9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 1 deletions

View file

@ -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)

View file

@ -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
}

View file

@ -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",