-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathtwirp_error
44 lines (38 loc) · 1.22 KB
/
twirp_error
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
import (
"github.com/twitchtv/twirp"
)
{{ $decorator := (or .Vars.DecoratorName (printf "%sWithTwirpError" .Interface.Name)) }}
// {{$decorator}} implements {{.Interface.Type}} interface instrumented such that the request data is injected into twirp.Error as metadata
type {{$decorator}} struct {
{{.Interface.Type}}
}
// New{{$decorator}} returns {{$decorator}}
func New{{$decorator}} (base {{.Interface.Type}}) {{$decorator}} {
return {{$decorator}} {
{{.Interface.Name}}: base,
}
}
{{range $method := .Interface.Methods}}
// {{$method.Name}} implements {{$.Interface.Type}}
func (_d {{$decorator}}) {{$method.Declaration}} {
{{- if $method.ReturnsError}}
{{range $param := $method.Params}}
{{if not ( and $method.AcceptsContext (eq $param.Name "ctx")) }}
defer injectRequestDataToError({{$param.Name}} ,&err)
{{end}}
{{end}}
{{end}}
{{$method.Pass (printf "_d.%s." $.Interface.Name) }}
}
{{end}}
func injectRequestDataToError(req interface{}, perr *error) {
err := *perr
if err != nil {
twerr, ok := err.(twirp.Error)
if !ok {
twerr = twirp.InternalErrorWith(err)
}
jsonReq, _ := json.Marshal(req)
*perr = twerr.WithMeta("request", string(jsonReq))
}
}