errortype is a linter that finds inconsistent uses of Go error types. v0.0.12 can rename errors to follow the naming conventions, checks %w operands of fmt.Errorf, and prepares you for a new go vet check in Go 1.27.
gopls v0.24 ships a new analyzer, ptrtoerror, that reports when an error type is used both as a value E and as a pointer *E. That's the idea errortype is built around. Because ptrtoerror is on by default, a lot of Go developers will now see these bugs while they write code. Kudos to the gopls team!
The two analyzers complement each other. ptrtoerror asks you to declare which form you mean, and errortype already uses these declarations and checks more places, like %w operands and errors.As targets. More on that below.
Error naming, fixed automatically
The Go conventions say sentinel errors start with Err (ErrNotFound) and error types end with Error (ParseError). The name tells readers and tools whether to match with errors.Is or query with errors.AsType.
-naming checks this. On a large codebase nobody wants to rename errors by hand, so -fix renames the declarations and their uses in the package. You can run it on a whole module, or fix new names as you write them:
-var NotFound = errors.New("not found")
+// Deprecated: Use [ErrNotFound] instead.
+var NotFound = ErrNotFound
+
+var ErrNotFound = errors.New("not found")
-type ErrParse struct{ Line int }
+// Deprecated: Use [ParseError] instead.
+//
+//go:fix inline
+type ErrParse = ParseError
+
+type ParseError struct{ Line int }
Exported declarations keep the old name with a // Deprecated: comment so other packages keep compiling. Deprecated type aliases get //go:fix inline, so go fix ./... migrates their uses, while uses of renamed var sentinels in other packages are left to you. You can preview the names with -fix -diff.
-naming is off by default. Try it during development (included in -recommended), and turn it on in CI once you're compliant.
%w operands are checked
The following program sums up numbers, one per line, but never hits its ParseError branch, printing the generic fallback instead (full version on the Go Playground):
type ParseError struct {
Line int
Err error
}
func (e ParseError) Error() string { return fmt.Sprintf("line %d: %v", e.Line, e.Err) }
// in sumLines:
return 0, fmt.Errorf("sum: %w", ParseError{Line: line, Err: err}) // wrapped as a value
// in printError:
if perr, ok := errors.AsType[*ParseError](err); ok { // never matches
$ go run .
unexpected error: sum: line 3: strconv.Atoi: parsing "3x": invalid syntax
$ errortype .
.../main.go:48:17: Error type "ParseError" should be queried as a value ("errors.AsType[ParseError]"), not a pointer. (et:ast)
The program compiles, and nothing complains. The %w operand is passed as any, and *ParseError implements error just like ParseError. errortype now counts %w operands as evidence for the intended form, so it tells you to fix the query. When a pointer Unwrap method already settles the form, it reports the %w operand instead.
A new go vet check in Go 1.27
In files at go 1.27 or later, go vet's printf check now reports %w on *E whenever E itself implements error. Because this depends on the file's Go version (usually the go directive in go.mod) rather than the toolchain, go vet stays quiet even with a Go 1.27 toolchain, until you bump go.mod to 1.27. At that point it starts flagging working code, like an error type with a value receiver that is consistently used as a pointer (Go Playground, try editing the go.mod part at the bottom):
type StatusError struct{ Code int }
func (e StatusError) Error() string { return fmt.Sprintf("status %d", e.Code) }
func get() error {
// go vet at go 1.27: %w wants operand of error type StatusError, not pointer type *StatusError (defeats errors.Is)
return fmt.Errorf("get: %w", &StatusError{404})
}
errortype -receivers flags the receiver. At go 1.27 or later, it's reported by default:
.../main.go:10:6: Method "Error" should be implemented with a pointer receiver ("func (*StatusError) Error"), not a value receiver (et:rcv+)
With a pointer receiver, only *StatusError implements error. go vet has nothing to report, and the compiler rejects returning StatusError{...}.
Fix these while you're still on go 1.26, and you won't have to when you upgrade. After raising the go directive, go vet can still report types that only embed an error or come from a dependency.
Legacy error query helpers
Pre-Go 1.13 helpers like func IsNotFound(err error) bool with a plain type assertion ignore wrapped errors. -legacy reports them, and -fix rewrites them to errors.AsType (or errors.As on files below Go 1.26), correcting the pointer or value form at the same time. It's off by default for now and might be turned on later.
Found in real projects
errortype recognizes wrapper functions around the errors package, and that's how it found bugs in Grafana Loki, Canonical Juju and Chromium's LUCI: error handlers passed invalid targets to errors.As through github.com/pkg/errors, github.com/juju/errors and LUCI's own errors package, and would have panicked the first time they ran. All three are fixed upstream.
The latest one is in Grafana Mimir, and this time every conversion used the same form. The compactor counts failed block uploads by file type, using UploadError:
type UploadError struct {
cause error
fileType FileType
}
func (e UploadError) Error() string { return fmt.Sprintf("failed to upload %s: %v", e.fileType, e.cause) }
func (e *UploadError) Unwrap() error { return e.cause }
Every return in the package used the value UploadError{...}. The compactor checked for a *UploadError with errors.As, which never matched, so every known failure was counted as type="unknown". The pointer Unwrap also hid the cause from errors.Is.
#12477 fixed the bug by returning &UploadError{...}. errortype reports those value returns on the old code, and on current main it still reports the root cause:
$ errortype ./pkg/storage/tsdb/block
.../block/block.go:66:6: Method "Error" should be implemented with a pointer receiver ("func (*UploadError) Error"), like "Unwrap" (et:rcv+)
...
It also reports three places in the tests that still use the value form, one of which ptrtoerror flags as well. With a pointer receiver, a value return no longer compiles, so the bug can't come back. I've sent that fix upstream as #17467. The README has a minimal version of this pattern.
errortype and ptrtoerror from gopls
ptrtoerror is strict about types where both E and *E implement error. When the defining package doesn't show which form it means, gopls asks you to add var _ error = ... and offers that as a quick fix.
Each of these declarations helps errortype. It has recommended them from the start, because then it doesn't have to infer the form. With gopls asking, far more packages will declare their intent.
errortype covers code without declarations as well, since it infers the form from how a type is used. None of the bugs above involved an inconsistent conversion. I run errortype before I commit and in CI, and as a golangci-lint plugin in my editor.
Try it
go install fillmore-labs.com/errortype@v0.0.12
errortype -recommended ./...
Feedback is very welcome.