-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.go
54 lines (48 loc) · 1.21 KB
/
client.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
// Package httperrors ...
// CODE GENERATED AUTOMATICALLY
// DO NOT EDIT
package httperrors
import (
"context"
"net/http"
"strings"
"github.com/valyala/fasthttp"
)
// ClientErrorProcessor ...
type ClientErrorProcessor struct {
defaultCode int
defaultMessage string
}
// Encode writes a svc error to the given http.ResponseWriter.
func (e *ClientErrorProcessor) Encode(ctx context.Context, r *fasthttp.Response, err error) {
code := e.defaultCode
message := e.defaultMessage
if err, ok := err.(errorCode); ok {
if err.StatusCode() != e.defaultCode {
code = err.StatusCode()
message = err.Error()
}
}
r.SetStatusCode(code)
r.SetBodyString(message)
return
}
// Decode reads a Service error from the given *http.Response.
func (e *ClientErrorProcessor) Decode(r *fasthttp.Response) error {
msgBytes := r.Body()
msg := strings.TrimSpace(string(msgBytes))
if msg == "" {
msg = http.StatusText(r.StatusCode())
}
return &httpError{
Code: r.StatusCode(),
Message: msg,
}
}
// NewClientErrorProcessor ...
func NewClientErrorProcessor(defaultCode int, defaultMessage string) *ClientErrorProcessor {
return &ClientErrorProcessor{
defaultCode: defaultCode,
defaultMessage: defaultMessage,
}
}