From c61d204aab8fc3447bd5aa0af1ce2a3f6648fc81 Mon Sep 17 00:00:00 2001 From: Thomas Hallgren Date: Wed, 19 Jan 2022 08:28:40 +0100 Subject: [PATCH] Add an Unwrap method to transport.ConnectionError Adds an `Unwrap` method to enable error checking with `errors.Is`. Signed-off-by: Thomas Hallgren --- internal/transport/transport.go | 6 ++++++ internal/transport/transport_test.go | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/internal/transport/transport.go b/internal/transport/transport.go index d3bf65b2bdf..0c43efaa649 100644 --- a/internal/transport/transport.go +++ b/internal/transport/transport.go @@ -741,6 +741,12 @@ func (e ConnectionError) Origin() error { return e.err } +// Unwrap returns the original error of this connection error or nil when the +// origin is nil. +func (e ConnectionError) Unwrap() error { + return e.err +} + var ( // ErrConnClosing indicates that the transport is closing. ErrConnClosing = connectionErrorf(true, nil, "transport is closing") diff --git a/internal/transport/transport_test.go b/internal/transport/transport_test.go index ec864afb6e9..b9a5d01fff6 100644 --- a/internal/transport/transport_test.go +++ b/internal/transport/transport_test.go @@ -27,6 +27,7 @@ import ( "io" "math" "net" + "os" "runtime" "strconv" "strings" @@ -2399,3 +2400,10 @@ func (s) TestClientDecodeHeaderStatusErr(t *testing.T) { }) } } + +func TestConnectionError_Unwrap(t *testing.T) { + err := connectionErrorf(false, os.ErrNotExist, "unwrap me") + if !errors.Is(err, os.ErrNotExist) { + t.Error("ConnectionError does not unwrap") + } +}