github.com/blend/go-sdk@v1.20220411.3/validate/error_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package validate 9 10 import ( 11 "fmt" 12 "testing" 13 14 "github.com/blend/go-sdk/assert" 15 "github.com/blend/go-sdk/ex" 16 ) 17 18 func TestError(t *testing.T) { 19 assert := assert.New(t) 20 21 verr := Error(fmt.Errorf("this is a test"), nil) 22 assert.NotNil(verr) 23 assert.Equal(ErrValidation, ex.ErrClass(verr)) 24 assert.Empty(ex.ErrMessage(verr)) 25 assert.Equal(fmt.Errorf("this is a test"), ErrCause(verr)) 26 27 verr = Error(fmt.Errorf("this is a test"), nil, "foo", "bar") 28 assert.NotNil(verr) 29 assert.Equal(ErrValidation, ex.ErrClass(verr)) 30 assert.Equal("foobar", ErrMessage(verr)) 31 assert.Equal(fmt.Errorf("this is a test"), ErrCause(verr)) 32 } 33 34 func TestErrorf(t *testing.T) { 35 assert := assert.New(t) 36 37 verr := Errorf(fmt.Errorf("this is a test"), "foo", "minimum: %d", 30) 38 assert.NotNil(verr) 39 assert.Equal(ErrValidation, ex.ErrClass(verr)) 40 assert.Equal("minimum: 30", ErrMessage(verr)) 41 assert.Equal(fmt.Errorf("this is a test"), ErrCause(verr)) 42 } 43 44 func TestErrCause(t *testing.T) { 45 assert := assert.New(t) 46 47 err := ex.New(ErrNonLengthType) 48 assert.Equal(ErrNonLengthType, ex.ErrClass(err)) 49 assert.Equal(ErrNonLengthType, ErrCause(err)) 50 51 verr := Error(ErrEmpty, "foo") 52 assert.Equal(ErrValidation, ex.ErrClass(verr)) 53 assert.Equal(ErrEmpty, ErrCause(verr)) 54 }