Skip to content

Commit d165b61

Browse files
authored
implement Trace method (#11)
1 parent e8da389 commit d165b61

1 file changed

Lines changed: 49 additions & 38 deletions

File tree

err.go

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ func New(code std.Code, msg string, data ...interface{}) Err {
3030
}
3131

3232
/*
33-
Trace returns the call stack.
33+
Caller returns the most recent error caller.
3434
*/
35-
func (errs Err) Trace() std.Trace {
36-
var callers std.Trace
37-
for _, msg := range errs {
38-
callers = append(callers, msg.Caller())
35+
func (errs Err) Caller() std.Caller {
36+
var caller std.Caller
37+
if len(errs) > 0 {
38+
caller = errs[len(errs)-1].Caller()
3939
}
40-
return callers
40+
return caller
4141
}
4242

4343
/*
@@ -88,38 +88,6 @@ func (errs Err) Error() string {
8888
return str
8989
}
9090

91-
/*
92-
HTTPStatus returns the associated HTTP status code, if any. Otherwise,
93-
returns 200.
94-
*/
95-
func (errs Err) HTTPStatus() int {
96-
status := http.StatusOK
97-
if len(errs) > 0 {
98-
if code, ok := Codes[errs[len(errs)-1].Code()]; ok {
99-
status = code.HTTPStatus()
100-
}
101-
}
102-
return status
103-
}
104-
105-
/*
106-
Msg returns the error message.
107-
*/
108-
func (errs Err) Msg() string {
109-
str := ""
110-
if len(errs) > 0 {
111-
str = errs[len(errs)-1].Msg()
112-
}
113-
return str
114-
}
115-
116-
/*
117-
String implements the stringer and Coder interfaces.
118-
*/
119-
func (errs Err) String() string {
120-
return fmt.Sprintf("%s", errs)
121-
}
122-
12391
/*
12492
Format implements fmt.Formatter. https://golang.org/pkg/fmt/#hdr-Printing
12593
@@ -228,6 +196,49 @@ func From(code std.Code, err error) Err {
228196
return err.(Err)
229197
}
230198

199+
/*
200+
HTTPStatus returns the associated HTTP status code, if any. Otherwise,
201+
returns 200.
202+
*/
203+
func (errs Err) HTTPStatus() int {
204+
status := http.StatusOK
205+
if len(errs) > 0 {
206+
if code, ok := Codes[errs[len(errs)-1].Code()]; ok {
207+
status = code.HTTPStatus()
208+
}
209+
}
210+
return status
211+
}
212+
213+
/*
214+
Msg returns the error message.
215+
*/
216+
func (errs Err) Msg() string {
217+
str := ""
218+
if len(errs) > 0 {
219+
str = errs[len(errs)-1].Msg()
220+
}
221+
return str
222+
}
223+
224+
/*
225+
String implements the stringer and Coder interfaces.
226+
*/
227+
func (errs Err) String() string {
228+
return fmt.Sprintf("%s", errs)
229+
}
230+
231+
/*
232+
Trace returns the call stack.
233+
*/
234+
func (errs Err) Trace() std.Trace {
235+
var callers std.Trace
236+
for _, msg := range errs {
237+
callers = append(callers, msg.Caller())
238+
}
239+
return callers
240+
}
241+
231242
/*
232243
With adds a new error to the stack without changing the leading cause.
233244
*/

0 commit comments

Comments
 (0)