Debian Codegraph @

internal/errors/errors.go

raw symbols SCIP
1 package errors
2
3 import (
4 "strings"
5 )
6
7 // GitError is the errors from git package
8 type GitError string
9
10 const (
11 // ErrGitCommand is thrown when git command returned an error code
12 ErrGitCommand GitError = "git command returned error code"
13 // ErrAuthenticationRequired is thrown when an authentication required on
14 // a remote operation
15 ErrAuthenticationRequired GitError = "authentication required"
16 // ErrAuthorizationFailed is thrown when authorization failed while trying
17 // to authenticate with remote
18 ErrAuthorizationFailed GitError = "authorization failed"
19 // ErrInvalidAuthMethod is thrown when invalid auth method is invoked
20 ErrInvalidAuthMethod GitError = "invalid auth method"
21 // ErrAlreadyUpToDate is thrown when a repository is already up to date
22 // with its src on merge/fetch/pull
23 ErrAlreadyUpToDate GitError = "already up to date"
24 // ErrCouldNotFindRemoteRef is thrown when trying to fetch/pull cannot
25 // find suitable remote reference
26 ErrCouldNotFindRemoteRef GitError = "could not find remote ref"
27 // ErrMergeAbortedTryCommit indicates that the repositort is not clean and
28 // some changes may conflict with the merge
29 ErrMergeAbortedTryCommit GitError = "stash/commit changes. aborted"
30 // ErrRemoteBranchNotSpecified means that default remote branch is not set
31 // for the current branch. can be setted with "git config --local --add
32 // branch.<your branch name>.remote=<your remote name> "
33 ErrRemoteBranchNotSpecified GitError = ("upstream not set")
34 // ErrRemoteNotFound is thrown when the remote is not reachable. It may be
35 // caused by the deletion of the remote or connectivity problems
36 ErrRemoteNotFound GitError = ("remote not found")
37 // ErrConflictAfterMerge is thrown when a conflict occurs at merging two
38 // references
39 ErrConflictAfterMerge GitError = ("conflict while merging")
40 // ErrUnmergedFiles possibly occurs after a conflict
41 ErrUnmergedFiles GitError = ("unmerged files detected")
42 // ErrReferenceBroken thrown when unable to resolve reference
43 ErrReferenceBroken GitError = ("unable to resolve reference")
44 // ErrPermissionDenied is thrown when ssh authentication occurs
45 ErrPermissionDenied GitError = ("permission denied")
46 // ErrOverwrittenByMerge is the thrown when there is un-tracked files on working tree
47 ErrOverwrittenByMerge GitError = ("move or remove un-tracked files before merge")
48 // ErrUserEmailNotSet is thrown if there is no configured user email while
49 // commit command
50 ErrUserEmailNotSet GitError = ("user email not configured")
51 // ErrUnclassified is unconsidered error type
52 ErrUnclassified GitError = ("unclassified error")
53 // NoErrIterationHalted is thrown for catching stops in interators
54 NoErrIterationHalted GitError = ("iteration halted")
55 )
56
57 func (e GitError) Error() string {
58 return string(e)
59 }
60
61 // ParseGitError takes git output as an input and tries to find some meaningful
62 // errors can be used by the app
63 func ParseGitError(out string, err error) error {
64 if strings.Contains(out, "error: Your local changes to the following files would be overwritten by merge") {
65 return ErrMergeAbortedTryCommit
66 } else if strings.Contains(out, "ERROR: Repository not found") {
67 return ErrRemoteNotFound
68 } else if strings.Contains(out, "for your current branch, you must specify a branch on the command line") {
69 return ErrRemoteBranchNotSpecified
70 } else if strings.Contains(out, "Automatic merge failed; fix conflicts and then commit the result") {
71 return ErrConflictAfterMerge
72 } else if strings.Contains(out, "error: Pulling is not possible because you have unmerged files.") {
73 return ErrUnmergedFiles
74 } else if strings.Contains(out, "unable to resolve reference") {
75 return ErrReferenceBroken
76 } else if strings.Contains(out, "git config --global add user.email") {
77 return ErrUserEmailNotSet
78 } else if strings.Contains(out, "Permission denied (publickey)") {
79 return ErrPermissionDenied
80 } else if strings.Contains(out, "would be overwritten by merge") {
81 return ErrOverwrittenByMerge
82 }
83 return ErrUnclassified
84 }