gopkg.in/hedzr/errors.v3@v3.3.1/join_test.go (about)

     1  // Copyright © 2023 Hedzr Yeh.
     2  
     3  package errors_test
     4  
     5  import (
     6  	"errors"
     7  	"fmt"
     8  	"testing"
     9  
    10  	v3 "gopkg.in/hedzr/errors.v3"
    11  )
    12  
    13  func TestJoinErrors(t *testing.T) {
    14  	err1 := errors.New("err1")
    15  	err2 := errors.New("err2")
    16  	err := v3.Join(err1, err2)
    17  	fmt.Printf("%T, %v\n", err, err)
    18  	if v3.Is(err, err1) {
    19  		t.Log("err is err1")
    20  	} else {
    21  		t.Fatal("expecting err is err1")
    22  	}
    23  	if v3.Is(err, err2) {
    24  		t.Log("err is err2")
    25  	} else {
    26  		t.Fatal("expecting err is err2")
    27  	}
    28  }
    29  
    30  type DivisionError struct {
    31  	IntA int
    32  	IntB int
    33  	Msg  string
    34  }
    35  
    36  func (e *DivisionError) Error() string {
    37  	return e.Msg
    38  }
    39  
    40  func Divide(a, b int) (int, error) {
    41  	if b == 0 {
    42  		return 0, &DivisionError{
    43  			Msg:  fmt.Sprintf("cannot divide '%d' by zero", a),
    44  			IntA: a, IntB: b,
    45  		}
    46  	}
    47  	return a / b, nil
    48  }