/
handler.go
59 lines (49 loc) · 1.54 KB
/
handler.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
// Copyright © 2019 Banzai Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package errorhandler
import (
"emperror.dev/emperror"
"github.com/banzaicloud/allspark/internal/platform/log"
)
type Handler struct {
logger log.Logger
}
// NewHandler returns a handler which logs errors using the platform logger
func NewHandler(logger log.Logger) *Handler {
return &Handler{logger: logger}
}
// Handle logs an error
func (h *Handler) Handle(err error) {
var ctx map[string]interface{}
// Extract context from the error and attach it to the log
if kvs := emperror.Context(err); len(kvs) > 0 {
ctx = ToMap(kvs)
}
logger := h.logger.WithFields(log.Fields(ctx))
type errorCollection interface {
Errors() []error
}
if errs, ok := err.(errorCollection); ok {
for _, e := range errs.Errors() {
ctx = nil
// Extract context from the error and attach it to the log
if kvs := emperror.Context(e); len(kvs) > 0 {
ctx = ToMap(kvs)
}
h.logger.WithFields(log.Fields(ctx)).Error(e.Error())
}
} else {
logger.Error(err.Error())
}
}