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

     1  package errors
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func TestNew(t *testing.T) {
    10  	err := New("hello %v", "world")
    11  
    12  	t.Logf("failed: %+v", err)
    13  
    14  	err = Skip(1).
    15  		WithSkip(0).
    16  		WithMessage("bug skip 0").
    17  		Build()
    18  	t.Logf("failed: %+v", err)
    19  
    20  	err = Message("1").
    21  		WithSkip(0).
    22  		WithMessage("bug msg").
    23  		Build()
    24  	t.Logf("failed: %+v", err)
    25  
    26  	err = NewBuilder().
    27  		WithCode(Internal).
    28  		WithErrors(io.EOF).
    29  		WithErrors(io.ErrShortWrite).
    30  		Build()
    31  	t.Logf("failed: %+v", err)
    32  
    33  	err = NewBuilder().
    34  		WithCode(Internal).
    35  		WithErrors(io.EOF).
    36  		WithErrors(io.ErrShortWrite).
    37  		Build()
    38  	// Attach(io.ErrClosedPipe)
    39  	t.Logf("failed: %+v", err)
    40  
    41  	err = NewBuilder().Build()
    42  	err.Attach(os.ErrClosed, os.ErrInvalid, os.ErrPermission)
    43  	t.Logf("failed: %+v", err)
    44  
    45  	err = New(WithErrors(io.EOF, io.ErrShortWrite))
    46  	t.Logf("failed: %+v", err)
    47  
    48  	err = New()
    49  	t.Logf("failed: %+v", err)
    50  
    51  	// since v3.0.5, Attach() has no return value
    52  	// err = New("hello").Attach(io.EOF)
    53  	// t.Logf("failed: %+v", err)
    54  }
    55  
    56  func TestUnwrap(t *testing.T) {
    57  	t.Log("unwrap all inner error (including Code object) one by one:")
    58  	err := New()
    59  	err. // WithCode(NotFound).
    60  		WithErrors(io.EOF, io.ErrShortBuffer).
    61  		WithMessage("has code and errors").
    62  		Attach(os.ErrClosed, os.ErrInvalid, os.ErrPermission)
    63  	var e error = err
    64  	for e != nil {
    65  		e = Unwrap(err)
    66  		t.Logf("failed: %v", e)
    67  	}
    68  	if o, ok := err.(interface{ Reset() }); ok {
    69  		o.Reset()
    70  	}
    71  	t.Log("again")
    72  	e = err
    73  	for e != nil {
    74  		e = Unwrap(err)
    75  		t.Logf("failed: %v", e)
    76  	}
    77  }
    78  
    79  func TestWithStackInfo_New(t *testing.T) {
    80  	err := New("hello %v", "world")
    81  
    82  	err.WithErrors(io.EOF, io.ErrShortWrite).
    83  		WithErrors(io.ErrClosedPipe).
    84  		WithCode(Internal).
    85  		End()
    86  	t.Logf("failed: %+v", err)
    87  
    88  	if CanCause(err) {
    89  		e := err.(causer).Cause()
    90  		t.Logf("failed: %v", e)
    91  	}
    92  	if CanCauses(err) {
    93  		e := Causes(err)
    94  		t.Logf("failed: %v", e)
    95  	}
    96  
    97  	err2 := New("hello %v", "world")
    98  	err2.WithData(9, err, 10).WithTaggedData(TaggedData{"9": 9, "10": 10}).End()
    99  
   100  	t.Logf("failed: %+v", err2)
   101  	t.Logf("Data: %v", err2.Data())
   102  	t.Logf("TaggedData: %v", err2.TaggedData())
   103  }
   104  
   105  func TestTemplateFormat(t *testing.T) {
   106  	err := New("cannot set: %v (%v) -> %v (%v)")
   107  
   108  	_ = err.FormatWith("a", "b", "c", "d")
   109  	t.Logf("Error: %v", err)
   110  	t.Logf("Error: %+v", err)
   111  
   112  	_ = err.FormatWith("1", "2", "3", "4")
   113  	t.Logf("Error: %v", err)
   114  }
   115  
   116  func TestContainerMore(t *testing.T) {
   117  	var err error
   118  	ec := New("copying got errors")
   119  	ec.Attach(New("some error"))
   120  	ec.Defer(&err)
   121  	if err == nil {
   122  		t.Fatal(`bad`)
   123  	} else {
   124  		t.Logf(`wanted err is non-nil: %v`, err)
   125  	}
   126  }
   127  
   128  func TestIsDeep(t *testing.T) {
   129  	var err error
   130  	ec := New("copying got errors")
   131  	in := New("unmatched %q")
   132  	ec.Attach(in.FormatWith("demo"))
   133  	ec.Defer(&err)
   134  	if err == nil {
   135  		t.Fatal(`bad`)
   136  	} else {
   137  		t.Logf(`wanted err is non-nil: %v`, err)
   138  		if !Is(err, in) {
   139  			t.Fatal("expecting Is() got returning true")
   140  		}
   141  	}
   142  }