-
Notifications
You must be signed in to change notification settings - Fork 14
/
defaultcallback.go
62 lines (49 loc) · 1.35 KB
/
defaultcallback.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
package common
import (
"context"
"net/http"
"time"
"github.com/anz-bank/sysl-go/validator"
"github.com/go-chi/chi"
)
func DefaultCallback() Callback {
return Callback{
UpstreamTimeout: 120 * time.Second,
DownstreamTimeout: 120 * time.Second,
RouterBasePath: "/",
UpstreamConfig: Config{},
}
}
type Callback struct {
UpstreamTimeout time.Duration
DownstreamTimeout time.Duration
RouterBasePath string
UpstreamConfig validator.Validator
MapErrorFunc func(ctx context.Context, err error) *HTTPError
}
type Config struct{}
func (c Config) Validate() error {
return nil
}
func (g Callback) AddMiddleware(ctx context.Context, r chi.Router) {
}
func (g Callback) BasePath() string {
return g.RouterBasePath
}
func (g Callback) Config() interface{} {
return g.UpstreamConfig
}
func (g Callback) HandleError(ctx context.Context, w http.ResponseWriter, kind Kind, message string, cause error) {
se := CreateError(ctx, kind, message, cause)
g.MapError(ctx, se).WriteError(ctx, w)
}
func (g Callback) DownstreamTimeoutContext(ctx context.Context) (context.Context, context.CancelFunc) {
return context.WithTimeout(ctx, g.DownstreamTimeout)
}
func (g Callback) MapError(ctx context.Context, err error) *HTTPError {
if g.MapErrorFunc == nil {
httpErr := MapError(ctx, err)
return &httpErr
}
return g.MapErrorFunc(ctx, err)
}