This is an automated email from the ASF dual-hosted git repository.

dcelasun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git


The following commit(s) were added to refs/heads/master by this push:
     new be3f732  Make TTransportException unwrappable on Go 1.13+
be3f732 is described below

commit be3f7321cf0b7cff5d178ac09da02fc68dac6bd5
Author: Yuxuan 'fishy' Wang <[email protected]>
AuthorDate: Thu May 14 00:28:44 2020 -0700

    Make TTransportException unwrappable on Go 1.13+
    
    Client: go
    
    Go 1.13 introduced a new, optional, hidden interface for error
    implementations to make them unwrappable [1]. We currently already kind
    of support that (via TTransportException.Err), so just add a new
    function to make it Go 1.13+ compatible.
    
    [1] https://pkg.go.dev/[email protected]?tab=doc#pkg-overview
---
 lib/go/thrift/transport_exception.go      |  4 ++++
 lib/go/thrift/transport_exception_test.go | 28 ++++++++++++++++++++++++----
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/lib/go/thrift/transport_exception.go 
b/lib/go/thrift/transport_exception.go
index 9505b44..d2283ea 100644
--- a/lib/go/thrift/transport_exception.go
+++ b/lib/go/thrift/transport_exception.go
@@ -60,6 +60,10 @@ func (p *tTransportException) Err() error {
        return p.err
 }
 
+func (p *tTransportException) Unwrap() error {
+       return p.err
+}
+
 func NewTTransportException(t int, e string) TTransportException {
        return &tTransportException{typeId: t, err: errors.New(e)}
 }
diff --git a/lib/go/thrift/transport_exception_test.go 
b/lib/go/thrift/transport_exception_test.go
index b44314f..cf26258 100644
--- a/lib/go/thrift/transport_exception_test.go
+++ b/lib/go/thrift/transport_exception_test.go
@@ -36,25 +36,45 @@ func (t *timeout) Error() string {
        return fmt.Sprintf("Timeout: %v", t.timedout)
 }
 
+type unwrapper interface {
+       Unwrap() error
+}
+
 func TestTExceptionTimeout(t *testing.T) {
        timeout := &timeout{true}
        exception := NewTTransportExceptionFromError(timeout)
        if timeout.Error() != exception.Error() {
-               t.Fatalf("Error did not match: expected %q, got %q", 
timeout.Error(), exception.Error())
+               t.Errorf("Error did not match: expected %q, got %q", 
timeout.Error(), exception.Error())
        }
 
        if exception.TypeId() != TIMED_OUT {
-               t.Fatalf("TypeId was not TIMED_OUT: expected %v, got %v", 
TIMED_OUT, exception.TypeId())
+               t.Errorf("TypeId was not TIMED_OUT: expected %v, got %v", 
TIMED_OUT, exception.TypeId())
+       }
+
+       // NOTE: this can also be replaced by errors.Unwrap, but that requires
+       // go 1.13+.
+       if e, ok := exception.(unwrapper); !ok {
+               t.Error("Expected exception to be unwrappable, it is not.")
+       } else if e.Unwrap() != timeout {
+               t.Errorf("Unwrapped exception did not match: expected %v, got 
%v", timeout, e.Unwrap())
        }
 }
 
 func TestTExceptionEOF(t *testing.T) {
        exception := NewTTransportExceptionFromError(io.EOF)
        if io.EOF.Error() != exception.Error() {
-               t.Fatalf("Error did not match: expected %q, got %q", 
io.EOF.Error(), exception.Error())
+               t.Errorf("Error did not match: expected %q, got %q", 
io.EOF.Error(), exception.Error())
        }
 
        if exception.TypeId() != END_OF_FILE {
-               t.Fatalf("TypeId was not END_OF_FILE: expected %v, got %v", 
END_OF_FILE, exception.TypeId())
+               t.Errorf("TypeId was not END_OF_FILE: expected %v, got %v", 
END_OF_FILE, exception.TypeId())
+       }
+
+       // NOTE: this can also be replaced by errors.Unwrap, but that requires
+       // go 1.13+.
+       if e, ok := exception.(unwrapper); !ok {
+               t.Error("Expected exception to be unwrappable, it is not.")
+       } else if e.Unwrap() != io.EOF {
+               t.Errorf("Unwrapped exception did not match: expected %v, got 
%v", io.EOF, e.Unwrap())
        }
 }

Reply via email to