-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
132 lines (109 loc) · 2.44 KB
/
errors.go
File metadata and controls
132 lines (109 loc) · 2.44 KB
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
/**
* Copyright 2019 Innodev LLC. 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 (
"encoding/json"
"errors"
"fmt"
)
type Frame struct {
Function string `json:"function,omitempty"`
File string `json:"file,omitempty"`
Line int `json:"line,omitempty"`
}
func (frame Frame) String() string {
return fmt.Sprintf("\"%s:%d %s()\"", frame.File, frame.Line, frame.Function)
}
const StackBufferSize = 100
type Error interface {
Error() string
WithMeta(meta map[string]interface{}) Error
WithStack() Error
Is(err error) bool
MarshalJSON() ([]byte, error)
Unwrap() error
}
type Err struct {
Err error `json:"error"`
Message string `json:"message"`
Stack []Frame `json:"stack"`
Meta map[string]interface{} `json:"meta"`
}
func Plain(e interface{}) Error {
return _new(e, -1)
}
func New(e interface{}) Error {
return _new(e, 2)
}
func Wrap(err error, message string) Error {
return _wrap(err, message)
}
func Wrapf(err error, format string, args ...interface{}) error {
return _wrap(err, fmt.Sprintf(format, args...))
}
// Standard library passthroughs to allow for use as drop-in replacement
func As(err error, target interface{}) bool {
return errors.As(err, target)
}
func Is(err, target error) bool {
return errors.Is(err, target)
}
func Unwrap(err error) error {
return errors.Unwrap(err)
}
func (err *Err) Error() string {
if err == nil {
return "<nil>"
}
if err.Err == nil {
return err.Message
}
return err.Err.Error() + ":" + err.Message
}
func (err Err) WithMeta(meta map[string]interface{}) Error {
out := _copy(err)
if out.Meta == nil {
out.Meta = map[string]interface{}{}
}
for k, v := range meta {
out.Meta[k] = v
}
return out
}
func (err Err) WithStack() Error {
out := _copy(err)
out.Stack = getStack(1)
return out
}
func (err Err) Is(err2 error) bool {
if err.Message == err2.Error() {
return true
}
return errors.Is(err.Err, err2)
}
func (err *Err) MarshalJSON() ([]byte, error) {
if err == nil {
return json.Marshal(nil)
}
toJson := map[string]interface{}{
"error": err.Error(),
}
if len(err.Stack) > 0 {
toJson["stack"] = err.Stack
}
if len(err.Meta) > 0 {
toJson["meta"] = err.Meta
}
return json.Marshal(toJson)
}
func (err *Err) Unwrap() error {
return err.Err
}
func MustNotError(err error) {
if err != nil {
panic(err)
}
}