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