-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandlers.go
82 lines (68 loc) · 1.61 KB
/
handlers.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package g8
import (
"fmt"
"net/http"
"strings"
newrelic "github.com/newrelic/go-agent"
"github.com/rotisserie/eris"
"github.com/rs/zerolog"
)
const (
headerBuildVersion = "Build-Version"
headerCorrelationID = "Correlation-Id"
)
type HandlerConfig struct {
AppName string
FunctionName string
EnvName string
BuildVersion string
Logger zerolog.Logger
NewRelicApp newrelic.Application
}
type LambdaResult interface{}
type StepEvent interface{}
type Validatable interface {
Validate() error
}
type Err struct {
Status int `json:"-"`
Code string `json:"code"`
Detail string `json:"detail"`
}
func (err Err) Error() string {
errorParts := []string{
fmt.Sprintf("Code: %s; Status: %d; Detail: %s", err.Code, err.Status, err.Detail),
}
return strings.Join(errorParts, "; ")
}
var ErrInternalServer = Err{
Status: http.StatusInternalServerError,
Code: "INTERNAL_SERVER_ERROR",
Detail: "Internal server error",
}
var ErrInvalidBody = Err{
Status: http.StatusBadRequest,
Code: "INVALID_REQUEST_BODY",
Detail: "Invalid request body",
}
func ErrValidation(detail string) Err {
return Err{
Status: http.StatusBadRequest,
Code: "VALIDATION_ERROR",
Detail: detail,
}
}
func configureLogger(conf HandlerConfig) zerolog.Context {
return conf.Logger.With().
Str("application", conf.AppName).
Str("function_name", conf.FunctionName).
Str("env", conf.EnvName).
Str("build_version", conf.BuildVersion)
}
func logUnhandledError(logger zerolog.Logger, err error) {
logger.Error().
Fields(map[string]interface{}{
"error": eris.ToJSON(err, true),
}).
Msg("Unhandled error")
}