From 406337bfc7f73c9bf45a76a2637f5380c9016c6c Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Tue, 26 Nov 2024 14:35:56 +0200 Subject: [PATCH 01/10] -rm-try flag --- samples/main.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/samples/main.go b/samples/main.go index db79f4b..9d901e9 100644 --- a/samples/main.go +++ b/samples/main.go @@ -19,16 +19,13 @@ var ( "\nplay-recursion (= runs recursion example)", ) isErr = flag.Bool("err", false, "tells if we want to have an error") + rmTry = flag.Bool("rm-try", false, "remove Try prefix from errors") ) func init() { // highlight that this is before flag.Parse to allow it to work properly. err2.SetLogTracer(os.Stderr) // for import err2.SetLogTracer(nil) - - // select which one you want to play with - err2.SetFormatter(formatter.DecamelAndRmTryPrefix) - // err2.SetFormatter(formatter.Decamel) } func main() { @@ -37,6 +34,12 @@ func main() { flag.Parse() + if *rmTry { + // select which one you want to play with + err2.SetFormatter(formatter.DecamelAndRmTryPrefix) + // err2.SetFormatter(formatter.Decamel) + } + switch *mode { case "db": doDBMain() From f08499c28f98d7f79bb87d13e98ebec3a8f2b5b2 Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Tue, 26 Nov 2024 14:36:28 +0200 Subject: [PATCH 02/10] fix typo except -> expect --- err2_test.go | 92 +++++++++++++-------------- internal/debug/debug_test.go | 36 +++++------ internal/{except => expect}/except.go | 2 +- internal/handler/handler_test.go | 26 ++++---- internal/handler/handlers_test.go | 12 ++-- internal/str/str_test.go | 8 +-- internal/x/x_test.go | 22 +++---- try/copy_test.go | 14 ++-- try/out_test.go | 10 +-- 9 files changed, 111 insertions(+), 111 deletions(-) rename internal/{except => expect}/except.go (98%) diff --git a/err2_test.go b/err2_test.go index 09dc69a..2e8216a 100644 --- a/err2_test.go +++ b/err2_test.go @@ -9,7 +9,7 @@ import ( "testing" "github.com/lainio/err2" - "github.com/lainio/err2/internal/except" + "github.com/lainio/err2/internal/expect" "github.com/lainio/err2/try" ) @@ -70,7 +70,7 @@ func TestHandle_noerrHandler(t *testing.T) { var err error var handlerCalled bool defer func() { - except.That(t, handlerCalled) + expect.That(t, handlerCalled) }() // This is the handler we are testing! defer err2.Handle(&err, func(noerr bool) { @@ -88,12 +88,12 @@ func TestHandle_noerrHandler(t *testing.T) { var err error var handlerCalled bool defer func() { - except.That(t, handlerCalled) + expect.That(t, handlerCalled) }() defer err2.Handle(&err, func(err error) error { // this should not be called, so lets try to fuckup things... handlerCalled = false - except.That(t, false) + expect.That(t, false) return err }) @@ -111,20 +111,20 @@ func TestHandle_noerrHandler(t *testing.T) { var err error var handlerCalled bool defer func() { - except.ThatNot(t, handlerCalled) + expect.ThatNot(t, handlerCalled) }() // This is the handler we are testing! defer err2.Handle(&err, func(err error) error { - except.ThatNot(t, handlerCalled) + expect.ThatNot(t, handlerCalled) handlerCalled = false - except.That(t, true, "error should be handled") + expect.That(t, true, "error should be handled") return err }) // This is the handler we are testing! AND it's not called in error. defer err2.Handle(&err, func(bool) { - except.That(t, false, "when error this is not called") + expect.That(t, false, "when error this is not called") }) try.To1(throw()) @@ -141,14 +141,14 @@ func TestHandle_noerrHandler(t *testing.T) { callCount int ) defer func() { - except.ThatNot(t, handlerCalled) - except.Equal(t, callCount, 2) - except.Equal(t, err.Error(), finalAnnotatedErr.Error()) + expect.ThatNot(t, handlerCalled) + expect.Equal(t, callCount, 2) + expect.Equal(t, err.Error(), finalAnnotatedErr.Error()) }() // This is the handler we are testing! AND it's not called in error. defer err2.Handle(&err, func(noerr bool) { - except.That( + expect.That( t, false, "if error occurs/reset, this cannot happen", @@ -159,18 +159,18 @@ func TestHandle_noerrHandler(t *testing.T) { // important! test that our handler doesn't change the current error // and it's not nil defer err2.Handle(&err, func(er error) error { - except.That(t, er != nil, "er val: ", er, err) - except.Equal(t, callCount, 1, "this is called in sencond") + expect.That(t, er != nil, "er val: ", er, err) + expect.Equal(t, callCount, 1, "this is called in sencond") callCount++ return er }) defer err2.Handle(&err, func(err error) error { // this should not be called, so lets try to fuckup things... - except.Equal(t, callCount, 0, "this is called in first") + expect.Equal(t, callCount, 0, "this is called in first") callCount++ handlerCalled = false - except.That(t, err != nil) + expect.That(t, err != nil) return finalAnnotatedErr }) try.To1(throw()) @@ -182,17 +182,17 @@ func TestHandle_noerrHandler(t *testing.T) { var err error var handlerCalled bool defer func() { - except.That(t, handlerCalled) + expect.That(t, handlerCalled) }() // This is the handler we are testing! defer err2.Handle(&err, func(noerr bool) { - except.That(t, noerr) + expect.That(t, noerr) handlerCalled = noerr }) defer err2.Handle(&err, func(err error) error { - except.That(t, false, "no error to handle!") + expect.That(t, false, "no error to handle!") // this should not be called, so lets try to fuckup things... handlerCalled = false // see first deferred function return err @@ -207,27 +207,27 @@ func TestHandle_noerrHandler(t *testing.T) { var err error var handlerCalled bool defer func() { - except.That(t, handlerCalled) + expect.That(t, handlerCalled) }() // This is the handler we are testing! defer err2.Handle(&err, func(noerr bool) { - except.That(t, true) - except.That(t, noerr) + expect.That(t, true) + expect.That(t, noerr) handlerCalled = noerr }) defer err2.Handle(&err) defer err2.Handle(&err, func(err error) error { - except.That(t, false, "no error to handle!") + expect.That(t, false, "no error to handle!") // this should not be called, so lets try to fuckup things... handlerCalled = false // see first deferred function return err }) defer err2.Handle(&err, func(err error) error { - except.That(t, false, "no error to handle!") + expect.That(t, false, "no error to handle!") // this should not be called, so lets try to fuckup things... handlerCalled = false // see first deferred function return err @@ -243,20 +243,20 @@ func TestHandle_noerrHandler(t *testing.T) { var err error var noerrHandlerCalled, errHandlerCalled bool defer func() { - except.That(t, noerrHandlerCalled) - except.That(t, errHandlerCalled) + expect.That(t, noerrHandlerCalled) + expect.That(t, errHandlerCalled) }() // This is the handler we are testing! defer err2.Handle(&err, func(noerr bool) { - except.That(t, true) // we are here, for debugging - except.That(t, noerr) + expect.That(t, true) // we are here, for debugging + expect.That(t, noerr) noerrHandlerCalled = noerr }) // this is the err handler that -- RESETS -- the error to nil defer err2.Handle(&err, func(err error) error { - except.That(t, err != nil) // helps fast debugging + expect.That(t, err != nil) // helps fast debugging // this should not be called, so lets try to fuckup things... noerrHandlerCalled = false // see first deferred function @@ -266,7 +266,7 @@ func TestHandle_noerrHandler(t *testing.T) { }) defer err2.Handle(&err, func(err error) error { - except.That(t, err != nil) // helps fast debugging + expect.That(t, err != nil) // helps fast debugging // this should not be called, so lets try to fuckup things... noerrHandlerCalled = false // see first deferred function @@ -284,14 +284,14 @@ func TestHandle_noerrHandler(t *testing.T) { var err error var handlerCalled bool defer func() { - except.That(t, handlerCalled) + expect.That(t, handlerCalled) }() defer err2.Handle(&err) defer err2.Handle(&err) defer err2.Handle(&err, func(err error) error { - except.That(t, false, "no error to handle!") + expect.That(t, false, "no error to handle!") // this should not be called, so lets try to fuckup things... handlerCalled = false // see first deferred function return err @@ -299,13 +299,13 @@ func TestHandle_noerrHandler(t *testing.T) { // This is the handler we are testing! defer err2.Handle(&err, func(noerr bool) { - except.That(t, true, "this must be called") - except.That(t, noerr) + expect.That(t, true, "this must be called") + expect.That(t, noerr) handlerCalled = noerr }) defer err2.Handle(&err, func(err error) error { - except.That(t, false, "no error to handle!") + expect.That(t, false, "no error to handle!") // this should not be called, so lets try to fuckup things... handlerCalled = false // see first deferred function return err @@ -376,7 +376,7 @@ func TestPanickingCatchAll(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() defer func() { - except.That( + expect.That( t, recover() == nil, "panics should NOT carry on", @@ -424,7 +424,7 @@ func TestPanickingCarryOn_Handle(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() defer func() { - except.That( + expect.That( t, recover() != nil, "panics should went thru when not our errors", @@ -554,12 +554,12 @@ func TestPanicking_Handle(t *testing.T) { defer func() { r := recover() if tt.wants == nil { - except.That(t, r != nil, "wants err, then panic") + expect.That(t, r != nil, "wants err, then panic") } }() err := tt.args.f() if err != nil { - except.Equal(t, err.Error(), tt.wants.Error()) + expect.Equal(t, err.Error(), tt.wants.Error()) } }) } @@ -600,7 +600,7 @@ func TestPanicking_Catch(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() defer func() { - except.That( + expect.That( t, recover() == nil, "panics should NOT carry on", @@ -623,7 +623,7 @@ func TestCatch_Error(t *testing.T) { func Test_TryOutError(t *testing.T) { t.Parallel() defer err2.Catch(func(err error) error { - except.Equal(t, err.Error(), "fails: test: this is an ERROR", + expect.Equal(t, err.Error(), "fails: test: this is an ERROR", "=> we should catch right error str here") return err }) @@ -633,7 +633,7 @@ func Test_TryOutError(t *testing.T) { // let's test try.Out1() and it's throw capabilities here, even try.To1() // is the preferred way. retVal = try.Out1(noThrow()).Handle().Val1 - except.Equal(t, retVal, "test", "if no error happens, we get value") + expect.Equal(t, retVal, "test", "if no error happens, we get value") _ = try.Out1(throw()).Handle("fails: %v", retVal).Val1 t.Fail() // If everything works in Handle we are never here. @@ -665,21 +665,21 @@ func TestCatch_Panic(t *testing.T) { func TestSetErrorTracer(t *testing.T) { t.Parallel() w := err2.ErrorTracer() - except.That(t, w == nil, "error tracer should be nil") + expect.That(t, w == nil, "error tracer should be nil") var w1 io.Writer err2.SetErrorTracer(w1) w = err2.ErrorTracer() - except.That(t, w == nil, "error tracer should be nil") + expect.That(t, w == nil, "error tracer should be nil") } func TestSetErrRetTracer(t *testing.T) { t.Parallel() w := err2.ErrRetTracer() - except.That(t, w == nil, "error return tracer should be nil") + expect.That(t, w == nil, "error return tracer should be nil") var w1 io.Writer err2.SetErrRetTracer(w1) w = err2.ErrRetTracer() - except.That(t, w == nil, "error return tracer should be nil") + expect.That(t, w == nil, "error return tracer should be nil") } func ExampleCatch_withFmt() { diff --git a/internal/debug/debug_test.go b/internal/debug/debug_test.go index 501458e..9850c6e 100644 --- a/internal/debug/debug_test.go +++ b/internal/debug/debug_test.go @@ -6,7 +6,7 @@ import ( "strings" "testing" - "github.com/lainio/err2/internal/except" + "github.com/lainio/err2/internal/expect" ) func TestFullName(t *testing.T) { @@ -42,7 +42,7 @@ func TestFullName(t *testing.T) { tt := ttv t.Run(tt.name, func(t *testing.T) { t.Parallel() - except.Equal(t, tt.retval, tt.fullName()) + expect.Equal(t, tt.retval, tt.fullName()) }) } } @@ -94,7 +94,7 @@ func TestIsAnchor(t *testing.T) { tt := ttv t.Run(tt.name, func(t *testing.T) { t.Parallel() - except.Equal(t, tt.retval, tt.isAnchor(tt.input)) + expect.Equal(t, tt.retval, tt.isAnchor(tt.input)) }) } } @@ -140,7 +140,7 @@ func TestIsFuncAnchor(t *testing.T) { tt := ttv t.Run(tt.name, func(t *testing.T) { t.Parallel() - except.Equal(t, tt.retval, tt.isFuncAnchor(tt.input)) + expect.Equal(t, tt.retval, tt.isFuncAnchor(tt.input)) }) } } @@ -161,7 +161,7 @@ func TestFnLNro(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() output := fnLNro(tt.input) - except.Equal(t, output, tt.output) + expect.Equal(t, output, tt.output) }) } } @@ -212,7 +212,7 @@ func TestFnName(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() output := fnName(tt.input) - except.Equal(t, output, tt.output) + expect.Equal(t, output, tt.output) }) } } @@ -239,7 +239,7 @@ func TestStackPrint_noLimits(t *testing.T) { FuncName: "", Level: 0, }) - except.Equal(t, tt.input, w.String()) + expect.Equal(t, tt.input, w.String()) }) } } @@ -267,8 +267,8 @@ func TestStackPrintForTest(t *testing.T) { // print(tt.output) // println("------") // print(w.String()) - except.Equal(t, a, b) - except.Equal(t, tt.output, w.String()) + expect.Equal(t, a, b) + expect.Equal(t, tt.output, w.String()) }) } } @@ -346,7 +346,7 @@ func TestCalcAnchor(t *testing.T) { t.Parallel() r := strings.NewReader(tt.input) anchor := calcAnchor(r, tt.StackInfo) - except.Equal(t, tt.anchor, anchor) + expect.Equal(t, tt.anchor, anchor) }) } } @@ -385,10 +385,10 @@ func TestStackPrint_limit(t *testing.T) { stackPrint(readStack, writeStack, tt.StackInfo) ins := strings.Split(tt.input, "\n") outs := strings.Split(writeStack.String(), "\n") - except.Thatf(t, len(ins) > len(outs), + expect.Thatf(t, len(ins) > len(outs), "input length:%d should be greater:%d", len(ins), len(outs)) wantResult, gotResult := tt.output, writeStack.String() - except.Equal(t, gotResult, wantResult) + expect.Equal(t, gotResult, wantResult) }) } } @@ -460,10 +460,10 @@ func TestStackPrint_OneFunction(t *testing.T) { stackPrint(r, w, tt.StackInfo) ins := strings.Split(tt.input, "\n") outs := strings.Split(w.String(), "\n") - except.Thatf(t, len(ins) > len(outs), + expect.Thatf(t, len(ins) > len(outs), "input length:%d should be greater:%d", len(ins), len(outs)) b, a := tt.output, w.String() - except.Equal(t, a, b) + expect.Equal(t, a, b) }) } } @@ -528,10 +528,10 @@ func TestFuncName(t *testing.T) { FuncName: tt.FuncName, Level: tt.Level, }) - except.That(t, found) - except.Equal(t, tt.output, name) - except.Equal(t, ln, tt.outln) - except.Equal(t, fr, tt.outFrame) + expect.That(t, found) + expect.Equal(t, tt.output, name) + expect.Equal(t, ln, tt.outln) + expect.Equal(t, fr, tt.outFrame) }) } } diff --git a/internal/except/except.go b/internal/expect/except.go similarity index 98% rename from internal/except/except.go rename to internal/expect/except.go index 932099f..f612f2d 100644 --- a/internal/except/except.go +++ b/internal/expect/except.go @@ -1,4 +1,4 @@ -package except +package expect import ( "fmt" diff --git a/internal/handler/handler_test.go b/internal/handler/handler_test.go index 0e8476b..1755a0d 100644 --- a/internal/handler/handler_test.go +++ b/internal/handler/handler_test.go @@ -6,7 +6,7 @@ import ( "fmt" "testing" - "github.com/lainio/err2/internal/except" + "github.com/lainio/err2/internal/expect" "github.com/lainio/err2/internal/handler" "github.com/lainio/err2/internal/x" ) @@ -121,11 +121,11 @@ func TestProcess(t *testing.T) { if handler.WorkToDo(tt.args.Any, tt.args.Err) { handler.Process(&tt.args.Info) - except.Equal(t, panicHandlerCalled, tt.want.panicCalled) - except.Equal(t, errorHandlerCalled, tt.want.errorCalled) - except.Equal(t, nilHandlerCalled, tt.want.nilCalled) + expect.Equal(t, panicHandlerCalled, tt.want.panicCalled) + expect.Equal(t, errorHandlerCalled, tt.want.errorCalled) + expect.Equal(t, nilHandlerCalled, tt.want.nilCalled) - except.Equal(t, myErrVal.Error(), tt.want.errStr) + expect.Equal(t, myErrVal.Error(), tt.want.errStr) } resetCalled() }) @@ -152,13 +152,13 @@ func TestPreProcess_debug(t *testing.T) { // and that's what error stack tracing is all about Handle() - except.ThatNot(t, panicHandlerCalled) - except.ThatNot(t, errorHandlerCalled) - except.ThatNot(t, nilHandlerCalled) + expect.ThatNot(t, panicHandlerCalled) + expect.ThatNot(t, errorHandlerCalled) + expect.ThatNot(t, nilHandlerCalled) // See the name of this test function. Decamel it + error const want = "testing: t runner: error" - except.Equal(t, myErrVal.Error(), want) + expect.Equal(t, myErrVal.Error(), want) resetCalled() } @@ -243,11 +243,11 @@ func TestPreProcess(t *testing.T) { err = handler.PreProcess(&err, &tt.args.Info, tt.args.a) - except.Equal(t, panicHandlerCalled, tt.want.panicCalled) - except.Equal(t, errorHandlerCalled, tt.want.errorCalled) - except.Equal(t, nilHandlerCalled, tt.want.nilCalled) + expect.Equal(t, panicHandlerCalled, tt.want.panicCalled) + expect.Equal(t, errorHandlerCalled, tt.want.errorCalled) + expect.Equal(t, nilHandlerCalled, tt.want.nilCalled) - except.Equal(t, err.Error(), tt.want.errStr) + expect.Equal(t, err.Error(), tt.want.errStr) } resetCalled() }) diff --git a/internal/handler/handlers_test.go b/internal/handler/handlers_test.go index a98586c..f96b156 100644 --- a/internal/handler/handlers_test.go +++ b/internal/handler/handlers_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/lainio/err2" - "github.com/lainio/err2/internal/except" + "github.com/lainio/err2/internal/expect" "github.com/lainio/err2/internal/handler" ) @@ -93,17 +93,17 @@ func TestHandlers(t *testing.T) { t.Parallel() anys := tt.args.f - except.That(t, anys != nil, "cannot be nil") + expect.That(t, anys != nil, "cannot be nil") fns, dis := handler.ToErrorFns(anys) - except.That(t, fns != nil, "cannot be nil") - except.Equal(t, dis, tt.dis, "disabled wanted") + expect.That(t, fns != nil, "cannot be nil") + expect.Equal(t, dis, tt.dis, "disabled wanted") errHandler := handler.Pipeline(fns) err := errHandler(err2.ErrNotFound) if err == nil { - except.That(t, tt.want == nil) + expect.That(t, tt.want == nil) } else { - except.Equal(t, err.Error(), tt.want.Error()) + expect.Equal(t, err.Error(), tt.want.Error()) } }) } diff --git a/internal/str/str_test.go b/internal/str/str_test.go index c013d0b..3b0bfa3 100644 --- a/internal/str/str_test.go +++ b/internal/str/str_test.go @@ -3,7 +3,7 @@ package str_test import ( "testing" - "github.com/lainio/err2/internal/except" + "github.com/lainio/err2/internal/expect" "github.com/lainio/err2/internal/str" ) @@ -49,7 +49,7 @@ func TestCamel(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() got := str.DecamelRegexp(tt.args.s) - except.Equal(t, got, tt.want) + expect.Equal(t, got, tt.want) }) } } @@ -106,7 +106,7 @@ func TestDecamel(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() got := str.Decamel(tt.args.s) - except.Equal(t, got, tt.want) + expect.Equal(t, got, tt.want) }) } } @@ -182,7 +182,7 @@ func TestDecamelRmTryPrefix(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() got := str.DecamelRmTryPrefix(tt.args.s) - except.Equal(t, got, tt.want) + expect.Equal(t, got, tt.want) }) } } diff --git a/internal/x/x_test.go b/internal/x/x_test.go index b18fc78..e7ce718 100644 --- a/internal/x/x_test.go +++ b/internal/x/x_test.go @@ -4,7 +4,7 @@ import ( "reflect" "testing" - "github.com/lainio/err2/internal/except" + "github.com/lainio/err2/internal/expect" ) var ( @@ -52,30 +52,30 @@ func TestSwap(t *testing.T) { var ( lhs, rhs = 1, 2 // these are ints as default ) - except.Equal(t, lhs, 1) - except.Equal(t, rhs, 2) + expect.Equal(t, lhs, 1) + expect.Equal(t, rhs, 2) Swap(&lhs, &rhs) - except.Equal(t, lhs, 2) - except.Equal(t, rhs, 1) + expect.Equal(t, lhs, 2) + expect.Equal(t, rhs, 1) } { var ( lhs, rhs float64 = 1, 2 ) - except.Equal(t, lhs, 1) - except.Equal(t, rhs, 2) + expect.Equal(t, lhs, 1) + expect.Equal(t, rhs, 2) Swap(&lhs, &rhs) - except.Equal(t, lhs, 2) - except.Equal(t, rhs, 1) + expect.Equal(t, lhs, 2) + expect.Equal(t, rhs, 1) } } func TestSReverse(t *testing.T) { t.Parallel() SReverse(lengths) - except.That(t, reflect.DeepEqual(lengths, reverseLengths)) + expect.That(t, reflect.DeepEqual(lengths, reverseLengths)) SReverse(lengths) // it's reverse now turn it to original - except.That(t, reflect.DeepEqual(lengths, original)) + expect.That(t, reflect.DeepEqual(lengths, original)) } func BenchmarkSSReverse(b *testing.B) { diff --git a/try/copy_test.go b/try/copy_test.go index 1498538..5c7300e 100644 --- a/try/copy_test.go +++ b/try/copy_test.go @@ -8,7 +8,7 @@ import ( "os" "testing" - "github.com/lainio/err2/internal/except" + "github.com/lainio/err2/internal/expect" "github.com/lainio/err2/try" ) @@ -16,8 +16,8 @@ const dataFile = "./try.go" func Benchmark_CopyBufferMy(b *testing.B) { all, err := os.ReadFile(dataFile) - except.Thatf(b, err == nil, "error: %v", err) - except.That(b, all != nil) + expect.Thatf(b, err == nil, "error: %v", err) + expect.That(b, all != nil) buf := make([]byte, 4) dst := bufio.NewWriter(bytes.NewBuffer(make([]byte, 0, len(all)))) @@ -29,8 +29,8 @@ func Benchmark_CopyBufferMy(b *testing.B) { func Benchmark_CopyBufferStd(b *testing.B) { all, err := os.ReadFile(dataFile) - except.Thatf(b, err == nil, "error: %v", err) - except.That(b, all != nil) + expect.Thatf(b, err == nil, "error: %v", err) + expect.That(b, all != nil) buf := make([]byte, 4) dst := bufio.NewWriter(bytes.NewBuffer(make([]byte, 0, len(all)))) @@ -42,8 +42,8 @@ func Benchmark_CopyBufferStd(b *testing.B) { func Benchmark_CopyBufferOur(b *testing.B) { all, err := os.ReadFile(dataFile) - except.Thatf(b, err == nil, "error: %v", err) - except.That(b, all != nil) + expect.Thatf(b, err == nil, "error: %v", err) + expect.That(b, all != nil) tmp := make([]byte, 4) dst := bufio.NewWriter(bytes.NewBuffer(make([]byte, 0, len(all)))) diff --git a/try/out_test.go b/try/out_test.go index 1ce0946..c6b4979 100644 --- a/try/out_test.go +++ b/try/out_test.go @@ -11,7 +11,7 @@ import ( "testing" "github.com/lainio/err2" - "github.com/lainio/err2/internal/except" + "github.com/lainio/err2/internal/expect" "github.com/lainio/err2/try" ) @@ -102,8 +102,8 @@ func TestResult2_Logf(t *testing.T) { return v1 + v2, v2 } num1, num2 := countSomething("1", "bad") - except.Equal(t, num2, 2) - except.Equal(t, num1, 3) + expect.Equal(t, num2, 2) + expect.Equal(t, num1, 3) } func TestResult_Handle(t *testing.T) { @@ -121,10 +121,10 @@ func TestResult_Handle(t *testing.T) { return nil } err := callFn(1) - except.That(t, err == nil, "no error when Out.Handle sets it nil") + expect.That(t, err == nil, "no error when Out.Handle sets it nil") err = callFn(0) - except.That(t, err != nil, "want error when Out.Handle sets it the same") + expect.That(t, err != nil, "want error when Out.Handle sets it the same") } func ExampleResult1_Handle() { From 8c85e3591c7a26e517e56e4c4e36f9220cb00c19 Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Fri, 29 Nov 2024 12:38:43 +0200 Subject: [PATCH 03/10] fix urls --- tracer.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tracer.go b/tracer.go index f5a96f3..7a052fd 100644 --- a/tracer.go +++ b/tracer.go @@ -31,9 +31,9 @@ func PanicTracer() io.Writer { return tracer.Panic.Tracer() } -// LogTracer returns a current [io.Writer] for the explicit [try.Result.Logf] -// function and automatic logging used in [Handle] and [Catch]. The -// default value is nil. +// LogTracer returns a current [io.Writer] for the explicit +// [github.com/lainio/err2/try.Result.Logf] function and automatic logging used +// in [Handle] and [Catch]. The default value is nil. func LogTracer() io.Writer { return tracer.Log.Tracer() } @@ -49,7 +49,7 @@ func LogTracer() io.Writer { // have multiple sections because every [Handle] and [Catch] prints it. If an // error happens in a deep call stack, the error trace includes various parts. // If you prefer similar to [Zig Error Return Traces], where you see how -// error bubbles up, you should use [SetErrRetTrace]. +// error bubbles up, you should use [SetErrRetTracer]. // // Remember that you can reset these with [flag] package support. See // documentation of err2 package's flag section. @@ -93,13 +93,14 @@ func SetPanicTracer(w io.Writer) { tracer.Panic.SetTracer(w) } -// SetLogTracer sets a current [io.Writer] for the explicit [try.Result.Logf] -// function and automatic logging used in [Handle] and [Catch]. The -// default is nil and then err2 uses std log package for logging. +// SetLogTracer sets a current [io.Writer] for the explicit +// [github.com/lainio/err2/try.Result.Logf] function and automatic logging used +// in [Handle] and [Catch]. The default is nil and then err2 uses std log +// package for logging. // -// You can use the std log package to redirect other logging packages like [glog] -// to automatically work with the err2 package. For the [glog], add this line at -// the beginning of your app: +// You can use the std log package to redirect other logging packages like +// [github.com/golang/glog] to automatically work with the err2 package. For the +// [github.com/golang/glog], add this line at the beginning of your app: // // glog.CopyStandardLogTo("INFO") // From f8a4a7f1eff051628663c82b56ece52b6a4bf058 Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Fri, 29 Nov 2024 19:06:05 +0200 Subject: [PATCH 04/10] adding err return trace stuff --- doc.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/doc.go b/doc.go index 9119fdd..1d28ec2 100644 --- a/doc.go +++ b/doc.go @@ -71,7 +71,9 @@ err2 offers optional stack tracing. And yes, it's fully automatic. Just call at the beginning your app, e.g. main function, or set the tracers programmatically (before [flag.Parse] if you are using that): - err2.SetErrorTracer(os.Stderr) // write error stack trace to stderr + err2.SetErrRmTracer(os.Stderr) // write error return trace to stderr + or + err2.SetErrorTracer(os.Stderr) // write error stack trace to stderr or err2.SetPanicTracer(log.Writer()) // panic stack trace to std logger @@ -80,7 +82,8 @@ practice still print their stack trace. The panic tracer's default values is [os.Stderr]. The default error tracer is nil. err2.SetPanicTracer(os.Stderr) // panic stack tracer's default is stderr - err2.SetErrorTracer(nil) // error stack tracer's default is nil + err2.SetErrRetTracer(nil) // error return tracer's default is nil + err2.SetErrorTracer(nil) // error stack tracer's default is nil Note that both panic and error traces are optimized by err2 package. That means that the head of the stack trace isn't the panic function, but an actual line @@ -109,6 +112,8 @@ And the following flags are supported (="default-value"): A name of the stream currently supported stderr, stdout or nil -err2-panic-trace="stderr" A name of the stream currently supported stderr, stdout or nil + -err2-ret-trace stream + stream for error return tracing: stderr, stdout -err2-trace="nil" A name of the stream currently supported stderr, stdout or nil From d46afd825c7c9a41791652d6ddefd6de0706da8c Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Fri, 29 Nov 2024 19:19:19 +0200 Subject: [PATCH 05/10] flag usage the latest --- doc.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/doc.go b/doc.go index 1d28ec2..3ab9540 100644 --- a/doc.go +++ b/doc.go @@ -108,14 +108,14 @@ tracer API: The err2 package supports Go's flags. All you need to do is to call [flag.Parse]. And the following flags are supported (="default-value"): - -err2-log="nil" - A name of the stream currently supported stderr, stdout or nil - -err2-panic-trace="stderr" - A name of the stream currently supported stderr, stdout or nil + -err2-log stream + stream for logging: nil -> log pkg + -err2-panic-trace stream + stream for panic tracing (default stderr) -err2-ret-trace stream - stream for error return tracing: stderr, stdout - -err2-trace="nil" - A name of the stream currently supported stderr, stdout or nil + stream for error return tracing: stderr, stdout + -err2-trace stream + stream for error tracing: stderr, stdout Note that you have called [SetErrorTracer] and others, before you call [flag.Parse]. This allows you set the defaults according your app's need and allow From f9d9b30ff2d973f23437d7150a3f50f2e40b95c6 Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Tue, 3 Dec 2024 19:00:43 +0200 Subject: [PATCH 06/10] refactor&optimization for PushAsserter --- assert/assert.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/assert/assert.go b/assert/assert.go index 13b78f1..1df956b 100644 --- a/assert/assert.go +++ b/assert/assert.go @@ -1062,6 +1062,7 @@ func PushAsserter(i Asserter) (retFn function) { var ( prevFound bool prevAsserter asserter + currentGID int ) // get pkg lvl asserter @@ -1069,20 +1070,15 @@ func PushAsserter(i Asserter) (retFn function) { // .. to check if we are doing unit tests if !curAsserter.isUnitTesting() { // .. allow GLS specific asserter. NOTE see current() - curGoRID := goid() - //asserterMap.Set(curGoRID, defAsserter[i]) + currentGID = goid() asserterMap.Tx(func(m map[int]asserter) { - cur, found := m[curGoRID] - if found { - prevAsserter = cur - prevFound = found - } - m[curGoRID] = defAsserter[i] + prevAsserter, prevFound = m[currentGID] + m[currentGID] = defAsserter[i] }) } if prevFound { return func() { - asserterMap.Set(goid(), prevAsserter) + asserterMap.Set(currentGID, prevAsserter) } } return PopAsserter From 4b04fe0ae4c05d84b677ae2a3096fd4e3aa30727 Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Tue, 17 Dec 2024 19:50:10 +0200 Subject: [PATCH 07/10] linter cfg, rm waringns --- .golangci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 5bebe3a..4d3ce52 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -62,7 +62,7 @@ linters: # - errorlint # - exhaustive # - exhaustivestruct - - exportloopref + # - exportloopref -> copyloopvar # but we are using go 1.18 # - forbidigo # - forcetypeassert - funlen @@ -92,6 +92,7 @@ linters: - ineffassign # - ireturn # - lll + # - copyloopvar # we have go 1.18 - makezero - misspell - nakedret From b7485d05a5b8376147d3ca0c460ad4b5a74d0c10 Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Mon, 3 Feb 2025 20:53:39 +0200 Subject: [PATCH 08/10] version change --- CHANGELOG.md | 8 ++++++++ README.md | 10 +++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92e71e3..4d7b99c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ### Version history +##### 1.2.0 +- Now `-err2-ret-trace` and `err2.SetErrRetTracer` gives us *error return traces* + which are even more readable than `-err2-trace`, `err2.SetErrorTracer` with + long error return traces +- A new automatic error formatter/generator added for `TryCopyFile` convention +- New features for `sample/` to demonstrate latest features +- Extended documentation + ##### 1.1.0 - `assert` package: - bug fix: call stack traversal during unit testing in some situations diff --git a/README.md b/README.md index 628b141..5ca3d97 100644 --- a/README.md +++ b/README.md @@ -623,10 +623,6 @@ Please see the full version history from [CHANGELOG](./CHANGELOG.md). ### Latest Release -##### 1.2.0 -- Now `-err2-ret-trace` and `err2.SetErrRetTracer` gives us *error return traces* - which are even more readable than `-err2-trace`, `err2.SetErrorTracer` with - long error return traces -- A new automatic error formatter/generator added for `TryCopyFile` convention -- New features for `sample/` to demonstrate latest features -- Extended documentation +##### 1.2.1 +- Refactoring & optimization +- Updated documentation From 71d1ba3b3e9625ebb06b7d302447c7ed19f5ff94 Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Fri, 7 Feb 2025 13:02:24 +0200 Subject: [PATCH 09/10] typo --- doc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc.go b/doc.go index 3ab9540..f696b4d 100644 --- a/doc.go +++ b/doc.go @@ -71,7 +71,7 @@ err2 offers optional stack tracing. And yes, it's fully automatic. Just call at the beginning your app, e.g. main function, or set the tracers programmatically (before [flag.Parse] if you are using that): - err2.SetErrRmTracer(os.Stderr) // write error return trace to stderr + err2.SetErrRetTracer(os.Stderr) // write error return trace to stderr or err2.SetErrorTracer(os.Stderr) // write error stack trace to stderr or From faeb1926be543eebb3b7ac672d308b5cb7fe0829 Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Fri, 7 Feb 2025 13:29:18 +0200 Subject: [PATCH 10/10] release note --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5ca3d97..672b9b7 100644 --- a/README.md +++ b/README.md @@ -624,5 +624,5 @@ Please see the full version history from [CHANGELOG](./CHANGELOG.md). ### Latest Release ##### 1.2.1 -- Refactoring & optimization +- Optimization and Refactoring - Updated documentation