I have a code where I clone multiple gitlab repositories. The library I am depending on is "gopkg.in/src-d/go-git.v4"
. The clone function will return an error if a repository already exists.
I want to ignore this error and continue the loop of clonning repoistories. Below is my attempt to solve the issue by using errors.New()
However, it does not work since the returned err
and the new error do not match.
import (
gitgo "gopkg.in/src-d/go-git.v4"
"log"
"errors"
)
var errRepoIsThere = errors.New("repository already exists")
_, err := gitgo.PlainClone(repoLocalPath, false, &gitgo.CloneOptions{})
if !errors.Is(err, errRepoIsThere) {
log.Fatal(err)
}
the error returned from gitgo.PlainClone
is as defined here:
https://pkg.go.dev/github.com/go-git/go-git/v5#pkg-variables
ErrRepositoryNotExists = errors.New("repository does not exist")
I've went through this question
How to compare Go errors and saw that all awnsers discourage the use of err.Error() == err2.Error()
type of error handling.
What would be the right approach for my issue in this case?
CodePudding user response:
That error is a package level var - essentially, a singleton - so comparison is appropriate:
err == gitgo.ErrRepositoryNotExists
Comparing .Error()
is considered poor practice because error text is incidental (but package exports are assumed reliable)