This commit is contained in:
daniel.w 2024-08-27 21:53:58 +08:00
parent 09cb6fd077
commit 345e6d2454
8 changed files with 55 additions and 10 deletions

View File

@ -1,3 +1,6 @@
GOFMT ?= gofmt
GOFILES := $(shell find . -name "*.go")
.PHONY: test
test: # 進行測試
go test -v --cover ./...

View File

@ -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
}
})
}
}

View File

@ -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 函數

View File

@ -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 (

View File

@ -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) {

View File

@ -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 全域變數應由服務或模組設置

View File

@ -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) {

View File

@ -5,4 +5,5 @@ use (
./validator
./worker_pool
./jwt
./errs
)