File: errors.go

package info (click to toggle)
lazygit 0.57.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,748 kB
  • sloc: sh: 153; makefile: 76
file content (43 lines) | stat: -rw-r--r-- 1,045 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package app

import (
	"strings"

	"github.com/jesseduffield/lazygit/pkg/i18n"
	"github.com/samber/lo"
)

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 knownError(tr *i18n.TranslationSet, err error) (string, bool) {
	errorMessage := err.Error()

	knownErrorMessages := []string{minGitVersionErrorMessage(tr)}

	if lo.Contains(knownErrorMessages, errorMessage) {
		return errorMessage, true
	}

	mappings := []errorMapping{
		{
			originalError: "fatal: not a git repository",
			newError:      tr.NotARepository,
		},
		{
			originalError: "getwd: no such file or directory",
			newError:      tr.WorkingDirectoryDoesNotExist,
		},
	}

	if mapping, ok := lo.Find(mappings, func(mapping errorMapping) bool {
		return strings.Contains(errorMessage, mapping.originalError)
	}); ok {
		return mapping.newError, true
	}

	return "", false
}