-
Notifications
You must be signed in to change notification settings - Fork 0
/
with_secondary.go
90 lines (77 loc) · 2.81 KB
/
with_secondary.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
83
84
85
86
87
88
89
90
// Copyright 2019 The Cockroach Authors.
//
// 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 secondary
import (
"context"
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/interspace/errors/errbase"
)
type withSecondaryError struct {
cause error
// secondaryError is an additional error payload that provides
// additional context towards troubleshooting.
secondaryError error
}
var _ error = (*withSecondaryError)(nil)
var _ errbase.SafeDetailer = (*withSecondaryError)(nil)
var _ fmt.Formatter = (*withSecondaryError)(nil)
var _ errbase.Formatter = (*withSecondaryError)(nil)
// SafeDetails reports the PII-free details from the secondary error.
func (e *withSecondaryError) SafeDetails() []string {
var details []string
for err := e.secondaryError; err != nil; err = errbase.UnwrapOnce(err) {
sd := errbase.GetSafeDetails(err)
details = sd.Fill(details)
}
return details
}
// Printing a withSecondary reveals the details.
func (e *withSecondaryError) Format(s fmt.State, verb rune) { errbase.FormatError(e, s, verb) }
func (e *withSecondaryError) FormatError(p errbase.Printer) (next error) {
if p.Detail() {
p.Print("secondary error attachment\n")
p.Printf("%+v", e.secondaryError)
}
return e.cause
}
func (e *withSecondaryError) Error() string { return e.cause.Error() }
func (e *withSecondaryError) Cause() error { return e.cause }
func (e *withSecondaryError) Unwrap() error { return e.cause }
func encodeWithSecondaryError(ctx context.Context, err error) (string, []string, proto.Message) {
e := err.(*withSecondaryError)
enc := errbase.EncodeError(ctx, e.secondaryError)
return "", nil, &enc
}
func decodeWithSecondaryError(
ctx context.Context, cause error, _ string, _ []string, payload proto.Message,
) error {
enc, ok := payload.(*errbase.EncodedError)
if !ok {
// If this ever happens, this means some version of the library
// (presumably future) changed the payload type, and we're
// receiving this here. In this case, give up and let
// DecodeError use the opaque type.
return nil
}
return &withSecondaryError{
cause: cause,
secondaryError: errbase.DecodeError(ctx, *enc),
}
}
func init() {
tn := errbase.GetTypeKey((*withSecondaryError)(nil))
errbase.RegisterWrapperDecoder(tn, decodeWithSecondaryError)
errbase.RegisterWrapperEncoder(tn, encodeWithSecondaryError)
}