-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patherrors.go.tmpl
96 lines (81 loc) · 2.23 KB
/
errors.go.tmpl
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
{{define "errors"}}
{{- $webrpcErrors := .WebrpcErrors -}}
{{- $schemaErrors := .SchemaErrors -}}
{{- $opts := .Opts -}}
//
// Errors
//
type WebRPCError struct {
Name string `json:"error"`
Code int `json:"code"`
Message string `json:"msg"`
Cause string `json:"cause,omitempty"`
HTTPStatus int `json:"status"`
cause error
{{- if $opts.errorStackTrace }}
{{- /* The below field is reflect-compatible with golang.org/x/xerrors.*/}}
frame struct { frames [3]uintptr }
{{- end}}
}
var _ error = WebRPCError{}
func (e WebRPCError) Error() string {
if e.cause != nil {
return fmt.Sprintf("%s %d: %s: %v", e.Name, e.Code, e.Message, e.cause)
}
return fmt.Sprintf("%s %d: %s", e.Name, e.Code, e.Message)
}
func (e WebRPCError) Is(target error) bool {
if target == nil {
return false
}
if rpcErr, ok := target.(WebRPCError); ok {
return rpcErr.Code == e.Code
}
return errors.Is(e.cause, target)
}
func (e WebRPCError) Unwrap() error {
return e.cause
}
func (e WebRPCError) WithCause(cause error) WebRPCError {
err := e
err.cause = cause
err.Cause = cause.Error()
{{- if $opts.errorStackTrace }}
runtime.Callers(1, err.frame.frames[:])
{{- end}}
return err
}
func (e WebRPCError) WithCausef(format string, args ...interface{}) WebRPCError {
cause := fmt.Errorf(format, args...)
err := e
err.cause = cause
err.Cause = cause.Error()
{{- if $opts.errorStackTrace }}
runtime.Callers(1, err.frame.frames[:])
{{- end}}
return err
}
// Deprecated: Use .WithCause() method on WebRPCError.
func ErrorWithCause(rpcErr WebRPCError, cause error) WebRPCError {
return rpcErr.WithCause(cause)
}
{{- if $opts.errorStackTrace }}
func (e WebRPCError) StackFrames() []uintptr {
return e.frame.frames[:]
}
{{- end }}
// Webrpc errors
var (
{{- range $_, $error := $webrpcErrors}}
{{ printf "Err%s = WebRPCError{Code: %v, Name: %q, Message: %q, HTTPStatus: %v}" $error.Name $error.Code $error.Name $error.Message $error.HTTPStatus}}
{{- end}}
)
{{- if len $schemaErrors }}
// Schema errors
var (
{{- range $_, $error := $schemaErrors}}
{{ printf "Err%s = WebRPCError{Code: %v, Name: %q, Message: %q, HTTPStatus: %v}" $error.Name $error.Code $error.Name $error.Message $error.HTTPStatus}}
{{- end}}
)
{{ end -}}
{{- end -}}