-
Notifications
You must be signed in to change notification settings - Fork 7
/
headers.go
66 lines (52 loc) · 1.28 KB
/
headers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Package stormrpc provides the functionality for creating RPC servers/clients that communicate via NATS.
package stormrpc
import (
"strconv"
"strings"
"time"
"github.com/nats-io/nats.go"
)
const (
// errorHeader will be deprecated in a future update in favor of 'Nats-Service-Error' and 'Nats-Service-Error-Code'.
errorHeader = "stormrpc-error"
deadlineHeader = "stormrpc-deadline"
)
func setDeadlineHeader(header nats.Header, deadline time.Time) {
header.Set(deadlineHeader, strconv.FormatInt(deadline.Unix(), 10))
}
func parseDeadlineHeader(header nats.Header) time.Time {
dh := header.Get(deadlineHeader)
if dh == "" {
return time.Time{}
}
i, err := strconv.ParseInt(dh, 10, 64)
if err != nil {
return time.Time{}
}
return time.Unix(i, 0)
}
func setErrorHeader(header nats.Header, err error) {
header.Set(errorHeader, err.Error())
}
func parseErrorHeader(header nats.Header) *Error {
eh := header.Get(errorHeader)
if eh == "" {
return nil
}
sp := strings.Split(eh, ":")
if len(sp) < 2 {
return &Error{
Code: ErrorCodeUnknown,
Message: "unknown error",
}
}
code := codeFromString(strings.TrimSpace(sp[0]))
msg := strings.TrimSpace(sp[1])
if code == ErrorCodeUnknown {
msg = "unknown error"
}
return &Error{
Code: code,
Message: msg,
}
}