Skip to content

Make NoArtifactError.Error() nil-safe#67

Merged
bdehamer merged 2 commits intobdehamer/no-artifact-cachefrom
copilot/sub-pr-66
Mar 20, 2026
Merged

Make NoArtifactError.Error() nil-safe#67
bdehamer merged 2 commits intobdehamer/no-artifact-cachefrom
copilot/sub-pr-66

Conversation

Copy link

Copilot AI commented Mar 20, 2026

NoArtifactError.Error() would panic on a nil err field, making zero-value construction (&NoArtifactError{}) unsafe — a pattern used throughout controller tests.

Changes

  • pkg/deploymentrecord/client.go: Guard against both a nil receiver and a nil wrapped error in Error(), returning "no artifact found" in either case
func (n *NoArtifactError) Error() string {
    if n == nil || n.err == nil {
        return "no artifact found"
    }
    return fmt.Sprintf("no artifact found: %s", n.err.Error())
}

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI changed the title [WIP] [WIP] Address feedback on making NoArtifactError.Error() nil-safe Make NoArtifactError.Error() nil-safe Mar 20, 2026
Copilot AI requested a review from bdehamer March 20, 2026 21:41
@bdehamer bdehamer marked this pull request as ready for review March 20, 2026 21:42
@bdehamer bdehamer requested a review from a team as a code owner March 20, 2026 21:42
Copilot AI review requested due to automatic review settings March 20, 2026 21:42
@bdehamer bdehamer merged commit 3195680 into bdehamer/no-artifact-cache Mar 20, 2026
5 checks passed
@bdehamer bdehamer deleted the copilot/sub-pr-66 branch March 20, 2026 21:43
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes deploymentrecord.NoArtifactError.Error() safe to call when the receiver is nil or when the wrapped err field is nil, preventing panics from zero-value usage patterns (notably in controller tests).

Changes:

  • Add nil checks in (*NoArtifactError).Error() to avoid panics when n or n.err is nil.
  • Return a consistent fallback message ("no artifact found") for nil-safe cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 172 to 176
func (n *NoArtifactError) Error() string {
if n == nil || n.err == nil {
return "no artifact found"
}
return fmt.Sprintf("no artifact found: %s", n.err.Error())
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error() is now nil-receiver safe, but Unwrap() still dereferences n unconditionally. If a nil *NoArtifactError is ever stored in an error interface, errors.Unwrap/Is can call Unwrap() and panic. Consider making Unwrap() return nil when the receiver is nil (and/or when n.err is nil) to keep the type fully nil-safe.

Copilot uses AI. Check for mistakes.
Comment on lines +173 to +175
if n == nil || n.err == nil {
return "no artifact found"
}
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change adds a new behavior path (n == nil or n.err == nil) but there isn't a unit test asserting that (&NoArtifactError{}).Error() returns the expected message and does not panic. Adding a small regression test would prevent future reintroductions of the nil-deref.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants