-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed.go
66 lines (54 loc) · 1.75 KB
/
embed.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
// Copyright (c) 2021, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package errors
import (
"fmt"
"golang.org/x/xerrors"
)
// An Embedder unwraps an embedded error.
type Embedder interface {
error
Unembed() error
}
// Unembed recursively unwraps all Embedder errors and returns the (original)
// error that was wrapped with extra context.
// If err is not an Embedder, Unembed returns err as provided.
func Unembed(err error) error {
//goland:noinspection GoTypeAssertionOnErrors
if u, ok := err.(Embedder); ok {
return Unembed(u.Unembed())
}
return err
}
type embedError struct {
error
stack *StackTrace
}
// StackTrace returns the StackTrace of this error or, if nil, tries to return
// the first non-nil StackTrace of an embedded error.
func (e *embedError) StackTrace() *StackTrace {
if e.stack == nil {
e.stack = GetStackTrace(Unembed(e.error))
}
return e.stack
}
// Unembed the underlying error.
func (e *embedError) Unembed() error { return e.error }
// Unwrap the underlying error.
func (e *embedError) Unwrap() error { return e.error }
// Format uses xerrors.FormatError to call the FormatError method of the error
// with a Printer configured according to s and v, and writes the result to s.
func (e *embedError) Format(s fmt.State, v rune) {
xerrors.FormatError(e, s, v)
}
// FormatError prints the error to the Printer using PrintError and returns the
// next error in the error chain, if any.
func (e *embedError) FormatError(p Printer) error {
PrintError(p, e)
return Unwrap(Unembed(e.error))
}
// GoString prints the error in basic Go syntax.
func (e *embedError) GoString() string {
return fmt.Sprintf("errors.embedError{error: %#v}", e.error)
}