Skip to content

Commit ec907ab

Browse files
author
Dean Karn
committed
Add license
1 parent 453654e commit ec907ab

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Dean Karn
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

errors.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ package errors
44
// a stack trace.
55
func Wrap(err error, prefix string) *Wrapped {
66
if w, ok := err.(*Wrapped); ok {
7-
w.errors = append(w.errors, newWrapped(err, prefix))
7+
w.Errors = append(w.Errors, newWrapped(err, prefix))
88
return w
99
}
1010
return &Wrapped{
11-
errors: []*Wrapped{newWrapped(err, prefix)},
11+
Errors: []*Wrapped{newWrapped(err, prefix)},
1212
}
1313
}
1414

@@ -19,9 +19,9 @@ func HasType(err error, typ string) bool {
1919
if !ok {
2020
return false
2121
}
22-
for i := len(w.errors) - 1; i >= 0; i-- {
23-
for j := 0; j < len(w.errors[i].Types); j++ {
24-
if w.errors[i].Types[j] == typ {
22+
for i := len(w.Errors) - 1; i >= 0; i-- {
23+
for j := 0; j < len(w.Errors[i].Types); j++ {
24+
if w.Errors[i].Types[j] == typ {
2525
return true
2626
}
2727
}
@@ -32,7 +32,12 @@ func HasType(err error, typ string) bool {
3232
// Cause extracts and returns the root error
3333
func Cause(err error) error {
3434
if w, ok := err.(*Wrapped); ok {
35-
return w.errors[0]
35+
// if root level error
36+
if len(w.Errors) > 0 {
37+
return w.Errors[0]
38+
}
39+
// already extracted error
40+
return w
3641
}
3742
return err
3843
}
@@ -42,8 +47,8 @@ func Cause(err error) error {
4247
func IsErr(err, errType error) bool {
4348
if w, ok := err.(*Wrapped); ok {
4449
// if root level error
45-
if len(w.errors) > 0 {
46-
return w.errors[0].Err == errType
50+
if len(w.Errors) > 0 {
51+
return w.Errors[0].Err == errType
4752
}
4853
// already extracted error
4954
return w.Err == errType

stack.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,11 @@ func Stack() Frame {
148148
s := callers()
149149
return fr(s, 0)
150150
}
151+
152+
// StackLevel returns a stack from for parsing into a trace line
153+
// this is primarily used by other libraries who use this pacakge
154+
// internally as the level needs to be adjusted.
155+
func StackLevel(level int) Frame {
156+
s := callers()
157+
return fr(s, level)
158+
}

wrapped.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func newWrapped(err error, prefix string) *Wrapped {
2929
// which case it only contains an array of errors
3030
type Wrapped struct {
3131
// hidden field with wrapped errors, will expose a helper method to get this
32-
errors []*Wrapped
32+
Errors []*Wrapped
3333

3434
// Err is the wrapped error, either the original or already wrapped
3535
Err error
@@ -49,11 +49,11 @@ type Wrapped struct {
4949

5050
// Error returns the formatted error string
5151
func (w *Wrapped) Error() string {
52-
if len(w.errors) > 0 {
53-
lines := make([]string, 0, len(w.errors))
52+
if len(w.Errors) > 0 {
53+
lines := make([]string, 0, len(w.Errors))
5454
// source=<source> <prefix>: <error> tag=value tag2=value2 types=type1,type2
55-
for i := len(w.errors) - 1; i >= 0; i-- {
56-
line := w.errors[i].parseLine()
55+
for i := len(w.Errors) - 1; i >= 0; i-- {
56+
line := w.Errors[i].parseLine()
5757
lines = append(lines, line)
5858
}
5959
return strings.Join(lines, "\n")
@@ -80,7 +80,7 @@ func (w *Wrapped) parseLine() string {
8080

8181
// helper method to get the current *Wrapped from the top level
8282
func (w *Wrapped) current() *Wrapped {
83-
return w.errors[len(w.errors)-1]
83+
return w.Errors[len(w.Errors)-1]
8484
}
8585

8686
// WithTags allows the addition of multiple tags

0 commit comments

Comments
 (0)