소스 검색

完成封装

yjp 3 년 전
부모
커밋
44a7a48aaf
5개의 변경된 파일135개의 추가작업 그리고 0개의 파일을 삭제
  1. 18 0
      .gitignore
  2. 71 0
      errors.go
  3. 39 0
      errors_test.go
  4. 5 0
      go.mod
  5. 2 0
      go.sum

+ 18 - 0
.gitignore

@@ -0,0 +1,18 @@
+# Binaries for programs and plugins
+*.exe
+*.exe~
+*.dll
+*.so
+*.dylib
+
+# Test binary, build with `go test -c`
+*.test
+
+# Output of the go coverage tool, specifically when used with LiteIDE
+*.out
+
+.idea/
+file/
+**/bin
+
+/vendor/

+ 71 - 0
errors.go

@@ -0,0 +1,71 @@
+package fserr
+
+import (
+	"fmt"
+	"github.com/pkg/errors"
+)
+
+const (
+	ErrUnknownCode      uint32 = 0x00000001
+	ErrParamCode        uint32 = 0x00000002
+	ErrConflictCode     uint32 = 0x00000003
+	ErrNotExistCode     uint32 = 0x00000004
+	ErrTypeTransferCode uint32 = 0x00000005
+	ErrAuthCode         uint32 = 0x00000006
+	ErrCustomCode       uint32 = 0x00001000
+)
+
+var codeMap = map[uint32]string{
+	ErrUnknownCode:      "未知错误",
+	ErrParamCode:        "参数错误",
+	ErrConflictCode:     "资源冲突",
+	ErrNotExistCode:     "资源不存在",
+	ErrTypeTransferCode: "类型转换错误",
+	ErrAuthCode:         "鉴权错误",
+	ErrCustomCode:       "自定义错误",
+}
+
+func NewUnknownError(message string) error {
+	return newError(ErrUnknownCode, message)
+}
+
+func NewParamError(message string) error {
+	return newError(ErrParamCode, message)
+}
+
+func NewConflictError(message string) error {
+	return newError(ErrConflictCode, message)
+}
+
+func NewNotExistError(message string) error {
+	return newError(ErrNotExistCode, message)
+}
+
+func NewTypeTransferError(message string) error {
+	return newError(ErrTypeTransferCode, message)
+}
+
+func NewAuthError(message string) error {
+	return newError(ErrAuthCode, message)
+}
+
+func NewCustomError(message string) error {
+	return newError(ErrCustomCode, message)
+}
+
+type Error struct {
+	code uint32
+	error
+}
+
+func (err Error) Error() string {
+	errMsg := fmt.Sprintf("错误代码: 0x%08x\n代码信息: %s\n%+v\n", err.code, codeMap[err.code], err.error)
+	return errMsg
+}
+
+func newError(code uint32, message string) *Error {
+	return &Error{
+		code:  code,
+		error: errors.New(message),
+	}
+}

+ 39 - 0
errors_test.go

@@ -0,0 +1,39 @@
+package fserr
+
+import (
+	"errors"
+	"fmt"
+	"testing"
+)
+
+var testErr = newError(ErrUnknownCode, "测试错误")
+
+func TestErrors(t *testing.T) {
+	err := testErr
+	fmt.Println("测试错误打印:", err)
+
+	if err != testErr {
+		t.Fatal("错误不同")
+	}
+
+	isRight := errors.Is(err, testErr)
+	if !isRight {
+		t.Fatal("Is错误")
+	}
+
+	targetErr := &Error{}
+	asRight := errors.As(err, &targetErr)
+	if !asRight {
+		t.Fatal("As错误")
+	}
+
+	fmt.Println("测试目标错误打印:", targetErr)
+
+	if !errors.Is(err, targetErr) {
+		t.Fatal("目标错误和原始错误不一致")
+	}
+
+	if err != targetErr {
+		t.Fatal("目标错误和原始错误不一致")
+	}
+}

+ 5 - 0
go.mod

@@ -0,0 +1,5 @@
+module git.sxidc.com/service-supports/fserr
+
+go 1.16
+
+require github.com/pkg/errors v0.9.1

+ 2 - 0
go.sum

@@ -0,0 +1,2 @@
+github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
+github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=