OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package errors | |
6 | |
7 import ( | |
8 "fmt" | |
9 "path/filepath" | |
10 "runtime" | |
11 ) | |
12 | |
13 // MarkedError is the specific error type retuned by MakeMarkFn. It's designed | |
14 // so that you can access the underlying object if needed. | |
15 type MarkedError struct { | |
16 // Orig contains the original data (error or otherwise) which was passed
to | |
17 // the marker function. | |
18 Orig interface{} | |
19 | |
20 msgFmt string | |
21 } | |
22 | |
23 func (te *MarkedError) Error() string { | |
24 return fmt.Sprintf(te.msgFmt, te.Orig) | |
25 } | |
26 | |
27 // 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 | |
29 // will help identify the error as originating from your package. | |
30 func MakeMarkFn(name string) func(interface{}) error { | |
31 fmtstring1 := name + " - %s:%d - %%+v" | |
32 fmtstring2 := name + " - %+v" | |
33 return func(errish interface{}) error { | |
34 if errish == nil { | |
35 return nil | |
36 } | |
37 _, filename, line, gotInfo := runtime.Caller(1) | |
38 pfx := fmtstring2 | |
39 if gotInfo { | |
40 pfx = fmt.Sprintf(fmtstring1, filepath.Base(filename), l
ine) | |
41 } | |
42 return &MarkedError{errish, pfx} | |
43 } | |
44 } | |
OLD | NEW |