5
0

2 کامیت‌ها 849af4d77f ... 7a5423c9dd

نویسنده SHA1 پیام تاریخ
  jys 7a5423c9dd 应用码取消2位限制 1 سال پیش
  jys 05836f0b7b 删除README 1 سال پیش
4فایلهای تغییر یافته به همراه184 افزوده شده و 71 حذف شده
  1. 0 48
      README.md
  2. 172 0
      logs/log
  3. 4 15
      public.go
  4. 8 8
      public_test.go

+ 0 - 48
README.md

@@ -1,48 +0,0 @@
-# fserr
-
-方是错误封装
-
-# v2alpha版本更新
-
-## 特性
-1. 采用6位错误码定位具体错误类型,并根据错误类型给出更加具体的错误信息;
-2. 用户无感知的栈信息;
-3. 兼容主流错误风格及api,高度可替换性。
-
-## 快速开始
-
-```go
-package main
-
-import (
-	"fmt"
-	"git.sxidc.com/service-supports/fserr/v2alpha"
-)
-
-const (
-	ErrProjectNotExist = iota + 201
-)
-
-func init() {
-	// 设置服务码
-	fserr.SetAppCode(10)
-
-	// fserr包内置错误码
-	fserr.NewOK(fserr.ErrDb, "处理失败,请联系开发人员")
-
-	// 业务模块错误
-	fserr.NewOK(ErrProjectNotExist, "项目不存在")
-}
-
-func Add() error {
-	return fserr.WithCode(nil, ErrProjectNotExist)
-}
-
-func main() {
-	err := Add()
-	if err != nil {
-		fmt.Printf("%+v\n", fserr.ParseCode(err))
-		return
-	}
-}
-```

+ 172 - 0
logs/log

@@ -0,0 +1,172 @@
+2023-09-05T15:37:24.522+0800	ERROR	test/main.go:10	
+aaa
+
+2023-09-05T15:37:35.702+0800	ERROR	test/main.go:10	
+aaa
+main.b
+	C:/Users/26897/gopath/src/fserr/test/main.go:18
+main.a
+	C:/Users/26897/gopath/src/fserr/test/main.go:14
+main.main
+	C:/Users/26897/gopath/src/fserr/test/main.go:9
+runtime.main
+	C:/Program Files/Go/src/runtime/proc.go:267
+runtime.goexit
+	C:/Program Files/Go/src/runtime/asm_amd64.s:1650
+
+2023-09-05T15:37:47.586+0800	ERROR	test/main.go:10	
+aaa
+
+2023-09-05T15:37:55.463+0800	ERROR	test/main.go:10	
+aaa
+
+
+2023-09-05T15:39:28.848+0800	ERROR	test/main.go:10	
+aaa
+
+2023-09-05T15:51:21.420+0800	ERROR	test/main.go:10	
+aaa
+
+2023-09-05T15:51:28.285+0800	ERROR	test/main.go:10	
+aaa
+
+2023-09-05T15:51:55.549+0800	ERROR	test/main.go:10	
+aaa
+
+2023-09-05T16:02:52.141+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:02:52.153+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:02:52.153+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:02:52.153+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:02:52.153+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:02:52.153+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:02:52.153+0800	WARN	fserr/center.go:18	duplicate business code, code: 300001, message: basic error
+2023-09-05T16:02:52.153+0800	WARN	fserr/center.go:18	duplicate business code, code: 300001, message: basic error
+2023-09-05T16:03:02.131+0800	WARN	fserr/center.go:18	duplicate business code, code: 300001, message: basic error
+2023-09-05T16:03:22.744+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:03:22.754+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:03:22.754+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:03:22.754+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:03:22.754+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:03:22.754+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:03:22.754+0800	WARN	fserr/center.go:18	duplicate business code, code: 300001, message: basic error
+2023-09-05T16:03:22.754+0800	WARN	fserr/center.go:18	duplicate business code, code: 300001, message: basic error
+2023-09-05T16:03:26.771+0800	ERROR	fserr/public_test.go:132	
+stack error
+git.sxidc.com/service-supports/fserr.(*TestPublicSuite).SetupTest
+	C:/Users/26897/gopath/src/fserr/public_test.go:42
+github.com/stretchr/testify/suite.Run.func1
+	C:/Users/26897/gopath/pkg/mod/github.com/stretchr/testify@v1.8.3/suite/suite.go:187
+testing.tRunner
+	C:/Program Files/Go/src/testing/testing.go:1595
+runtime.goexit
+	C:/Program Files/Go/src/runtime/asm_amd64.s:1650
+
+2023-09-05T16:03:34.181+0800	WARN	fserr/center.go:18	duplicate business code, code: 300001, message: basic error
+2023-09-05T16:04:11.072+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:04:11.081+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:04:11.081+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:04:11.082+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:04:11.082+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:04:11.082+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-09-05T16:04:11.082+0800	WARN	fserr/center.go:18	duplicate business code, code: 300001, message: basic error
+2023-09-05T16:04:11.082+0800	WARN	fserr/center.go:18	duplicate business code, code: 300001, message: basic error
+2023-09-05T16:04:11.082+0800	ERROR	fserr/public_test.go:132	
+stack error
+git.sxidc.com/service-supports/fserr.(*TestPublicSuite).SetupTest
+	C:/Users/26897/gopath/src/fserr/public_test.go:42
+github.com/stretchr/testify/suite.Run.func1
+	C:/Users/26897/gopath/pkg/mod/github.com/stretchr/testify@v1.8.3/suite/suite.go:187
+testing.tRunner
+	C:/Program Files/Go/src/testing/testing.go:1595
+runtime.goexit
+	C:/Program Files/Go/src/runtime/asm_amd64.s:1650
+
+2023-09-05T16:04:11.082+0800	WARN	fserr/center.go:18	duplicate business code, code: 300001, message: basic error
+2023-10-20T10:05:20.271+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:05:20.280+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:05:20.280+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:05:20.280+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:05:20.280+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:05:20.280+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:05:20.280+0800	WARN	fserr/center.go:18	duplicate business code, code: 3000000001, message: basic error
+2023-10-20T10:05:20.280+0800	WARN	fserr/center.go:18	duplicate business code, code: 3000000001, message: basic error
+2023-10-20T10:05:20.281+0800	ERROR	fserr/public_test.go:132	
+stack error
+git.sxidc.com/service-supports/fserr.(*TestPublicSuite).SetupTest
+	C:/Users/26897/gopath/src/fserr/public_test.go:42
+github.com/stretchr/testify/suite.Run.func1
+	C:/Users/26897/gopath/pkg/mod/github.com/stretchr/testify@v1.8.3/suite/suite.go:187
+testing.tRunner
+	C:/Program Files/Go/src/testing/testing.go:1595
+runtime.goexit
+	C:/Program Files/Go/src/runtime/asm_amd64.s:1650
+
+2023-10-20T10:05:20.281+0800	WARN	fserr/center.go:18	duplicate business code, code: 3000000001, message: basic error
+2023-10-20T10:08:38.948+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:08:38.955+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:08:38.955+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:08:38.955+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:08:38.955+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:08:38.956+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:08:38.956+0800	WARN	fserr/center.go:18	duplicate business code, code: 30001, message: basic error
+2023-10-20T10:08:38.956+0800	WARN	fserr/center.go:18	duplicate business code, code: 30001, message: basic error
+2023-10-20T10:08:38.956+0800	ERROR	fserr/public_test.go:132	
+stack error
+git.sxidc.com/service-supports/fserr.(*TestPublicSuite).SetupTest
+	C:/Users/26897/gopath/src/fserr/public_test.go:42
+github.com/stretchr/testify/suite.Run.func1
+	C:/Users/26897/gopath/pkg/mod/github.com/stretchr/testify@v1.8.3/suite/suite.go:187
+testing.tRunner
+	C:/Program Files/Go/src/testing/testing.go:1595
+runtime.goexit
+	C:/Program Files/Go/src/runtime/asm_amd64.s:1650
+
+2023-10-20T10:08:38.956+0800	WARN	fserr/center.go:18	duplicate business code, code: 30001, message: basic error
+2023-10-20T10:08:53.173+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:08:53.182+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:08:53.182+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:08:53.182+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:08:53.182+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:08:53.182+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:08:53.182+0800	WARN	fserr/center.go:18	duplicate business code, code: 30001, message: basic error
+2023-10-20T10:08:53.182+0800	WARN	fserr/center.go:18	duplicate business code, code: 30001, message: basic error
+2023-10-20T10:08:53.183+0800	ERROR	fserr/public_test.go:132	
+stack error
+git.sxidc.com/service-supports/fserr.(*TestPublicSuite).SetupTest
+	C:/Users/26897/gopath/src/fserr/public_test.go:42
+github.com/stretchr/testify/suite.Run.func1
+	C:/Users/26897/gopath/pkg/mod/github.com/stretchr/testify@v1.8.3/suite/suite.go:187
+testing.tRunner
+	C:/Program Files/Go/src/testing/testing.go:1595
+runtime.goexit
+	C:/Program Files/Go/src/runtime/asm_amd64.s:1650
+
+2023-10-20T10:08:53.183+0800	WARN	fserr/center.go:18	duplicate business code, code: 30001, message: basic error
+2023-10-20T10:09:03.537+0800	WARN	fserr/center.go:18	duplicate business code, code: 1001, message: ok
+2023-10-20T10:09:03.545+0800	WARN	fserr/center.go:18	duplicate business code, code: 1001, message: ok
+2023-10-20T10:09:03.545+0800	WARN	fserr/center.go:18	duplicate business code, code: 1001, message: ok
+2023-10-20T10:09:03.546+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:09:03.546+0800	WARN	fserr/center.go:18	duplicate business code, code: 100102, message: customer error
+2023-10-20T10:09:03.546+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:09:03.546+0800	WARN	fserr/center.go:18	duplicate business code, code: 100102, message: customer error
+2023-10-20T10:09:03.548+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:09:03.548+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:09:03.548+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:09:03.548+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:09:03.548+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:09:03.549+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:09:03.549+0800	WARN	fserr/center.go:18	duplicate business code, code: 1, message: basic error
+2023-10-20T10:09:03.549+0800	WARN	fserr/center.go:18	duplicate business code, code: 30001, message: basic error
+2023-10-20T10:09:03.549+0800	WARN	fserr/center.go:18	duplicate business code, code: 30001, message: basic error
+2023-10-20T10:09:03.549+0800	ERROR	fserr/public_test.go:132	
+stack error
+git.sxidc.com/service-supports/fserr.(*TestPublicSuite).SetupTest
+	C:/Users/26897/gopath/src/fserr/public_test.go:42
+github.com/stretchr/testify/suite.Run.func1
+	C:/Users/26897/gopath/pkg/mod/github.com/stretchr/testify@v1.8.3/suite/suite.go:187
+testing.tRunner
+	C:/Program Files/Go/src/testing/testing.go:1595
+runtime.goexit
+	C:/Program Files/Go/src/runtime/asm_amd64.s:1650
+
+2023-10-20T10:09:03.549+0800	WARN	fserr/center.go:18	duplicate business code, code: 30001, message: basic error

+ 4 - 15
public.go

@@ -3,8 +3,6 @@ package fserr
 import (
 	"errors"
 	"fmt"
-	"strconv"
-	"strings"
 )
 
 // New 创建新的错误,支持格式化占位符
@@ -104,7 +102,7 @@ func ParseCode(err error) *withCode {
 			cause:        nil,
 			Msg:          err.Error(),
 			HttpCode:     defaultErrCode.HttpCode,
-			BusinessCode: serviceCode * 10000,
+			BusinessCode: serviceCode,
 		}
 	}
 
@@ -126,19 +124,10 @@ func IsCode[T codeType](err error, code T) bool {
 }
 
 // SetAppCode 设置服务错误码
-// 例如设置服务码为300000,用户不存在错误码为2001,
-// 那么最终业务错误码为302001
+// 模块码、模块错误码一共四位,指定应用码将拼接在前
+// 例如应用码位101,模块码为1,模块错误码为21,那么最终业务错误码为:1010121
 func SetAppCode[T codeType](code T) {
-	strCode := strconv.Itoa(int(code))
-	if len(strCode) == 1 {
-		strCode = "0" + strCode
-	}
-	c, err := strconv.ParseInt(strCode+strings.Repeat("0", 6-len(strCode)),
-		10, 64)
-	if err != nil {
-		panic(err)
-	}
-	serviceCode = int(c)
+	serviceCode = int(code) * 10000
 }
 
 // outerMsg 获取最外层的错误信息

+ 8 - 8
public_test.go

@@ -121,10 +121,10 @@ func (s *TestPublicSuite) TestIsCode() {
 }
 
 func (s *TestPublicSuite) TestSetServiceCode() {
-	SetAppCode(300000)
+	SetAppCode(3)
 	NewOK(2001, "ok")
 	err := WithCode(nil, 2001)
-	s.Equal(302001, ParseCode(err).BusinessCode)
+	s.Equal(32001, ParseCode(err).BusinessCode)
 }
 
 func (s *TestPublicSuite) TestWithStack() {
@@ -144,24 +144,24 @@ func TestServiceCode(t *testing.T) {
 	assert.Equal(t, 10001, code.BusinessCode)
 	assert.Equal(t, http.StatusInternalServerError, code.HttpCode)
 
-	SetAppCode(10)
+	SetAppCode(2)
 	NewBadRequest(ErrDb, "db error")
 	code = ParseCode(WithCode(nil, ErrDb))
 	assert.Equal(t, "db error", code.Msg)
-	assert.Equal(t, 100002, code.BusinessCode)
+	assert.Equal(t, 20002, code.BusinessCode)
 	assert.Equal(t, http.StatusBadRequest, code.HttpCode)
 
-	SetAppCode(100000)
+	SetAppCode(3)
 	NewInternalError(ErrParam, "param error")
 	code = ParseCode(WithCode(nil, ErrParam))
 	assert.Equal(t, "param error", code.Msg)
-	assert.Equal(t, 100003, code.BusinessCode)
+	assert.Equal(t, 30003, code.BusinessCode)
 	assert.Equal(t, http.StatusInternalServerError, code.HttpCode)
 
-	SetAppCode(1000)
+	SetAppCode(4)
 	NewConflict(ErrRetry, "retry error")
 	code = ParseCode(WithCode(nil, ErrRetry))
-	assert.Equal(t, 100004, code.BusinessCode)
+	assert.Equal(t, 40004, code.BusinessCode)
 	assert.Equal(t, http.StatusConflict, code.HttpCode)
 	assert.Equal(t, "retry error", code.Msg)
 }