-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
69 lines (62 loc) · 1.88 KB
/
errors.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
// Copyright 2020 The Reddico Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package krangio
import (
"encoding/json"
"github.com/ainsleyclark/errors"
)
type (
// Error represents an error that occurred during the
// processing of a Krang Lambda function.
Error struct {
Err *errors.Error `json:"error" bson:"error"`
Service string `json:"service" bson:"service"` // Currently running function, for example "scrape"
}
// marshalError is the error sent when a function
// or service failed.
marshalError struct {
Error wrappingError `json:"error" bson:"error"`
Service string `json:"service" bson:"service"`
}
// wrappingError is the wrapping error features the error
// and file line in strings suitable for json.Marshal
wrappingError struct {
Code string `json:"code" bson:"code"`
Message string `json:"message" bson:"message"`
Operation string `json:"operation" bson:"operation"`
Err string `json:"error" bson:"error"`
FileLine string `json:"file_line" bson:"file_line"`
}
)
// marshal is an alias of json.Marshal
var marshal = json.Marshal
// NewError returns a new Lambda error.
func NewError(err error, service string) *Error {
return &Error{
Err: errors.ToError(err),
Service: service,
}
}
// Error returns the JSON representation of the error
// message by implementing the error interface.
func (e *Error) Error() string {
b, err := marshal(e)
if err != nil {
return "error marshalling lambda error"
}
return string(b[:])
}
// ToMap returns a map of the error if there is one.
func (e *Error) ToMap() map[string]any {
if e == nil || e.Err == nil {
return nil
}
return map[string]any{
"code": e.Err.Code,
"message": e.Err.Message,
"operation": e.Err.Operation,
"error": e.Err.Error(),
"file_line": e.Err.FileLine(),
}
}