github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/nomad/structs/errors_test.go (about) 1 package structs 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestRPCCodedErrors(t *testing.T) { 11 cases := []struct { 12 err error 13 code int 14 message string 15 }{ 16 { 17 NewErrRPCCoded(400, "a test message,here"), 18 400, 19 "a test message,here", 20 }, 21 { 22 NewErrRPCCodedf(500, "a test message,here %s %s", "and,here%s", "second"), 23 500, 24 "a test message,here and,here%s second", 25 }, 26 } 27 28 for _, c := range cases { 29 t.Run(c.err.Error(), func(t *testing.T) { 30 code, msg, ok := CodeFromRPCCodedErr(c.err) 31 assert.True(t, ok) 32 assert.Equal(t, c.code, code) 33 assert.Equal(t, c.message, msg) 34 }) 35 } 36 37 negativeCases := []string{ 38 "random error", 39 errRPCCodedErrorPrefix, 40 errRPCCodedErrorPrefix + "123", 41 errRPCCodedErrorPrefix + "qwer,asdf", 42 } 43 for _, c := range negativeCases { 44 t.Run(c, func(t *testing.T) { 45 _, _, ok := CodeFromRPCCodedErr(errors.New(c)) 46 assert.False(t, ok) 47 }) 48 } 49 }