diff --git a/Makefile b/Makefile index f3676cd..238a31a 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +GOFMT ?= gofmt +GOFILES := $(shell find . -name "*.go") + .PHONY: test test: # 進行測試 go test -v --cover ./... diff --git a/errors/easy_func_test.go b/errors/easy_func_test.go index a3ef45c..57a03eb 100644 --- a/errors/easy_func_test.go +++ b/errors/easy_func_test.go @@ -1030,3 +1030,40 @@ func TestFromError(t *testing.T) { }) } } + +func Test_newErr(t *testing.T) { + type args struct { + scope uint32 + detail uint32 + msg string + } + tests := []struct { + name string + args args + }{ + { + name: "ok", + args: args{ + scope: code.CloudEPMember, + detail: code.InvalidFormat, + msg: "gg88g88", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + er := newErr(tt.args.scope, tt.args.detail, tt.args.msg) + // er.CodeStr() 會補滿6碼,業務邏輯要回應這個 + // 105,060 前面兩位會乘一萬做計算,中間兩位乘100 來做計算最後用補的 + fmt.Println(er.Scope(), er.Category(), er.Code(), er.FullCode(), er.CodeStr()) + fmt.Println(er.Error()) // 建立十原始錯誤 -> 業務邏輯,給客人看 gg88g88 + er2 := fmt.Errorf("test origin err") + er = er.Wrap(er2) // 包裝錯誤 + fmt.Println(er.Error()) // gg88g88: test origin err + err := er.Unwrap() + if err != nil { + return + } + }) + } +} diff --git a/errors/errors.go b/errors/errors.go index dec0bf9..dafb78b 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -77,10 +77,10 @@ func (e *LibError) CodeStr() string { } if e.Category() == code2.CatGRPC { - return fmt.Sprintf("%d%04d", e.Scope(), e.Category()+e.Code()) + return fmt.Sprintf("%02d%04d", e.Scope(), e.Category()+e.Code()) } - return fmt.Sprintf("%d%04d", e.Scope(), e.Code()) + return fmt.Sprintf("%02d%04d", e.Scope(), e.Code()) } // Code 私有屬性 "code" 的 getter 函數 diff --git a/errs/easy_func.go b/errs/easy_func.go index ddf1782..8f22ef1 100644 --- a/errs/easy_func.go +++ b/errs/easy_func.go @@ -1,12 +1,13 @@ package errs import ( - "code.30cm.net/digimon/library-go/errs/code" "errors" "fmt" + "strings" + + "code.30cm.net/digimon/library-go/errs/code" "github.com/zeromicro/go-zero/core/logx" "google.golang.org/grpc/status" - "strings" ) const ( diff --git a/errs/easy_func_test.go b/errs/easy_func_test.go index 8ee416a..a07633c 100644 --- a/errs/easy_func_test.go +++ b/errs/easy_func_test.go @@ -1,10 +1,11 @@ package errs import ( - "code.30cm.net/digimon/library-go/errs/code" "context" - "github.com/zeromicro/go-zero/core/logx" "testing" + + "code.30cm.net/digimon/library-go/errs/code" + "github.com/zeromicro/go-zero/core/logx" ) func TestFromError(t *testing.T) { diff --git a/errs/errors.go b/errs/errors.go index 6d9a1f6..68b2f9d 100644 --- a/errs/errors.go +++ b/errs/errors.go @@ -1,12 +1,13 @@ package errs import ( - "code.30cm.net/digimon/library-go/errs/code" "errors" "fmt" + "net/http" + + "code.30cm.net/digimon/library-go/errs/code" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "net/http" ) // Scope 全域變數應由服務或模組設置 diff --git a/errs/errors_test.go b/errs/errors_test.go index 3e72bc6..8859f45 100644 --- a/errs/errors_test.go +++ b/errs/errors_test.go @@ -1,11 +1,12 @@ package errs import ( - "code.30cm.net/digimon/library-go/errs/code" "errors" - "google.golang.org/grpc/codes" "net/http" "testing" + + "code.30cm.net/digimon/library-go/errs/code" + "google.golang.org/grpc/codes" ) func TestNewError(t *testing.T) { diff --git a/go.work b/go.work index 90a2422..fc827c6 100644 --- a/go.work +++ b/go.work @@ -5,4 +5,5 @@ use ( ./validator ./worker_pool ./jwt + ./errs )