/
wrapper.go
49 lines (42 loc) · 1.18 KB
/
wrapper.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
// Code generated by server_generator. DO NOT EDIT.
// generated version: devel
package wrapper
import (
"fmt"
"github.com/go-generalize/api_gen/server_generator/sample/wrapper/internal"
)
// APIError - API Error
type APIError struct {
Status int `json:"status"`
Body interface{} `json:"body"`
err error `json:"-"`
}
// NewAPIError - constructor
func NewAPIError(status int, bodies ...interface{}) *APIError {
ae := &APIError{Status: status}
if length := len(bodies); length > 0 {
if length == 1 {
ae.Body = bodies[0]
} else {
ae.Body = bodies
}
}
return ae
}
// Error - makes it compatible with `error` interface.
func (ae *APIError) Error() string {
if ae.err == nil {
return fmt.Sprintf("status=%d, body=%v", ae.Status, ae.Body)
}
return fmt.Sprintf("status=%d, body=%v, err=%+v", ae.Status, ae.Body, ae.err)
}
// SetError - sets error to APIError.err
func (ae *APIError) SetError(err error) *APIError {
ae.err = internal.Errorf(": %w", err)
return ae
}
// SetErrorf - sets error to APIError.err (use `%w` to wrap the error)
func (ae *APIError) SetErrorf(format string, a ...interface{}) *APIError {
ae.err = internal.Errorf(format, a...)
return ae
}