浏览代码

export tcp status code

Paul 5 年前
父节点
当前提交
8231ff791f
共有 4 个文件被更改,包括 32 次插入43 次删除
  1. 3
    5
      request/go.mod
  2. 1
    23
      request/go.sum
  3. 6
    1
      request/tr/error.go
  4. 22
    14
      request/tr/tr.go

+ 3
- 5
request/go.mod 查看文件

@@ -4,21 +4,19 @@ require (
4 4
 	github.com/davecgh/go-spew v1.1.1 // indirect
5 5
 	github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 // indirect
6 6
 	github.com/gin-gonic/gin v1.3.0
7
+	github.com/golang/protobuf v1.2.0 // indirect
7 8
 	github.com/json-iterator/go v1.1.5 // indirect
8 9
 	github.com/mattn/go-isatty v0.0.4 // indirect
9 10
 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
10 11
 	github.com/modern-go/reflect2 v1.0.1 // indirect
11
-	github.com/onsi/gomega v1.4.2 // indirect
12 12
 	github.com/pmezard/go-difflib v1.0.0 // indirect
13
-	github.com/sirupsen/logrus v1.0.6
14 13
 	github.com/stretchr/testify v1.2.2 // indirect
15 14
 	github.com/ugorji/go/codec v0.0.0-20180831062425-e253f1f20942 // indirect
16 15
 	github.com/wpajqz/linker v0.0.0-20180920052826-c21901f38257
17
-	golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b // indirect
18 16
 	golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc // indirect
17
+	golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect
19 18
 	golang.org/x/sys v0.0.0-20180918153733-ee1b12c67af4 // indirect
20
-	gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
21
-	gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
22 19
 	gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
23 20
 	gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
21
+	gopkg.in/yaml.v2 v2.2.1 // indirect
24 22
 )

+ 1
- 23
request/go.sum 查看文件

@@ -1,7 +1,5 @@
1 1
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2 2
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3
-github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
4
-github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
5 3
 github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cougNAV9szl6CVoj2RYwzS3DpUQNtlY=
6 4
 github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
7 5
 github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
@@ -10,8 +8,6 @@ github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
10 8
 github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
11 9
 github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
12 10
 github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
13
-github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
14
-github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
15 11
 github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
16 12
 github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
17 13
 github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
@@ -20,14 +16,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
20 16
 github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
21 17
 github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
22 18
 github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
23
-github.com/onsi/ginkgo v1.6.0 h1:Ix8l273rp3QzYgXSR+c8d1fTG7UPgYkOSELPhiY/YGw=
24
-github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
25
-github.com/onsi/gomega v1.4.2 h1:3mYCb7aPxS/RU7TI1y4rkEn1oKmPRjNJLNEXgw7MH2I=
26
-github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
27 19
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
28 20
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
29
-github.com/sirupsen/logrus v1.0.6 h1:hcP1GmhGigz/O7h1WVUM5KklBp1JoNS9FggWKdj/j3s=
30
-github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
31 21
 github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
32 22
 github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
33 23
 github.com/ugorji/go/codec v0.0.0-20180831062425-e253f1f20942 h1:CZORS/4d6i+5FKSAtbRIjlElV2BAFYv/bokcaEVUimQ=
@@ -36,28 +26,16 @@ github.com/wpajqz/go-sdk v0.0.0-20180819022515-c5611acbd333/go.mod h1:8Z5OwaDGrR
36 26
 github.com/wpajqz/linker v0.0.0-20180818151004-5d64412a1130/go.mod h1:1LpNSex7lqvQi8t5uj9LBLKsvxfm3HK2j6o5Ht3BGmQ=
37 27
 github.com/wpajqz/linker v0.0.0-20180920052826-c21901f38257 h1:i5vHdYSGYZr6fJ2S/MTY8PCvUAZCks5wbZcmBgmNKGc=
38 28
 github.com/wpajqz/linker v0.0.0-20180920052826-c21901f38257/go.mod h1:qDGjG+ES9GK54nkt/8PE3SbGtKyZI0U0XSU7B8hvhqM=
39
-golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b h1:2b9XGzhjiYsYPnKXoEfL7klWZQIt8IfyRCz62gCqqlQ=
40
-golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
41
-golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
42 29
 golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc h1:ZMCWScCvS2fUVFw8LOpxyUUW5qiviqr4Dg5NdjLeiLU=
43 30
 golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
31
+golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
44 32
 golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
45
-golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
46 33
 golang.org/x/sys v0.0.0-20180918153733-ee1b12c67af4 h1:h8ij2QOL81JqJ/Vi5Ru+hl4a1yct8+XDGrgBhG0XbuE=
47 34
 golang.org/x/sys v0.0.0-20180918153733-ee1b12c67af4/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
48
-golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
49
-gopkg.in/airbrake/gobrake.v2 v2.0.9 h1:7z2uVWwn7oVeeugY1DtlPAy5H+KYgB1KeKTnqjNatLo=
50
-gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
51 35
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
52
-gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
53
-gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
54
-gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 h1:OAj3g0cR6Dx/R07QgQe8wkA9RNjB2u4i700xBkIT4e0=
55
-gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo=
56 36
 gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
57 37
 gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
58 38
 gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
59 39
 gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
60
-gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
61
-gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
62 40
 gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
63 41
 gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

+ 6
- 1
request/tr/error.go 查看文件

@@ -2,5 +2,10 @@ package tr
2 2
 
3 3
 // TCPError response tcp error
4 4
 type TCPError struct {
5
-	Msg string `json:"msg" example:"status bad request"`
5
+	Status int    `json:"status"`
6
+	Msg    string `json:"msg"`
7
+}
8
+
9
+func (e TCPError) Error() string {
10
+	return e.Msg
6 11
 }

+ 22
- 14
request/tr/tr.go 查看文件

@@ -1,9 +1,6 @@
1 1
 package tr
2 2
 
3 3
 import (
4
-	"runtime"
5
-
6
-	"github.com/sirupsen/logrus"
7 4
 	"github.com/wpajqz/linker"
8 5
 )
9 6
 
@@ -13,36 +10,47 @@ type Checker interface {
13 10
 
14 11
 func ParseParamFail(ctx linker.Context, param interface{}, fs ...func() error) bool {
15 12
 	if param != nil {
16
-		if Fail(ctx, linker.StatusBadRequest, ctx.ParseParam(param)) {
17
-			return true
13
+		err := ctx.ParseParam(param)
14
+		if err != nil {
15
+			err = TCPError{Status: linker.StatusBadRequest, Msg: err.Error()}
16
+
17
+			return Fail(ctx, err)
18 18
 		}
19 19
 
20 20
 		if checker, ok := param.(Checker); ok {
21
-			return Fail(ctx, linker.StatusBadRequest, checker.Check())
21
+			err := checker.Check()
22
+			if err != nil {
23
+				err = TCPError{Status: linker.StatusBadRequest, Msg: err.Error()}
24
+
25
+				return Fail(ctx, checker.Check())
26
+			}
22 27
 		}
23 28
 	}
24 29
 
25 30
 	for _, f := range fs {
26
-		if Fail(ctx, linker.StatusBadRequest, f()) {
27
-			return true
31
+		err := f()
32
+		if err != nil {
33
+			err = TCPError{Status: linker.StatusBadRequest, Msg: err.Error()}
34
+
35
+			return Fail(ctx, err)
28 36
 		}
29 37
 	}
30 38
 
31 39
 	return false
32 40
 }
33 41
 
34
-func Fail(ctx linker.Context, status int, err error) bool {
42
+func Fail(ctx linker.Context, err error) bool {
35 43
 	if err == nil {
36 44
 		return false
37 45
 	}
38 46
 
39
-	ctx.Error(status, err.Error())
40
-
41
-	if status == linker.StatusInternalServerError {
42
-		_, file, line, _ := runtime.Caller(1)
43
-		logrus.WithField("err", err).Errorf("file:%s line:%d", file, line)
47
+	status := linker.StatusInternalServerError
48
+	if v, ok := err.(TCPError); ok {
49
+		status = v.Status
44 50
 	}
45 51
 
52
+	ctx.Error(status, err.Error())
53
+
46 54
 	return true
47 55
 }
48 56