[chore] - fix error comparisons (#2142)

* fix error comparisons

* fix imports
This commit is contained in:
ahrav
2023-12-01 08:32:41 -08:00
committed by GitHub
parent f3d51d1714
commit 279f915799
3 changed files with 16 additions and 15 deletions
+2 -1
View File
@@ -2,6 +2,7 @@ package launchdarkly
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"regexp" "regexp"
@@ -88,7 +89,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
_, err := ldclient.MakeCustomClient(resMatch, defaultSDKConfig, defaultSDKTimeout) _, err := ldclient.MakeCustomClient(resMatch, defaultSDKConfig, defaultSDKTimeout)
if err == nil { if err == nil {
s1.Verified = true s1.Verified = true
} else if err == ldclient.ErrInitializationFailed || err.Error() == invalidSDKKeyError { } else if errors.Is(err, ldclient.ErrInitializationFailed) || err.Error() == invalidSDKKeyError {
// If initialization fails, the key is not valid, so do nothing. // If initialization fails, the key is not valid, so do nothing.
} else { } else {
// If the error isn't nil or known, then this is likely a timeout error: ldclient.ErrInitializationTimeout // If the error isn't nil or known, then this is likely a timeout error: ldclient.ErrInitializationTimeout
+3 -2
View File
@@ -2,6 +2,7 @@ package output
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"log" "log"
"net/url" "net/url"
@@ -172,7 +173,7 @@ func GenerateDiff(commit *object.Commit, fileName string) string {
// First grab the first parent of the commit. If there are none, we are at the first commit and should diff against // First grab the first parent of the commit. If there are none, we are at the first commit and should diff against
// an empty file. // an empty file.
parent, err := commit.Parent(0) parent, err := commit.Parent(0)
if err != object.ErrParentNotFound && err != nil { if !errors.Is(err, object.ErrParentNotFound) && err != nil {
logger.Error(err, "could not find parent", "commit", commit.Hash.String()) logger.Error(err, "could not find parent", "commit", commit.Hash.String())
} }
@@ -180,7 +181,7 @@ func GenerateDiff(commit *object.Commit, fileName string) string {
var parentFile *object.File var parentFile *object.File
if parent != nil { if parent != nil {
parentFile, err = parent.File(fileName) parentFile, err = parent.File(fileName)
if err != nil && err != object.ErrFileNotFound { if err != nil && !errors.Is(err, object.ErrFileNotFound) {
logger.Error(err, "could not get previous version of file") logger.Error(err, "could not get previous version of file")
return diff return diff
} }
+11 -12
View File
@@ -3,6 +3,7 @@ package git
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"errors"
"fmt" "fmt"
"net/url" "net/url"
"os" "os"
@@ -13,9 +14,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/go-errors/errors"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/object"
"github.com/google/go-github/v42/github" "github.com/google/go-github/v42/github"
@@ -116,7 +115,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobId sources.JobID, so
var conn sourcespb.Git var conn sourcespb.Git
if err := anypb.UnmarshalTo(connection, &conn, proto.UnmarshalOptions{}); err != nil { if err := anypb.UnmarshalTo(connection, &conn, proto.UnmarshalOptions{}); err != nil {
return errors.WrapPrefix(err, "error unmarshalling connection", 0) return fmt.Errorf("error unmarshalling connection: %w", err)
} }
if uri := conn.GetUri(); uri != "" { if uri := conn.GetUri(); uri != "" {
@@ -131,7 +130,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobId sources.JobID, so
if err != nil { if err != nil {
return fmt.Errorf("error creating filter: %w", err) return fmt.Errorf("error creating filter: %w", err)
} }
opts := []ScanOption{ScanOptionFilter(filter), ScanOptionLogOptions(new(gogit.LogOptions))} opts := []ScanOption{ScanOptionFilter(filter), ScanOptionLogOptions(new(git.LogOptions))}
if depth := conn.GetMaxDepth(); depth != 0 { if depth := conn.GetMaxDepth(); depth != 0 {
opts = append(opts, ScanOptionMaxDepth(depth)) opts = append(opts, ScanOptionMaxDepth(depth))
@@ -388,7 +387,7 @@ func executeClone(ctx context.Context, params cloneParams) (*git.Repository, err
// Execute command and wait for the stdout / stderr. // Execute command and wait for the stdout / stderr.
output, err := cloneCmd.CombinedOutput() output, err := cloneCmd.CombinedOutput()
if err != nil { if err != nil {
err = errors.WrapPrefix(err, "error running 'git clone'", 0) err = fmt.Errorf("error executing git clone: %w", err)
} }
logger.V(3).Info("git subcommand finished", "output", string(output)) logger.V(3).Info("git subcommand finished", "output", string(output))
@@ -742,14 +741,14 @@ func normalizeConfig(scanOptions *ScanOptions, repo *git.Repository) (err error)
if !plumbing.IsHash(scanOptions.BaseHash) { if !plumbing.IsHash(scanOptions.BaseHash) {
base, err := TryAdditionalBaseRefs(repo, scanOptions.BaseHash) base, err := TryAdditionalBaseRefs(repo, scanOptions.BaseHash)
if err != nil { if err != nil {
return errors.WrapPrefix(err, "unable to resolve base ref", 0) return fmt.Errorf("unable to resolve base ref: %w", err)
} }
scanOptions.BaseHash = base.String() scanOptions.BaseHash = base.String()
baseCommit, _ = repo.CommitObject(plumbing.NewHash(scanOptions.BaseHash)) baseCommit, _ = repo.CommitObject(plumbing.NewHash(scanOptions.BaseHash))
} else { } else {
baseCommit, err = repo.CommitObject(baseHash) baseCommit, err = repo.CommitObject(baseHash)
if err != nil { if err != nil {
return errors.WrapPrefix(err, "unable to resolve base ref", 0) return fmt.Errorf("unable to resolve base ref: %w", err)
} }
} }
} }
@@ -760,14 +759,14 @@ func normalizeConfig(scanOptions *ScanOptions, repo *git.Repository) (err error)
if !plumbing.IsHash(scanOptions.HeadHash) { if !plumbing.IsHash(scanOptions.HeadHash) {
head, err := TryAdditionalBaseRefs(repo, scanOptions.HeadHash) head, err := TryAdditionalBaseRefs(repo, scanOptions.HeadHash)
if err != nil { if err != nil {
return errors.WrapPrefix(err, "unable to resolve head ref", 0) return fmt.Errorf("unable to resolve head ref: %w", err)
} }
scanOptions.HeadHash = head.String() scanOptions.HeadHash = head.String()
headCommit, _ = repo.CommitObject(plumbing.NewHash(scanOptions.HeadHash)) headCommit, _ = repo.CommitObject(plumbing.NewHash(scanOptions.HeadHash))
} else { } else {
headCommit, err = repo.CommitObject(headHash) headCommit, err = repo.CommitObject(headHash)
if err != nil { if err != nil {
return errors.WrapPrefix(err, "unable to resolve head ref", 0) return fmt.Errorf("unable to resolve head ref: %w", err)
} }
} }
} }
@@ -776,7 +775,7 @@ func normalizeConfig(scanOptions *ScanOptions, repo *git.Repository) (err error)
if headCommit != nil && baseCommit != nil { if headCommit != nil && baseCommit != nil {
mergeBase, err := headCommit.MergeBase(baseCommit) mergeBase, err := headCommit.MergeBase(baseCommit)
if err != nil || len(mergeBase) < 1 { if err != nil || len(mergeBase) < 1 {
return errors.WrapPrefix(err, "could not find common base between the given references", 0) return fmt.Errorf("unable to resolve merge base: %w", err)
} }
scanOptions.BaseHash = mergeBase[0].Hash.String() scanOptions.BaseHash = mergeBase[0].Hash.String()
} }
@@ -791,7 +790,7 @@ func stripPassword(u string) (string, error) {
repoURL, err := url.Parse(u) repoURL, err := url.Parse(u)
if err != nil { if err != nil {
return "", errors.WrapPrefix(err, "repo remote cannot be sanitized as URI", 0) return "", fmt.Errorf("repo remote is not a URI: %w", err)
} }
repoURL.User = nil repoURL.User = nil
@@ -808,7 +807,7 @@ func TryAdditionalBaseRefs(repo *git.Repository, base string) (*plumbing.Hash, e
} }
for _, prefix := range revisionPrefixes { for _, prefix := range revisionPrefixes {
outHash, err := repo.ResolveRevision(plumbing.Revision(prefix + base)) outHash, err := repo.ResolveRevision(plumbing.Revision(prefix + base))
if err == plumbing.ErrReferenceNotFound { if errors.Is(err, plumbing.ErrReferenceNotFound) {
continue continue
} }
if err != nil { if err != nil {