-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patherrors.go
65 lines (56 loc) · 1.72 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
package apitoolkit
import (
"context"
"errors"
"log"
"reflect"
"time"
gerrors "github.com/go-errors/errors"
)
// ATError is the Apitoolkit error type/object
type ATError struct {
When time.Time `json:"when,omitempty"`
ErrorType string `json:"error_type,omitempty"`
RootErrorType string `json:"root_error_type,omitempty"`
Message string `json:"message,omitempty"`
RootErrorMessage string `json:"root_error_message,omitempty"`
StackTrace string `json:"stack_trace,omitempty"`
}
// ReportError Allows you to report an error from your server to APIToolkit.
// This error would be associated with a given request,
// and helps give a request more context especially when investigating incidents
func ReportError(ctx context.Context, err error) {
if err == nil {
return
}
errorList, ok := ctx.Value(ErrorListCtxKey).(*[]ATError)
if !ok {
log.Printf("APIToolkit: ErrorList context key was not found in the context. Is the middleware configured correctly? Error will not be notified. Error: %v \n", err)
return
}
*errorList = append(*errorList, BuildError(err))
}
func BuildError(err error) ATError {
errType := reflect.TypeOf(err).String()
rootError := rootCause(err)
rootErrorType := reflect.TypeOf(rootError).String()
errW := gerrors.Wrap(err, 2)
return ATError{
When: time.Now(),
ErrorType: errType,
RootErrorType: rootErrorType,
RootErrorMessage: rootError.Error(),
Message: errW.Error(),
StackTrace: errW.ErrorStack(),
}
}
// rootCause recursively unwraps an error and returns the original cause.
func rootCause(err error) error {
for {
cause := errors.Unwrap(err)
if cause == nil {
return err
}
err = cause
}
}