OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package errors | 5 package errors |
6 | 6 |
7 import ( | 7 import ( |
8 "errors" | |
8 "fmt" | 9 "fmt" |
9 "path/filepath" | 10 "path/filepath" |
10 "runtime" | 11 "runtime" |
11 ) | 12 ) |
12 | 13 |
14 // New is a pass-through version of the standard errors.New function. | |
M-A Ruel
2015/07/22 18:20:36
And what's the purpose?
dnj
2015/07/22 18:30:34
Guess: one can import "...luci-go/common/errors" a
M-A Ruel
2015/07/22 18:32:36
Oh right, I forgot about the package name aliasing
iannucci
2015/07/23 00:44:38
Right... I figured this was easier than fixing the
| |
15 var New = errors.New | |
16 | |
13 // MarkedError is the specific error type retuned by MakeMarkFn. It's designed | 17 // MarkedError is the specific error type retuned by MakeMarkFn. It's designed |
14 // so that you can access the underlying object if needed. | 18 // so that you can access the underlying object if needed. |
15 type MarkedError struct { | 19 type MarkedError struct { |
16 // Orig contains the original data (error or otherwise) which was passed to | 20 // Orig contains the original data (error or otherwise) which was passed to |
17 // the marker function. | 21 // the marker function. |
18 Orig interface{} | 22 Orig interface{} |
19 | 23 |
20 msgFmt string | 24 msgFmt string |
21 } | 25 } |
22 | 26 |
23 func (te *MarkedError) Error() string { | 27 func (te *MarkedError) Error() string { |
dnj
2015/07/23 01:03:37
Move to markederror.go
| |
24 return fmt.Sprintf(te.msgFmt, te.Orig) | 28 return fmt.Sprintf(te.msgFmt, te.Orig) |
25 } | 29 } |
26 | 30 |
27 // MakeMarkFn returns a new 'marker' function for your library. The name paramet er | 31 // MakeMarkFn returns a new 'marker' function for your library. The name paramet er |
28 // should probably match your package name, but it can by any string which | 32 // should probably match your package name, but it can by any string which |
29 // will help identify the error as originating from your package. | 33 // will help identify the error as originating from your package. |
30 func MakeMarkFn(name string) func(interface{}) error { | 34 func MakeMarkFn(name string) func(interface{}) error { |
31 fmtstring1 := name + " - %s:%d - %%+v" | 35 fmtstring1 := name + " - %s:%d - %%+v" |
32 fmtstring2 := name + " - %+v" | 36 fmtstring2 := name + " - %+v" |
33 return func(errish interface{}) error { | 37 return func(errish interface{}) error { |
34 if errish == nil { | 38 if errish == nil { |
35 return nil | 39 return nil |
36 } | 40 } |
37 _, filename, line, gotInfo := runtime.Caller(1) | 41 _, filename, line, gotInfo := runtime.Caller(1) |
38 pfx := fmtstring2 | 42 pfx := fmtstring2 |
39 if gotInfo { | 43 if gotInfo { |
40 pfx = fmt.Sprintf(fmtstring1, filepath.Base(filename), l ine) | 44 pfx = fmt.Sprintf(fmtstring1, filepath.Base(filename), l ine) |
41 } | 45 } |
42 return &MarkedError{errish, pfx} | 46 return &MarkedError{errish, pfx} |
43 } | 47 } |
44 } | 48 } |
OLD | NEW |