github.com/pion/dtls/v2@v2.2.12/errors_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package dtls 5 6 import ( 7 "errors" 8 "fmt" 9 "net" 10 "testing" 11 ) 12 13 var errExample = errors.New("an example error") 14 15 func TestErrorUnwrap(t *testing.T) { 16 cases := []struct { 17 err error 18 errUnwrapped []error 19 }{ 20 { 21 &FatalError{Err: errExample}, 22 []error{errExample}, 23 }, 24 { 25 &TemporaryError{Err: errExample}, 26 []error{errExample}, 27 }, 28 { 29 &InternalError{Err: errExample}, 30 []error{errExample}, 31 }, 32 { 33 &TimeoutError{Err: errExample}, 34 []error{errExample}, 35 }, 36 { 37 &HandshakeError{Err: errExample}, 38 []error{errExample}, 39 }, 40 } 41 for _, c := range cases { 42 c := c 43 t.Run(fmt.Sprintf("%T", c.err), func(t *testing.T) { 44 err := c.err 45 for _, unwrapped := range c.errUnwrapped { 46 e := errors.Unwrap(err) 47 if !errors.Is(e, unwrapped) { 48 t.Errorf("Unwrapped error is expected to be '%v', got '%v'", unwrapped, e) 49 } 50 } 51 }) 52 } 53 } 54 55 func TestErrorNetError(t *testing.T) { 56 cases := []struct { 57 err error 58 str string 59 timeout, temporary bool 60 }{ 61 {&FatalError{Err: errExample}, "dtls fatal: an example error", false, false}, 62 {&TemporaryError{Err: errExample}, "dtls temporary: an example error", false, true}, 63 {&InternalError{Err: errExample}, "dtls internal: an example error", false, false}, 64 {&TimeoutError{Err: errExample}, "dtls timeout: an example error", true, true}, 65 {&HandshakeError{Err: errExample}, "handshake error: an example error", false, false}, 66 {&HandshakeError{Err: &TimeoutError{Err: errExample}}, "handshake error: dtls timeout: an example error", true, true}, 67 } 68 for _, c := range cases { 69 c := c 70 t.Run(fmt.Sprintf("%T", c.err), func(t *testing.T) { 71 var ne net.Error 72 if !errors.As(c.err, &ne) { 73 t.Fatalf("%T doesn't implement net.Error", c.err) 74 } 75 if ne.Timeout() != c.timeout { 76 t.Errorf("%T.Timeout() should be %v", c.err, c.timeout) 77 } 78 if ne.Temporary() != c.temporary { //nolint:staticcheck 79 t.Errorf("%T.Temporary() should be %v", c.err, c.temporary) 80 } 81 if ne.Error() != c.str { 82 t.Errorf("%T.Error() should be %v", c.err, c.str) 83 } 84 }) 85 } 86 }