Skip to content

chore: Add nil checks before returning wrapped error #5309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/controllers/events/events_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ func (e *VCSEventsController) HandleGithubCommentEvent(event *github.IssueCommen

baseRepo, user, pullNum, err := e.Parser.ParseGithubIssueCommentEvent(logger, event)

wrapped := errors.Wrapf(err, "Failed parsing event: %s", githubReqID)
if err != nil {
wrapped := errors.Wrapf(err, "Failed parsing event: %s", githubReqID)
return HTTPResponse{
body: wrapped.Error(),
err: HTTPError{
Expand Down
6 changes: 5 additions & 1 deletion server/controllers/websocket/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,9 @@ func (m *Multiplexor) Handle(w http.ResponseWriter, r *http.Request) error {
go m.registry.Register(key, buffer)
defer m.registry.Deregister(key, buffer)

return errors.Wrapf(m.writer.Write(w, r, buffer), "writing to ws %s", key)
err = m.writer.Write(w, r, buffer)
if err != nil {
return errors.Wrapf(err, "writing to ws %s", key)
}
return nil
}
24 changes: 19 additions & 5 deletions server/core/db/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func (b *BoltDB) Unlock(p models.Project, workspace string) (*models.ProjectLock
}
return bucket.Delete([]byte(key))
})
err = errors.Wrap(err, "DB transaction failed")
if err != nil {
err = errors.Wrap(err, "DB transaction failed")
}
if foundLock {
return &lock, err
}
Expand Down Expand Up @@ -387,7 +389,10 @@ func (b *BoltDB) UpdatePullWithResults(pull models.PullRequest, newResults []com
// Now, we overwrite the key with our new status.
return b.writePullToBucket(bucket, key, newStatus)
})
return newStatus, errors.Wrap(err, "DB transaction failed")
if err != nil {
return models.PullStatus{}, fmt.Errorf("DB transaction failed: %w", err)
}
return newStatus, nil
}

// GetPullStatus returns the status for pull.
Expand All @@ -404,7 +409,10 @@ func (b *BoltDB) GetPullStatus(pull models.PullRequest) (*models.PullStatus, err
s, txErr = b.getPullFromBucket(bucket, key)
return txErr
})
return s, errors.Wrap(err, "DB transaction failed")
if err != nil {
return nil, errors.Wrap(err, "DB transaction failed")
}
return s, nil
}

// DeletePullStatus deletes the status for pull.
Expand All @@ -417,7 +425,10 @@ func (b *BoltDB) DeletePullStatus(pull models.PullRequest) error {
bucket := tx.Bucket(b.pullsBucketName)
return bucket.Delete(key)
})
return errors.Wrap(err, "DB transaction failed")
if err != nil {
return errors.Wrap(err, "DB transaction failed")
}
return nil
}

// UpdateProjectStatus updates project status.
Expand Down Expand Up @@ -449,7 +460,10 @@ func (b *BoltDB) UpdateProjectStatus(pull models.PullRequest, workspace string,
}
return b.writePullToBucket(bucket, key, currStatus)
})
return errors.Wrap(err, "DB transaction failed")
if err != nil {
return errors.Wrap(err, "DB transaction failed")
}
return nil
}

func (b *BoltDB) pullKey(pull models.PullRequest) ([]byte, error) {
Expand Down
38 changes: 30 additions & 8 deletions server/core/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ func (r *RedisDB) LockCommand(cmdName command.Name, lockTime time.Time) (*comman
_, err := r.client.Get(ctx, cmdLockKey).Result()
if err == redis.Nil {
err = r.client.Set(ctx, cmdLockKey, newLockSerialized, 0).Err()
return &lock, errors.Wrap(err, "db transaction failed")
if err != nil {
return nil, errors.Wrap(err, "db transaction failed")
}
return &lock, nil
} else if err != nil {
return nil, errors.Wrap(err, "db transaction failed")
}
Expand Down Expand Up @@ -267,7 +270,10 @@ func (r *RedisDB) UpdateProjectStatus(pull models.PullRequest, workspace string,
}

err = r.writePull(key, currStatus)
return errors.Wrap(err, "db transaction failed")
if err != nil {
return errors.Wrap(err, "db transaction failed")
}
return nil
}

func (r *RedisDB) GetPullStatus(pull models.PullRequest) (*models.PullStatus, error) {
Expand All @@ -277,16 +283,22 @@ func (r *RedisDB) GetPullStatus(pull models.PullRequest) (*models.PullStatus, er
}

pullStatus, err := r.getPull(key)

return pullStatus, errors.Wrap(err, "db transaction failed")
if err != nil {
return nil, errors.Wrap(err, "db transaction failed")
}
return pullStatus, nil
}

func (r *RedisDB) DeletePullStatus(pull models.PullRequest) error {
key, err := r.pullKey(pull)
if err != nil {
return err
}
return errors.Wrap(r.deletePull(key), "db transaction failed")
err = r.deletePull(key)
if err != nil {
return errors.Wrap(err, "db transaction failed")
}
return nil
}

func (r *RedisDB) UpdatePullWithResults(pull models.PullRequest, newResults []command.ProjectResult) (models.PullStatus, error) {
Expand Down Expand Up @@ -359,7 +371,11 @@ func (r *RedisDB) UpdatePullWithResults(pull models.PullRequest, newResults []co
}

// Now, we overwrite the key with our new status.
return newStatus, errors.Wrap(r.writePull(key, newStatus), "db transaction failed")
err = r.writePull(key, newStatus)
if err != nil {
return models.PullStatus{}, errors.Wrap(err, "db transaction failed")
}
return newStatus, nil
}

func (r *RedisDB) getPull(key string) (*models.PullStatus, error) {
Expand All @@ -383,12 +399,18 @@ func (r *RedisDB) writePull(key string, pull models.PullStatus) error {
return errors.Wrap(err, "serializing")
}
err = r.client.Set(ctx, key, serialized, 0).Err()
return errors.Wrap(err, "DB Transaction failed")
if err != nil {
return errors.Wrap(err, "DB Transaction failed")
}
return nil
}

func (r *RedisDB) deletePull(key string) error {
err := r.client.Del(ctx, key).Err()
return errors.Wrap(err, "DB Transaction failed")
if err != nil {
return errors.Wrap(err, "DB Transaction failed")
}
return nil
}

func (r *RedisDB) lockKey(p models.Project, workspace string) string {
Expand Down
6 changes: 3 additions & 3 deletions server/core/runtime/models/shell_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ func (s *ShellCommandRunner) RunCommandAsync(ctx command.ProjectContext) (chan<-
ctx.Log.Debug("writing %q to remote command's stdin", line)
_, err := io.WriteString(stdin, line)
if err != nil {
ctx.Log.Err(errors.Wrapf(err, "writing %q to process", line).Error())
err = errors.Wrapf(err, "writing %q to process", line)
ctx.Log.Err(err.Error())
}
}
}()
Expand Down Expand Up @@ -173,8 +174,7 @@ func (s *ShellCommandRunner) RunCommandAsync(ctx command.ProjectContext) (chan<-

// We're done now. Send an error if there was one.
if err != nil {
err = errors.Wrapf(err, "running '%s' '%s' in '%s'",
s.shell.String(), s.command, s.workingDir)
err = errors.Wrapf(err, "running '%s' '%s' in '%s'", s.shell.String(), s.command, s.workingDir)
log.Err(err.Error())
outCh <- Line{Err: err}
} else {
Expand Down
3 changes: 1 addition & 2 deletions server/events/pending_plan_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ func (p *DefaultPendingPlanFinder) findWithAbsPaths(pullDir string) ([]PendingPl
lsCmd.Dir = repoDir
lsOut, err := lsCmd.CombinedOutput()
if err != nil {
return nil, nil, errors.Wrapf(err, "running 'git ls-files . --others' in '%s' directory: %s",
repoDir, string(lsOut))
return nil, nil, errors.Wrapf(err, "running 'git ls-files . --others' in '%s' directory: %s", repoDir, string(lsOut))
}
for _, file := range strings.Split(string(lsOut), "\n") {
if filepath.Ext(file) == ".tfplan" {
Expand Down
3 changes: 3 additions & 0 deletions server/events/vcs/azuredevops_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func (g *AzureDevopsClient) GetModifiedFiles(logger logging.SimpleLogging, repo
sourceRefName := strings.Replace(pullRequest.GetSourceRefName(), "refs/heads/", "", 1)

r, resp, err := g.Client.Git.GetDiffs(g.ctx, owner, project, repoName, targetRefName, sourceRefName)
if err != nil {
return nil, errors.Wrap(err, "getting pull request")
}
if resp.StatusCode != http.StatusOK {
return nil, errors.Wrapf(err, "http response code %d getting diff %s to %s", resp.StatusCode, sourceRefName, targetRefName)
}
Expand Down
11 changes: 6 additions & 5 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,16 +542,14 @@ func (g *GitlabClient) MergePull(logger logging.SimpleLogging, pull models.PullR

mr, err := g.GetMergeRequest(logger, pull.BaseRepo.FullName, pull.Num)
if err != nil {
return errors.Wrap(
err, "unable to merge merge request, it was not possible to retrieve the merge request")
return errors.Wrap(err, "unable to merge merge request, it was not possible to retrieve the merge request")
}
project, resp, err := g.Client.Projects.GetProject(mr.ProjectID, nil)
if resp != nil {
logger.Debug("GET /projects/%d returned: %d", mr.ProjectID, resp.StatusCode)
}
if err != nil {
return errors.Wrap(
err, "unable to merge merge request, it was not possible to check the project requirements")
return errors.Wrap(err, "unable to merge merge request, it was not possible to check the project requirements")
}

if project != nil && project.OnlyAllowMergeIfPipelineSucceeds {
Expand All @@ -568,7 +566,10 @@ func (g *GitlabClient) MergePull(logger logging.SimpleLogging, pull models.PullR
if resp != nil {
logger.Debug("PUT /projects/%s/merge_requests/%d/merge returned: %d", pull.BaseRepo.FullName, pull.Num, resp.StatusCode)
}
return errors.Wrap(err, "unable to merge merge request, it may not be in a mergeable state")
if err != nil {
return errors.Wrap(err, "unable to merge merge request, it may not be in a mergeable state")
}
return nil
}

// MarkdownPullLink specifies the string used in a pull request comment to reference another pull request.
Expand Down
3 changes: 1 addition & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {

parsedURL, err := ParseAtlantisURL(userConfig.AtlantisURL)
if err != nil {
return nil, errors.Wrapf(err,
"parsing --%s flag %q", config.AtlantisURLFlag, userConfig.AtlantisURL)
return nil, errors.Wrapf(err, "parsing --%s flag %q", config.AtlantisURLFlag, userConfig.AtlantisURL)
}

underlyingRouter := mux.NewRouter()
Expand Down
10 changes: 8 additions & 2 deletions testdrive/testdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,14 @@ tunnels:
colorstring.Println("\n[green]Thank you for using atlantis :) \n[reset]For more information about how to use atlantis in production go to: https://www.runatlantis.io")
return nil
case err := <-ngrokErrors:
return errors.Wrap(err, "ngrok tunnel")
if err != nil {
err = errors.Wrap(err, "ngrok tunnel")
}
return err
case err := <-atlantisErrors:
return errors.Wrap(err, "atlantis server")
if err != nil {
err = errors.Wrap(err, "atlantis server")
}
return err
}
}