Skip to content

Commit

Permalink
[chore] - fix error comparisons (#2142)
Browse files Browse the repository at this point in the history
* fix error comparisons

* fix imports
  • Loading branch information
ahrav authored Dec 1, 2023
1 parent f3d51d1 commit 279f915
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
3 changes: 2 additions & 1 deletion pkg/detectors/launchdarkly/launchdarkly.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package launchdarkly

import (
"context"
"errors"
"fmt"
"net/http"
"regexp"
Expand Down Expand Up @@ -88,7 +89,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
_, err := ldclient.MakeCustomClient(resMatch, defaultSDKConfig, defaultSDKTimeout)
if err == nil {
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.
} else {
// If the error isn't nil or known, then this is likely a timeout error: ldclient.ErrInitializationTimeout
Expand Down
5 changes: 3 additions & 2 deletions pkg/output/legacy_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package output

import (
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
Expand Down Expand Up @@ -172,15 +173,15 @@ 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
// an empty file.
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())
}

// Now get the files from the commit and its parent.
var parentFile *object.File
if parent != nil {
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")
return diff
}
Expand Down
23 changes: 11 additions & 12 deletions pkg/sources/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package git
import (
"bufio"
"bytes"
"errors"
"fmt"
"net/url"
"os"
Expand All @@ -13,9 +14,7 @@ import (
"sync/atomic"
"time"

"github.com/go-errors/errors"
"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/object"
"github.com/google/go-github/v42/github"
Expand Down Expand Up @@ -116,7 +115,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobId sources.JobID, so

var conn sourcespb.Git
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 != "" {
Expand All @@ -131,7 +130,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobId sources.JobID, so
if err != nil {
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 {
opts = append(opts, ScanOptionMaxDepth(depth))
Expand Down Expand Up @@ -388,7 +387,7 @@ func executeClone(ctx context.Context, params cloneParams) (*git.Repository, err
// Execute command and wait for the stdout / stderr.
output, err := cloneCmd.CombinedOutput()
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))

Expand Down Expand Up @@ -742,14 +741,14 @@ func normalizeConfig(scanOptions *ScanOptions, repo *git.Repository) (err error)
if !plumbing.IsHash(scanOptions.BaseHash) {
base, err := TryAdditionalBaseRefs(repo, scanOptions.BaseHash)
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()
baseCommit, _ = repo.CommitObject(plumbing.NewHash(scanOptions.BaseHash))
} else {
baseCommit, err = repo.CommitObject(baseHash)
if err != nil {
return errors.WrapPrefix(err, "unable to resolve base ref", 0)
return fmt.Errorf("unable to resolve base ref: %w", err)
}
}
}
Expand All @@ -760,14 +759,14 @@ func normalizeConfig(scanOptions *ScanOptions, repo *git.Repository) (err error)
if !plumbing.IsHash(scanOptions.HeadHash) {
head, err := TryAdditionalBaseRefs(repo, scanOptions.HeadHash)
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()
headCommit, _ = repo.CommitObject(plumbing.NewHash(scanOptions.HeadHash))
} else {
headCommit, err = repo.CommitObject(headHash)
if err != nil {
return errors.WrapPrefix(err, "unable to resolve head ref", 0)
return fmt.Errorf("unable to resolve head ref: %w", err)
}
}
}
Expand All @@ -776,7 +775,7 @@ func normalizeConfig(scanOptions *ScanOptions, repo *git.Repository) (err error)
if headCommit != nil && baseCommit != nil {
mergeBase, err := headCommit.MergeBase(baseCommit)
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()
}
Expand All @@ -791,7 +790,7 @@ func stripPassword(u string) (string, error) {

repoURL, err := url.Parse(u)
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
Expand All @@ -808,7 +807,7 @@ func TryAdditionalBaseRefs(repo *git.Repository, base string) (*plumbing.Hash, e
}
for _, prefix := range revisionPrefixes {
outHash, err := repo.ResolveRevision(plumbing.Revision(prefix + base))
if err == plumbing.ErrReferenceNotFound {
if errors.Is(err, plumbing.ErrReferenceNotFound) {
continue
}
if err != nil {
Expand Down

0 comments on commit 279f915

Please sign in to comment.