Skip to content

Commit 36adfee

Browse files
committed
kite: add request ID to server-side kite handlers
1 parent b9112ab commit 36adfee

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

errors.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,32 @@ var ErrKeyNotTrusted = errors.New("kontrol key is not trusted")
1313

1414
// Error is the type of the kite related errors returned from kite package.
1515
type Error struct {
16-
Type string `json:"type"`
17-
Message string `json:"message"`
18-
CodeVal string `json:"code"`
16+
Type string `json:"type"`
17+
Message string `json:"message"`
18+
CodeVal string `json:"code"`
19+
RequestID string `json:"id"`
1920
}
2021

2122
func (e Error) Code() string {
2223
return e.CodeVal
2324
}
2425

2526
func (e Error) Error() string {
26-
if e.Type == "genericError" || e.Type == "" {
27-
return e.Message
27+
s := e.Message
28+
29+
if e.Type != "genericError" && e.Type != "" {
30+
s = e.Type + ": " + e.Message
31+
}
32+
33+
if e.RequestID != "" {
34+
return s + " (" + e.RequestID + ")"
2835
}
2936

30-
return fmt.Sprintf("%s: %s", e.Type, e.Message)
37+
return s
3138
}
3239

3340
// createError creates a new kite.Error for the given r variable
34-
func createError(r interface{}) *Error {
41+
func createError(req *Request, r interface{}) *Error {
3542
if r == nil {
3643
return nil
3744
}
@@ -52,5 +59,9 @@ func createError(r interface{}) *Error {
5259
}
5360
}
5461

62+
if kiteErr.RequestID == "" && req != nil {
63+
kiteErr.RequestID = req.ID
64+
}
65+
5566
return kiteErr
5667
}

method_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kite
22

33
import (
44
"errors"
5+
"strings"
56
"testing"
67
"time"
78
)
@@ -160,7 +161,7 @@ func TestMethod_Error(t *testing.T) {
160161
t.Fatal("PreHandle returns an error, however error is non-nil.")
161162
}
162163

163-
if err.Error() != testError.Error() {
164+
if !strings.HasPrefix(err.Error(), testError.Error()) {
164165
t.Errorf("Error should be '%v', got '%v'", testError, err)
165166
}
166167
}

request.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (c *Client) runMethod(method *Method, args *dnode.Partial) {
6666
defer func() {
6767
if r := recover(); r != nil {
6868
debug.PrintStack()
69-
kiteErr := createError(r)
69+
kiteErr := createError(request, r)
7070
c.LocalKite.Log.Error(kiteErr.Error()) // let's log it too :)
7171
callFunc(nil, kiteErr)
7272
}
@@ -76,7 +76,7 @@ func (c *Client) runMethod(method *Method, args *dnode.Partial) {
7676
request, callFunc = c.newRequest(method.name, args)
7777
if method.authenticate {
7878
if err := request.authenticate(); err != nil {
79-
callFunc(nil, err)
79+
callFunc(nil, createError(request, err))
8080
return
8181
}
8282
} else {
@@ -101,16 +101,17 @@ func (c *Client) runMethod(method *Method, args *dnode.Partial) {
101101
// available more so it will return a zero.
102102
if method.bucket != nil && method.bucket.TakeAvailable(1) == 0 {
103103
callFunc(nil, &Error{
104-
Type: "requestLimitError",
105-
Message: "The maximum request rate is exceeded.",
104+
Type: "requestLimitError",
105+
Message: "The maximum request rate is exceeded.",
106+
RequestID: request.ID,
106107
})
107108
return
108109
}
109110

110111
// Call the handler functions.
111112
result, err := method.ServeKite(request)
112113

113-
callFunc(result, createError(err))
114+
callFunc(result, createError(request, err))
114115
}
115116

116117
// runCallback is called when a callback method call is received from remote Kite.

0 commit comments

Comments
 (0)