This repository has been archived by the owner on Aug 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
error.go
152 lines (130 loc) · 3.8 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package errors
import (
"bytes"
)
// errorT represents an error with a message and context.
type errorT struct {
ctx context
msg string
}
// Error implements the error interface.
func (e *errorT) Error() string {
var buf bytes.Buffer
buf.WriteString(e.msg)
e.ctx.writeToBuf(&buf)
return buf.String()
}
// With returns an error with additional key/value pairs attached.
// It implements the Error interface.
func (e *errorT) With(keyvals ...interface{}) Error {
return e.withKeyvals(keyvals)
}
// MarshalText implements the TextMarshaler interface.
func (e *errorT) MarshalText() ([]byte, error) {
return []byte(e.Error()), nil
}
// Keyvals returns the contents of the error
// as an array of alternating keys and values.
func (e *errorT) Keyvals() []interface{} {
var keyvals []interface{}
keyvals = append(keyvals, "msg", e.msg)
keyvals = e.ctx.appendKeyvals(keyvals)
return keyvals
}
func (e *errorT) withKeyvals(keyvals []interface{}) *errorT {
return &errorT{
ctx: e.ctx.withKeyvals(keyvals),
msg: e.msg,
}
}
// causeT represents an error with a message, context, and an error which
// contains the original cause of the error condition.
type causeT struct {
*errorT
cause error
}
// Error implements the error interface.
func (c *causeT) Error() string {
var buf bytes.Buffer
buf.WriteString(c.msg)
c.ctx.writeToBuf(&buf)
buf.WriteString(": ")
buf.WriteString(c.cause.Error())
return buf.String()
}
// With returns an error with additional key/value pairs attached.
// It implements the Error interface.
func (c *causeT) With(keyvals ...interface{}) Error {
return &causeT{
errorT: c.errorT.withKeyvals(keyvals),
cause: c.cause,
}
}
// MarshalText implements the TextMarshaler interface.
func (c *causeT) MarshalText() ([]byte, error) {
return []byte(c.Error()), nil
}
// Cause implements the causer interface, and is compatible with
// the github.com/pkg/errors package.
func (c *causeT) Cause() error {
return c.cause
}
// Unwrap implements the Wrapper interface defined in the
// Go 2 draft designs for error inspection and printing.
func (c *causeT) Unwrap() error {
return c.cause
}
// Keyvals returns the contents of the error
// as an array of alternating keys and values.
func (c *causeT) Keyvals() []interface{} {
keyvals := c.errorT.Keyvals()
// TODO(jpj): this might be improved by checking if cause
// implements keyvalser, and appending keyvals.
keyvals = append(keyvals, "cause", c.cause.Error())
return keyvals
}
// attachT represents an error that has additional keyword/value pairs
// attached to it.
type attachT struct {
ctx context
cause error
}
// Error implements the error interface.
func (a *attachT) Error() string {
var buf bytes.Buffer
buf.WriteString(a.cause.Error())
a.ctx.writeToBuf(&buf)
return buf.String()
}
// With returns an error with additional key/value pairs attached.
// It implements the Error interface.
func (a *attachT) With(keyvals ...interface{}) Error {
return &attachT{
ctx: a.ctx.withKeyvals(keyvals),
cause: a.cause,
}
}
// MarshalText implements the TextMarshaler interface.
func (a *attachT) MarshalText() ([]byte, error) {
return []byte(a.Error()), nil
}
// Cause implements the causer interface, and is compatible with
// the github.com/pkg/errors package.
func (a *attachT) Cause() error {
return a.cause
}
// Unwrap implements the Wrapper interface defined in the
// Go 2 draft designs for error inspection and printing.
func (a *attachT) Unwrap() error {
return a.cause
}
// Keyvals returns the contents of the error
// as an array of alternating keys and values.
func (a *attachT) Keyvals() []interface{} {
var keyvals []interface{}
// TODO(jpj): this could be improved by checking if the
// cause implements the keyvalser interface.
keyvals = append(keyvals, "msg", a.cause.Error())
keyvals = a.ctx.appendKeyvals(keyvals)
return keyvals
}