Make NoArtifactError.Error() nil-safe#67
Make NoArtifactError.Error() nil-safe#67bdehamer merged 2 commits intobdehamer/no-artifact-cachefrom
NoArtifactError.Error() nil-safe#67Conversation
Co-authored-by: bdehamer <398027+bdehamer@users.noreply.github.com> Agent-Logs-Url: https://github.com/github/deployment-tracker/sessions/3eb7150d-f2e1-4ef9-8452-686a5ddb22f0
NoArtifactError.Error() nil-safe
There was a problem hiding this comment.
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 whennorn.erris 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.
| 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()) |
There was a problem hiding this comment.
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.
| if n == nil || n.err == nil { | ||
| return "no artifact found" | ||
| } |
There was a problem hiding this comment.
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.
NoArtifactError.Error()would panic on a nilerrfield, 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 inError(), returning"no artifact found"in either case🔒 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.