This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sanitize_error.go
69 lines (59 loc) · 1.71 KB
/
sanitize_error.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
package api
import (
"fmt"
"github.com/Worldcoin/hubble-commander/bls"
"github.com/Worldcoin/hubble-commander/db"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type InternalToAPIError struct {
apiError *APIError
commanderErrors []error
}
func NewInternalToAPIError(code int, message string, commanderErrors []error) *InternalToAPIError {
return &InternalToAPIError{
apiError: NewAPIError(code, message),
commanderErrors: commanderErrors,
}
}
var commonErrors = []*InternalToAPIError{
// Badger
NewInternalToAPIError(
40000,
"an error occurred while saving data to the Badger database",
[]error{
db.ErrInconsistentItemsLength,
db.ErrInvalidKeyListLength,
db.ErrInvalidKeyListMetadataLength,
},
),
NewInternalToAPIError(40001, "an error occurred while iterating over Badger database", []error{db.ErrIteratorFinished}),
// BLS
NewInternalToAPIError(99004, "an error occurred while fetching the domain for signing", []error{bls.ErrInvalidDomainLength}),
}
func sanitizeError(err error, errMap map[error]*APIError) *APIError {
if logrus.IsLevelEnabled(logrus.DebugLevel) {
logrus.Debugf("Sanitizing error:\n%+v", err)
}
for k, v := range errMap {
if errors.Is(err, k) {
if logrus.IsLevelEnabled(logrus.DebugLevel) {
v.Data = fmt.Sprintf("%+v", err)
}
return v
}
}
return sanitizeCommonError(err, commonErrors)
}
func sanitizeCommonError(err error, errMap []*InternalToAPIError) *APIError {
for i := range errMap {
selectedErrMap := errMap[i]
for j := range selectedErrMap.commanderErrors {
commanderErr := selectedErrMap.commanderErrors[j]
if errors.Is(err, commanderErr) {
return errMap[i].apiError
}
}
}
return NewUnknownAPIError(err)
}