Skip to content

cmd/vet: ensure errors implement Is, As, and Unwrap anonymous interfaces #40356

Closed
@carnott-snap

Description

@carnott-snap

The interfaces for errors.Is and errors.As seem stable and complete. Unfortunately, it requires reading through the godoc to ensure that your custom error type correctly implements interface{ As(interface{}) bool }, interface{ Is(error) bool }, or interface{ Unwrap() error }. This can lead to implementation bugs that fail pathologically.

Instead of exposing these symbols, as was proposed in #39539, we can add a vet check to ensure that errors with Is, As, or Unwrap methods implement the correct interfaces. This is not a requirement that all errors implement Is, As, and Unwrap, but simply that if the symbol exists, the syntax matches.

Technically, this is not required, as you can use an anonymous type to perform this check at compile time. But this is ungainly, not automatic, and fails to the same typographical issues due to its distributed implementation:

var (
        _ = (interface{ As(interface{}) bool })(myError{})
        _ = (interface{ Is(error) bool })(myError{})
        _ = (interface{ Unwrap() error })(myError{})
        _ = error(myError{})

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions