Ver código fonte

If there is a key named "request_id" in the context, add its value to the log.

Ma Cody 4 anos atrás
pai
commit
dd141abf72
2 arquivos alterados com 18 adições e 0 exclusões
  1. 2
    0
      request/go.sum
  2. 16
    0
      request/request.go

+ 2
- 0
request/go.sum Ver arquivo

@@ -7,6 +7,7 @@ github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6
7 7
 github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
8 8
 github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
9 9
 github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
10
+github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTMQQ=
10 11
 github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
11 12
 github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
12 13
 github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
@@ -36,6 +37,7 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6Zh
36 37
 golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
37 38
 golang.org/x/sys v0.0.0-20180918153733-ee1b12c67af4 h1:h8ij2QOL81JqJ/Vi5Ru+hl4a1yct8+XDGrgBhG0XbuE=
38 39
 golang.org/x/sys v0.0.0-20180918153733-ee1b12c67af4/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
40
+golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
39 41
 golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
40 42
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
41 43
 gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=

+ 16
- 0
request/request.go Ver arquivo

@@ -57,6 +57,14 @@ func Fail(ctx *gin.Context, err error) bool {
57 57
 		logrus.Error(err)
58 58
 	}
59 59
 
60
+	if requestID, found := ctx.Get("request_id"); found {
61
+		ctx.JSON(status, gin.H{
62
+			"msg": fmt.Sprintf("request_id: %s | %s",
63
+				requestID.(string),
64
+				err.Error())})
65
+		return true
66
+	}
67
+
60 68
 	ctx.JSON(status, gin.H{"msg": err.Error()})
61 69
 
62 70
 	return true
@@ -74,6 +82,14 @@ func Success(ctx *gin.Context, data interface{}) {
74 82
 	case http.MethodPost, http.MethodPut, http.MethodPatch:
75 83
 		ctx.JSON(http.StatusCreated, data)
76 84
 	default:
85
+		if requestID, found := ctx.Get("request_id"); found {
86
+			ctx.JSON(http.StatusBadRequest, HTTPError{
87
+				Msg: fmt.Sprintf("request_id: %s | unsupported request method %s",
88
+					requestID.(string),
89
+					ctx.Request.Method)})
90
+			return
91
+		}
92
+
77 93
 		ctx.JSON(http.StatusBadRequest, HTTPError{Msg: fmt.Sprintf("unsupported request method %s", ctx.Request.Method)})
78 94
 	}
79 95
 }