Error Variable Naming
ID |
go.err_prefixed_with_err |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Naming Convention |
Language |
Go |
Tags |
naming |
Description
Reports a package-level variable of error type whose name does not follow the Go
error-variable naming convention: exported error variables should be named ErrXxx and
unexported ones errXxx.
Rationale
The Err / err prefix marks a value as an error sentinel, the idiom used throughout the
standard library (os.ErrNotExist, sql.ErrNoRows). A consistent prefix lets callers
recognise sentinels they can compare against and keeps error declarations easy to find.
// Good — recognisable error sentinels
var ErrNotFound = errors.New("not found") // OK — exported, Err prefix
var errClosed = errors.New("closed") // OK — unexported, err prefix
// Bad — error values without the prefix
var NotFound error // FLAW — exported error without Err prefix
var failure error // FLAW — unexported error without err prefix
Remediation
Rename the variable so an exported error starts with Err and an unexported error starts
with err.