summaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorGiorgio Previtera <gprevitera@equalexperts.com>2019-07-07 19:15:11 +0100
committerJesse Duffield <jessedduffield@gmail.com>2019-07-14 14:24:18 +1000
commit9fb9962ce7043bc3152bb0bee4a7ac17c35592d6 (patch)
tree01380da3b47b930bb89b7eb9cc4681257d0e3939 /pkg
parent25c93c678d0c198ef1610ec9d8743d8ae1e81a84 (diff)
472 - Don't panic if not in a repository
Display a friendly message and exit with an error if not in a Git repository. Using the same approach used in this PR: https://github.com/jesseduffield/lazydocker/pull/14/files
Diffstat (limited to 'pkg')
-rw-r--r--pkg/app/app.go27
-rw-r--r--pkg/i18n/dutch.go3
-rw-r--r--pkg/i18n/english.go3
-rw-r--r--pkg/i18n/polish.go3
4 files changed, 35 insertions, 1 deletions
diff --git a/pkg/app/app.go b/pkg/app/app.go
index 85caa446d..b364936ae 100644
--- a/pkg/app/app.go
+++ b/pkg/app/app.go
@@ -33,6 +33,11 @@ type App struct {
ClientContext string
}
+type errorMapping struct {
+ originalError string
+ newError string
+}
+
func newProductionLogger(config config.AppConfigurer) *logrus.Logger {
log := logrus.New()
log.Out = ioutil.Discard
@@ -158,7 +163,8 @@ func (app *App) Run() error {
os.Exit(0)
}
- return app.Gui.RunWithSubprocesses()
+ err := app.Gui.RunWithSubprocesses()
+ return err
}
// Rebase contains logic for when we've been run in demon mode, meaning we've
@@ -192,3 +198,22 @@ func (app *App) Close() error {
}
return nil
}
+
+// 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: "fatal: not a git repository (or any of the parent directories): .git",
+ newError: app.Tr.SLocalize("notARepository"),
+ },
+ }
+
+ for _, mapping := range mappings {
+ if strings.Contains(errorMessage, mapping.originalError) {
+ return mapping.newError, true
+ }
+ }
+ return "", false
+}
diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go
index d736e2771..7d93b051c 100644
--- a/pkg/i18n/dutch.go
+++ b/pkg/i18n/dutch.go
@@ -748,6 +748,9 @@ func addDutch(i18nObject *i18n.Bundle) error {
}, &i18n.Message{
ID: "stashOptions",
Other: "Stash options",
+ }, &i18n.Message{
+ ID: "notARepository",
+ Other: "Error: must be run inside a repository",
},
)
}
diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go
index 82ba1b505..50a6e30a6 100644
--- a/pkg/i18n/english.go
+++ b/pkg/i18n/english.go
@@ -771,6 +771,9 @@ func addEnglish(i18nObject *i18n.Bundle) error {
}, &i18n.Message{
ID: "stashOptions",
Other: "Stash options",
+ }, &i18n.Message{
+ ID: "notARepository",
+ Other: "Error: must be run inside a repository",
},
)
}
diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go
index bd5fed9d5..7b32b3eaa 100644
--- a/pkg/i18n/polish.go
+++ b/pkg/i18n/polish.go
@@ -731,6 +731,9 @@ func addPolish(i18nObject *i18n.Bundle) error {
}, &i18n.Message{
ID: "stashOptions",
Other: "Stash options",
+ }, &i18n.Message{
+ ID: "notARepository",
+ Other: "Error: must be run inside a repository",
},
)
}