github.com/blend/go-sdk@v1.20220411.3/ex/util_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 ex
     9  
    10  import (
    11  	"fmt"
    12  	"testing"
    13  
    14  	"github.com/blend/go-sdk/assert"
    15  )
    16  
    17  type classProvider struct {
    18  	error
    19  	ErrClass error
    20  }
    21  
    22  func (cp classProvider) Class() error {
    23  	return cp.ErrClass
    24  }
    25  
    26  func TestErrClass(t *testing.T) {
    27  	assert := assert.New(t)
    28  
    29  	assert.Nil(ErrClass(nil))
    30  	var unsetErr error
    31  	assert.Nil(ErrClass(unsetErr))
    32  
    33  	assert.Nil(ErrClass("foo"))
    34  
    35  	err := New("this is a test")
    36  	assert.Equal("this is a test", ErrClass(err).Error())
    37  
    38  	cp := classProvider{
    39  		error:    fmt.Errorf("this is a provider test"),
    40  		ErrClass: fmt.Errorf("the error class"),
    41  	}
    42  	assert.Equal("the error class", ErrClass(cp).Error())
    43  	assert.Equal("this is a test", ErrClass(fmt.Errorf("this is a test")).Error())
    44  }
    45  
    46  func TestErrMessage(t *testing.T) {
    47  	assert := assert.New(t)
    48  
    49  	assert.Empty(ErrMessage(nil))
    50  	assert.Empty(ErrMessage(fmt.Errorf("foo bar baz")))
    51  	assert.Equal("this is a message", ErrMessage(New("error class", OptMessage("this is a message"))))
    52  }
    53  
    54  type stackProvider struct {
    55  	error
    56  	Stack StackTrace
    57  }
    58  
    59  func (sp stackProvider) StackTrace() StackTrace {
    60  	return sp.Stack
    61  }
    62  
    63  func TestErrStackTrace(t *testing.T) {
    64  	assert := assert.New(t)
    65  
    66  	err := New("this is a test")
    67  	assert.NotNil(ErrStackTrace(err))
    68  
    69  	sp := stackProvider{
    70  		error: fmt.Errorf("this is a provider test"),
    71  		Stack: StackStrings([]string{"first", "second"}),
    72  	}
    73  	assert.Equal([]string{"first", "second"}, ErrStackTrace(sp).Strings())
    74  
    75  	assert.Nil(ErrStackTrace(fmt.Errorf("this is also a test")))
    76  }