Skip to content

Support for Go 1.13 Error Wrapping #86

Description

@krak3n

Hi

I was wondering if you had any interest in support for go1.13 error wrapping? For example:

const (
	ErrIncorrectLength Error = iota + 1
	// Other sentinel error types here
)

type Error int

func (e Error) Error() string {
	switch e {
	case ErrIncorrectLength:
		return "incorrect length"
	// Other error strings here
	default:
		return "unknown error"
	}
}

func (u *UUID) UnmarshalText(text []byte) error {
	switch len(text) {
	// Previous switch conditions
	default:
		return fmt.Errorf("uuid: %w %d in string %q", ErrIncorrectLength, len(text), text)
	}
}

This would allow for the usage of errors.Is() and errors.As(), useful for testing or performing other conditional logic based on the type of error, for example:

id, err := uuid.FromString("foo")
if err != nil {
	if errors.Is(err, uuid.ErrIncorrectLength) {
		// some bespoke logic
	}
}

If you approve I'd be more than happy to open a PR with the implementation detail.If backwards compatibility is a concern we can probably get around it with build tags.

Demo over on the playground: https://play.golang.org/p/H5teBSdvfct

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions