Browse Source

去除旧版本代码

jys 2 years ago
parent
commit
810860690e
14 changed files with 131 additions and 329 deletions
  1. 0 0
      center.go
  2. 0 0
      center_test.go
  3. 0 0
      code.go
  4. 0 0
      code_test.go
  5. 0 0
      compatible.go
  6. 74 110
      errors.go
  7. 57 33
      errors_test.go
  8. 0 0
      option.go
  9. 0 0
      option_test.go
  10. 0 0
      public.go
  11. 0 0
      public_test.go
  12. 0 0
      stack.go
  13. 0 110
      v2alpha/errors.go
  14. 0 76
      v2alpha/errors_test.go

+ 0 - 0
v2alpha/center.go → center.go


+ 0 - 0
v2alpha/center_test.go → center_test.go


+ 0 - 0
v2alpha/code.go → code.go


+ 0 - 0
v2alpha/code_test.go → code_test.go


+ 0 - 0
v2alpha/compatible.go → compatible.go


+ 74 - 110
errors.go

@@ -2,145 +2,109 @@ package fserr
 
 import (
 	"fmt"
-	"github.com/pkg/errors"
 	"io"
 )
 
-const (
-	ErrUnknownCode      uint32 = 0x00000001
-	ErrParamCode        uint32 = 0x00000002
-	ErrConflictCode     uint32 = 0x00000003
-	ErrNotExistCode     uint32 = 0x00000004
-	ErrTypeTransferCode uint32 = 0x00000005
-	ErrAuthCode         uint32 = 0x00000006
-	ErrDataCode         uint32 = 0x00000007
-	ErrCustomCode       uint32 = 0x00001000
-)
-
-var codeMap = map[uint32]string{
-	ErrUnknownCode:      "未知错误",
-	ErrParamCode:        "参数错误",
-	ErrConflictCode:     "资源冲突",
-	ErrNotExistCode:     "资源不存在",
-	ErrTypeTransferCode: "类型转换错误",
-	ErrAuthCode:         "鉴权错误",
-	ErrDataCode:         "数据异常",
-	ErrCustomCode:       "自定义错误",
+type fundamental struct {
+	msg string
+	*stack
 }
 
-func ExplainCode(code uint32) string {
-	explain, ok := codeMap[code]
-	if !ok {
-		return codeMap[ErrUnknownCode]
-	}
+func (f *fundamental) Error() string { return f.msg }
 
-	return explain
-}
-
-func TransferFromError(err error) *Error {
-	transferErr, ok := err.(*Error)
-	if ok {
-		return transferErr
+func (f *fundamental) Format(s fmt.State, verb rune) {
+	switch verb {
+	case 'v':
+		if s.Flag('+') {
+			_, _ = io.WriteString(s, f.msg)
+			f.stack.Format(s, verb)
+			return
+		}
+		fallthrough
+	case 's':
+		_, _ = io.WriteString(s, f.msg)
+	case 'q':
+		_, _ = fmt.Fprintf(s, "%q", f.msg)
 	}
-
-	return NewUnknownError(err.Error())
-}
-
-func NewError(code uint32, message string) *Error {
-	return newError(code, message)
-}
-
-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 NewDataError(message string) *Error {
-	return newError(ErrDataCode, message)
+type withStack struct {
+	error
+	*stack
 }
 
-func NewCustomError(message string) *Error {
-	return newError(ErrCustomCode, message)
-}
-
-type Error struct {
-	code         uint32
-	message      string
-	StackMessage string
-}
+func (w *withStack) Cause() error { return w.error }
 
-func (err *Error) Code() uint32 {
-	return err.code
-}
+func (w *withStack) Unwrap() error { return w.error }
 
-func (err *Error) Error() string {
-	return err.message
-}
-
-func (err *Error) WithStack() error {
-	return &Error{
-		code:         err.code,
-		message:      err.message,
-		StackMessage: formStackMessage(err.message),
+func (w *withStack) Format(s fmt.State, verb rune) {
+	switch verb {
+	case 'v':
+		if s.Flag('+') {
+			if w.Cause() != nil {
+				_, _ = fmt.Fprintf(s, "%+v", w.Cause())
+			}
+			w.stack.Format(s, verb)
+			return
+		}
+		fallthrough
+	case 's':
+		_, _ = io.WriteString(s, w.Error())
+	case 'q':
+		_, _ = fmt.Fprintf(s, "%q", w.Error())
 	}
 }
 
-func (err *Error) Is(target error) bool {
-	transferErr, ok := target.(*Error)
-	if !ok {
-		return false
-	}
-
-	return err.message == transferErr.message && err.code == transferErr.code
+type withMessage struct {
+	cause error
+	msg   string
 }
 
-func (err *Error) Format(s fmt.State, verb rune) {
+func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() }
+func (w *withMessage) Cause() error  { return w.cause }
+func (w *withMessage) Unwrap() error { return w.cause }
+func (w *withMessage) Format(s fmt.State, verb rune) {
 	switch verb {
 	case 'v':
 		if s.Flag('+') {
-			io.WriteString(s, err.StackMessage)
+			_, _ = io.WriteString(s, w.msg+": ")
+			if w.Cause() != nil {
+				_, _ = fmt.Fprintf(s, "%+v", w.Cause())
+			}
 			return
 		}
 		fallthrough
 	case 's':
-		io.WriteString(s, err.message)
+		_, _ = io.WriteString(s, w.Error())
 	case 'q':
-		fmt.Fprintf(s, "%q", err.message)
+		_, _ = fmt.Fprintf(s, "%q", w.Error())
 	}
 }
 
-func newError(code uint32, message string) *Error {
-	_, ok := codeMap[code]
-	if !ok {
-		code = ErrUnknownCode
-	}
-
-	return &Error{
-		code:         code,
-		message:      message,
-		StackMessage: formStackMessage(message),
-	}
+type withCode struct {
+	cause        error
+	Msg          string `json:"msg"`
+	HttpCode     int    `json:"httpCode"`
+	BusinessCode int    `json:"businessCode"`
 }
 
-func formStackMessage(message string) string {
-	return fmt.Sprintf("\n%+v\n", errors.New(message))
+func (w *withCode) Error() string { return w.Msg }
+func (w *withCode) Cause() error  { return w.cause }
+func (w *withCode) Unwrap() error { return w.cause }
+func (w *withCode) Format(s fmt.State, verb rune) {
+	switch verb {
+	case 'v':
+		if s.Flag('+') {
+			_, _ = io.WriteString(s, w.Msg+"\n")
+			if w.Cause() != nil {
+				_, _ = fmt.Fprintf(s, "%+v", w.Cause())
+			}
+			return
+		}
+		fallthrough
+	case 's':
+		_, _ = io.WriteString(s, w.Error())
+	case 'q':
+		_, _ = fmt.Fprintf(s, "%q", w.Error())
+	}
 }

+ 57 - 33
errors_test.go

@@ -1,52 +1,76 @@
 package fserr
 
 import (
-	"errors"
 	"fmt"
+	"github.com/stretchr/testify/suite"
+	"net/http"
 	"testing"
 )
 
-var testErr = newError(ErrUnknownCode, "测试错误")
-
-func TestErrors(t *testing.T) {
-	fmt.Printf("测试错误打印: %+v\n", testErr)
-
-	err := Func1()
-	fmt.Printf("测试Func3错误打印: %+v\n", err)
-
-	err = testErr.WithStack()
-	fmt.Printf("测试WithStack错误打印: %+v\n", err)
+type TestErrorsSuite struct {
+	suite.Suite
+	originFundamental *fundamental
+	originStack       *withStack
+	originMessage     *withMessage
+	originCode        *withCode
+}
 
-	isRight := errors.Is(err, testErr)
-	if !isRight {
-		t.Fatal("Is错误")
+func (s *TestErrorsSuite) SetupTest() {
+	s.originFundamental = &fundamental{
+		msg:   "origin fundamental",
+		stack: callers(),
 	}
-
-	targetErr := &Error{}
-	asRight := errors.As(err, &targetErr)
-	if !asRight {
-		t.Fatal("As错误")
+	s.originStack = &withStack{
+		error: s.originFundamental,
+		stack: callers(),
 	}
-
-	fmt.Printf("测试目标错误打印: %+v\n", targetErr)
-
-	if !errors.Is(err, targetErr) {
-		t.Fatal("目标错误和原始错误不一致")
+	s.originMessage = &withMessage{
+		cause: s.originStack,
+		msg:   "origin message",
 	}
-
-	if err != targetErr {
-		t.Fatal("目标错误和原始错误不一致")
+	s.originCode = &withCode{
+		cause:        s.originMessage,
+		Msg:          "with code",
+		HttpCode:     http.StatusOK,
+		BusinessCode: ErrBasic,
 	}
 }
 
-func Func1() error {
-	return Func2()
+func (s *TestErrorsSuite) TestFundamental() {
+	s.Equal("origin fundamental", s.originFundamental.msg)
+	s.Equal("origin fundamental", s.originFundamental.Error())
+	s.Equal("origin fundamental",
+		fmt.Sprintf("%s", s.originFundamental))
+	s.Equal(`"origin fundamental"`,
+		fmt.Sprintf("%q", s.originFundamental))
+}
+
+func (s *TestErrorsSuite) TestStack() {
+	s.Equal(s.originFundamental, s.originStack.Cause())
+	s.Equal(s.originFundamental, s.originStack.Unwrap())
+	s.Equal("origin fundamental", s.originStack.Error())
+	s.Equal("origin fundamental", fmt.Sprintf("%s", s.originStack))
+	s.Equal(`"origin fundamental"`, fmt.Sprintf("%q", s.originStack))
 }
 
-func Func2() error {
-	return Func3()
+func (s *TestErrorsSuite) TestMessage() {
+	s.Equal(s.originStack, s.originMessage.Cause())
+	s.Equal(s.originStack, s.originMessage.Unwrap())
+	s.Equal("origin message: origin fundamental", s.originMessage.Error())
+	s.Equal("origin message: origin fundamental",
+		fmt.Sprintf("%s", s.originMessage))
+	s.Equal(`"origin message: origin fundamental"`,
+		fmt.Sprintf("%q", s.originMessage))
 }
 
-func Func3() error {
-	return testErr.WithStack()
+func (s *TestErrorsSuite) TestCode() {
+	s.Equal(s.originMessage, s.originCode.Cause())
+	s.Equal(s.originMessage, s.originCode.Unwrap())
+	s.Equal("with code", s.originCode.Error())
+	s.Equal("with code", fmt.Sprintf("%s", s.originCode))
+	s.Equal(`"with code"`, fmt.Sprintf("%q", s.originCode))
+}
+
+func TestErrors(t *testing.T) {
+	suite.Run(t, &TestErrorsSuite{})
 }

+ 0 - 0
v2alpha/option.go → option.go


+ 0 - 0
v2alpha/option_test.go → option_test.go


+ 0 - 0
v2alpha/public.go → public.go


+ 0 - 0
v2alpha/public_test.go → public_test.go


+ 0 - 0
v2alpha/stack.go → stack.go


+ 0 - 110
v2alpha/errors.go

@@ -1,110 +0,0 @@
-package fserr
-
-import (
-	"fmt"
-	"io"
-)
-
-type fundamental struct {
-	msg string
-	*stack
-}
-
-func (f *fundamental) Error() string { return f.msg }
-
-func (f *fundamental) Format(s fmt.State, verb rune) {
-	switch verb {
-	case 'v':
-		if s.Flag('+') {
-			_, _ = io.WriteString(s, f.msg)
-			f.stack.Format(s, verb)
-			return
-		}
-		fallthrough
-	case 's':
-		_, _ = io.WriteString(s, f.msg)
-	case 'q':
-		_, _ = fmt.Fprintf(s, "%q", f.msg)
-	}
-}
-
-type withStack struct {
-	error
-	*stack
-}
-
-func (w *withStack) Cause() error { return w.error }
-
-func (w *withStack) Unwrap() error { return w.error }
-
-func (w *withStack) Format(s fmt.State, verb rune) {
-	switch verb {
-	case 'v':
-		if s.Flag('+') {
-			if w.Cause() != nil {
-				_, _ = fmt.Fprintf(s, "%+v", w.Cause())
-			}
-			w.stack.Format(s, verb)
-			return
-		}
-		fallthrough
-	case 's':
-		_, _ = io.WriteString(s, w.Error())
-	case 'q':
-		_, _ = fmt.Fprintf(s, "%q", w.Error())
-	}
-}
-
-type withMessage struct {
-	cause error
-	msg   string
-}
-
-func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() }
-func (w *withMessage) Cause() error  { return w.cause }
-func (w *withMessage) Unwrap() error { return w.cause }
-func (w *withMessage) Format(s fmt.State, verb rune) {
-	switch verb {
-	case 'v':
-		if s.Flag('+') {
-			_, _ = io.WriteString(s, w.msg+": ")
-			if w.Cause() != nil {
-				_, _ = fmt.Fprintf(s, "%+v", w.Cause())
-			}
-			return
-		}
-		fallthrough
-	case 's':
-		_, _ = io.WriteString(s, w.Error())
-	case 'q':
-		_, _ = fmt.Fprintf(s, "%q", w.Error())
-	}
-}
-
-type withCode struct {
-	cause        error
-	Msg          string `json:"msg"`
-	HttpCode     int    `json:"httpCode"`
-	BusinessCode int    `json:"businessCode"`
-}
-
-func (w *withCode) Error() string { return w.Msg }
-func (w *withCode) Cause() error  { return w.cause }
-func (w *withCode) Unwrap() error { return w.cause }
-func (w *withCode) Format(s fmt.State, verb rune) {
-	switch verb {
-	case 'v':
-		if s.Flag('+') {
-			_, _ = io.WriteString(s, w.Msg+"\n")
-			if w.Cause() != nil {
-				_, _ = fmt.Fprintf(s, "%+v", w.Cause())
-			}
-			return
-		}
-		fallthrough
-	case 's':
-		_, _ = io.WriteString(s, w.Error())
-	case 'q':
-		_, _ = fmt.Fprintf(s, "%q", w.Error())
-	}
-}

+ 0 - 76
v2alpha/errors_test.go

@@ -1,76 +0,0 @@
-package fserr
-
-import (
-	"fmt"
-	"github.com/stretchr/testify/suite"
-	"net/http"
-	"testing"
-)
-
-type TestErrorsSuite struct {
-	suite.Suite
-	originFundamental *fundamental
-	originStack       *withStack
-	originMessage     *withMessage
-	originCode        *withCode
-}
-
-func (s *TestErrorsSuite) SetupTest() {
-	s.originFundamental = &fundamental{
-		msg:   "origin fundamental",
-		stack: callers(),
-	}
-	s.originStack = &withStack{
-		error: s.originFundamental,
-		stack: callers(),
-	}
-	s.originMessage = &withMessage{
-		cause: s.originStack,
-		msg:   "origin message",
-	}
-	s.originCode = &withCode{
-		cause:        s.originMessage,
-		Msg:          "with code",
-		HttpCode:     http.StatusOK,
-		BusinessCode: ErrBasic,
-	}
-}
-
-func (s *TestErrorsSuite) TestFundamental() {
-	s.Equal("origin fundamental", s.originFundamental.msg)
-	s.Equal("origin fundamental", s.originFundamental.Error())
-	s.Equal("origin fundamental",
-		fmt.Sprintf("%s", s.originFundamental))
-	s.Equal(`"origin fundamental"`,
-		fmt.Sprintf("%q", s.originFundamental))
-}
-
-func (s *TestErrorsSuite) TestStack() {
-	s.Equal(s.originFundamental, s.originStack.Cause())
-	s.Equal(s.originFundamental, s.originStack.Unwrap())
-	s.Equal("origin fundamental", s.originStack.Error())
-	s.Equal("origin fundamental", fmt.Sprintf("%s", s.originStack))
-	s.Equal(`"origin fundamental"`, fmt.Sprintf("%q", s.originStack))
-}
-
-func (s *TestErrorsSuite) TestMessage() {
-	s.Equal(s.originStack, s.originMessage.Cause())
-	s.Equal(s.originStack, s.originMessage.Unwrap())
-	s.Equal("origin message: origin fundamental", s.originMessage.Error())
-	s.Equal("origin message: origin fundamental",
-		fmt.Sprintf("%s", s.originMessage))
-	s.Equal(`"origin message: origin fundamental"`,
-		fmt.Sprintf("%q", s.originMessage))
-}
-
-func (s *TestErrorsSuite) TestCode() {
-	s.Equal(s.originMessage, s.originCode.Cause())
-	s.Equal(s.originMessage, s.originCode.Unwrap())
-	s.Equal("with code", s.originCode.Error())
-	s.Equal("with code", fmt.Sprintf("%s", s.originCode))
-	s.Equal(`"with code"`, fmt.Sprintf("%q", s.originCode))
-}
-
-func TestErrors(t *testing.T) {
-	suite.Run(t, &TestErrorsSuite{})
-}