Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Add support for Go 1.13 error chains #206

Merged
merged 4 commits into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ type withStack struct {

func (w *withStack) Cause() error { return w.error }

// Unwrap provides compatibility for Go 1.13 error chains.
func (w *withStack) Unwrap() error { return w.error }

func (w *withStack) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
Expand Down Expand Up @@ -241,6 +244,9 @@ type withMessage struct {
func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() }
func (w *withMessage) Cause() error { return w.cause }

// Unwrap provides compatibility for Go 1.13 error chains.
func (w *withMessage) Unwrap() error { return w.cause }

func (w *withMessage) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
Expand Down
16 changes: 16 additions & 0 deletions go113_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// +build go1.13

package errors

import (
stdlib_errors "errors"
"testing"
)

func TestErrorChainCompat(t *testing.T) {
err := stdlib_errors.New("error that gets wrapped")
wrapped := Wrap(err, "wrapped up")
if !stdlib_errors.Is(wrapped, err) {
t.Errorf("Wrap does not support Go 1.13 error chains")
}
}