From 848dfcd90acb90ac147ea826e7cccad9247f4b49 Mon Sep 17 00:00:00 2001 From: GreatDiscovery <1303535630@qq.com> Date: Thu, 19 Sep 2024 20:27:32 +0800 Subject: [PATCH] add: error with stack trace --- go.mod | 4 ++-- test/basic/error_stack_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 test/basic/error_stack_test.go diff --git a/go.mod b/go.mod index 0d8ed0f..dc6493a 100644 --- a/go.mod +++ b/go.mod @@ -14,11 +14,13 @@ require ( github.com/go-ini/ini v1.67.0 github.com/go-playground/assert/v2 v2.2.0 github.com/gofrs/uuid v4.2.0+incompatible + github.com/golang/mock v1.6.0 github.com/google/uuid v1.3.0 github.com/joaojeronimo/go-crc16 v0.0.0-20140729130949-59bd0194935e github.com/json-iterator/go v1.1.12 github.com/panjf2000/ants/v2 v2.8.1 github.com/patrickmn/go-cache v2.1.0+incompatible + github.com/prashantv/gostub v1.1.0 github.com/prometheus/client_golang v1.16.0 github.com/prometheus/common v0.44.0 github.com/redis/go-redis/v9 v9.2.1 @@ -98,7 +100,6 @@ require ( github.com/gobwas/glob v0.2.3 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/gogo/protobuf v1.3.2 // indirect - github.com/golang/mock v1.6.0 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/btree v1.0.1 // indirect github.com/google/gnostic-models v0.6.8 // indirect @@ -149,7 +150,6 @@ require ( github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prashantv/gostub v1.1.0 // indirect github.com/prometheus/client_model v0.4.0 // indirect github.com/prometheus/procfs v0.10.1 // indirect github.com/rubenv/sql-migrate v1.5.2 // indirect diff --git a/test/basic/error_stack_test.go b/test/basic/error_stack_test.go new file mode 100644 index 0000000..210cf22 --- /dev/null +++ b/test/basic/error_stack_test.go @@ -0,0 +1,30 @@ +package main + +import ( + "errors" + "fmt" + pkg_errors "github.com/pkg/errors" + "testing" +) + +func TestErrorStack(t *testing.T) { + err := causeStackError() + if err != nil { + fmt.Printf("%+v\n", err) // %+v 格式化会打印堆栈信息 + } + + fmt.Println("-------------------------------") + + err = causeNormalError() + if err != nil { + fmt.Printf("%+v\n", err) + } +} + +func causeStackError() error { + return pkg_errors.New("error with stack trace") +} + +func causeNormalError() error { + return errors.New("normal error") +}