You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

535 lines
12 KiB

  1. # errors
  2. import "github.com/juju/errors"
  3. [![GoDoc](https://godoc.org/github.com/juju/errors?status.svg)](https://godoc.org/github.com/juju/errors)
  4. The juju/errors provides an easy way to annotate errors without losing the
  5. orginal error context.
  6. The exported `New` and `Errorf` functions are designed to replace the
  7. `errors.New` and `fmt.Errorf` functions respectively. The same underlying
  8. error is there, but the package also records the location at which the error
  9. was created.
  10. A primary use case for this library is to add extra context any time an
  11. error is returned from a function.
  12. if err := SomeFunc(); err != nil {
  13. return err
  14. }
  15. This instead becomes:
  16. if err := SomeFunc(); err != nil {
  17. return errors.Trace(err)
  18. }
  19. which just records the file and line number of the Trace call, or
  20. if err := SomeFunc(); err != nil {
  21. return errors.Annotate(err, "more context")
  22. }
  23. which also adds an annotation to the error.
  24. When you want to check to see if an error is of a particular type, a helper
  25. function is normally exported by the package that returned the error, like the
  26. `os` package does. The underlying cause of the error is available using the
  27. `Cause` function.
  28. os.IsNotExist(errors.Cause(err))
  29. The result of the `Error()` call on an annotated error is the annotations joined
  30. with colons, then the result of the `Error()` method for the underlying error
  31. that was the cause.
  32. err := errors.Errorf("original")
  33. err = errors.Annotatef(err, "context")
  34. err = errors.Annotatef(err, "more context")
  35. err.Error() -> "more context: context: original"
  36. Obviously recording the file, line and functions is not very useful if you
  37. cannot get them back out again.
  38. errors.ErrorStack(err)
  39. will return something like:
  40. first error
  41. github.com/juju/errors/annotation_test.go:193:
  42. github.com/juju/errors/annotation_test.go:194: annotation
  43. github.com/juju/errors/annotation_test.go:195:
  44. github.com/juju/errors/annotation_test.go:196: more context
  45. github.com/juju/errors/annotation_test.go:197:
  46. The first error was generated by an external system, so there was no location
  47. associated. The second, fourth, and last lines were generated with Trace calls,
  48. and the other two through Annotate.
  49. Sometimes when responding to an error you want to return a more specific error
  50. for the situation.
  51. if err := FindField(field); err != nil {
  52. return errors.Wrap(err, errors.NotFoundf(field))
  53. }
  54. This returns an error where the complete error stack is still available, and
  55. `errors.Cause()` will return the `NotFound` error.
  56. ## func AlreadyExistsf
  57. ``` go
  58. func AlreadyExistsf(format string, args ...interface{}) error
  59. ```
  60. AlreadyExistsf returns an error which satisfies IsAlreadyExists().
  61. ## func Annotate
  62. ``` go
  63. func Annotate(other error, message string) error
  64. ```
  65. Annotate is used to add extra context to an existing error. The location of
  66. the Annotate call is recorded with the annotations. The file, line and
  67. function are also recorded.
  68. For example:
  69. if err := SomeFunc(); err != nil {
  70. return errors.Annotate(err, "failed to frombulate")
  71. }
  72. ## func Annotatef
  73. ``` go
  74. func Annotatef(other error, format string, args ...interface{}) error
  75. ```
  76. Annotatef is used to add extra context to an existing error. The location of
  77. the Annotate call is recorded with the annotations. The file, line and
  78. function are also recorded.
  79. For example:
  80. if err := SomeFunc(); err != nil {
  81. return errors.Annotatef(err, "failed to frombulate the %s", arg)
  82. }
  83. ## func Cause
  84. ``` go
  85. func Cause(err error) error
  86. ```
  87. Cause returns the cause of the given error. This will be either the
  88. original error, or the result of a Wrap or Mask call.
  89. Cause is the usual way to diagnose errors that may have been wrapped by
  90. the other errors functions.
  91. ## func DeferredAnnotatef
  92. ``` go
  93. func DeferredAnnotatef(err *error, format string, args ...interface{})
  94. ```
  95. DeferredAnnotatef annotates the given error (when it is not nil) with the given
  96. format string and arguments (like fmt.Sprintf). If *err is nil, DeferredAnnotatef
  97. does nothing. This method is used in a defer statement in order to annotate any
  98. resulting error with the same message.
  99. For example:
  100. defer DeferredAnnotatef(&err, "failed to frombulate the %s", arg)
  101. ## func Details
  102. ``` go
  103. func Details(err error) string
  104. ```
  105. Details returns information about the stack of errors wrapped by err, in
  106. the format:
  107. [{filename:99: error one} {otherfile:55: cause of error one}]
  108. This is a terse alternative to ErrorStack as it returns a single line.
  109. ## func ErrorStack
  110. ``` go
  111. func ErrorStack(err error) string
  112. ```
  113. ErrorStack returns a string representation of the annotated error. If the
  114. error passed as the parameter is not an annotated error, the result is
  115. simply the result of the Error() method on that error.
  116. If the error is an annotated error, a multi-line string is returned where
  117. each line represents one entry in the annotation stack. The full filename
  118. from the call stack is used in the output.
  119. first error
  120. github.com/juju/errors/annotation_test.go:193:
  121. github.com/juju/errors/annotation_test.go:194: annotation
  122. github.com/juju/errors/annotation_test.go:195:
  123. github.com/juju/errors/annotation_test.go:196: more context
  124. github.com/juju/errors/annotation_test.go:197:
  125. ## func Errorf
  126. ``` go
  127. func Errorf(format string, args ...interface{}) error
  128. ```
  129. Errorf creates a new annotated error and records the location that the
  130. error is created. This should be a drop in replacement for fmt.Errorf.
  131. For example:
  132. return errors.Errorf("validation failed: %s", message)
  133. ## func IsAlreadyExists
  134. ``` go
  135. func IsAlreadyExists(err error) bool
  136. ```
  137. IsAlreadyExists reports whether the error was created with
  138. AlreadyExistsf() or NewAlreadyExists().
  139. ## func IsNotFound
  140. ``` go
  141. func IsNotFound(err error) bool
  142. ```
  143. IsNotFound reports whether err was created with NotFoundf() or
  144. NewNotFound().
  145. ## func IsNotImplemented
  146. ``` go
  147. func IsNotImplemented(err error) bool
  148. ```
  149. IsNotImplemented reports whether err was created with
  150. NotImplementedf() or NewNotImplemented().
  151. ## func IsNotSupported
  152. ``` go
  153. func IsNotSupported(err error) bool
  154. ```
  155. IsNotSupported reports whether the error was created with
  156. NotSupportedf() or NewNotSupported().
  157. ## func IsNotValid
  158. ``` go
  159. func IsNotValid(err error) bool
  160. ```
  161. IsNotValid reports whether the error was created with NotValidf() or
  162. NewNotValid().
  163. ## func IsUnauthorized
  164. ``` go
  165. func IsUnauthorized(err error) bool
  166. ```
  167. IsUnauthorized reports whether err was created with Unauthorizedf() or
  168. NewUnauthorized().
  169. ## func Mask
  170. ``` go
  171. func Mask(other error) error
  172. ```
  173. Mask hides the underlying error type, and records the location of the masking.
  174. ## func Maskf
  175. ``` go
  176. func Maskf(other error, format string, args ...interface{}) error
  177. ```
  178. Mask masks the given error with the given format string and arguments (like
  179. fmt.Sprintf), returning a new error that maintains the error stack, but
  180. hides the underlying error type. The error string still contains the full
  181. annotations. If you want to hide the annotations, call Wrap.
  182. ## func New
  183. ``` go
  184. func New(message string) error
  185. ```
  186. New is a drop in replacement for the standard libary errors module that records
  187. the location that the error is created.
  188. For example:
  189. return errors.New("validation failed")
  190. ## func NewAlreadyExists
  191. ``` go
  192. func NewAlreadyExists(err error, msg string) error
  193. ```
  194. NewAlreadyExists returns an error which wraps err and satisfies
  195. IsAlreadyExists().
  196. ## func NewNotFound
  197. ``` go
  198. func NewNotFound(err error, msg string) error
  199. ```
  200. NewNotFound returns an error which wraps err that satisfies
  201. IsNotFound().
  202. ## func NewNotImplemented
  203. ``` go
  204. func NewNotImplemented(err error, msg string) error
  205. ```
  206. NewNotImplemented returns an error which wraps err and satisfies
  207. IsNotImplemented().
  208. ## func NewNotSupported
  209. ``` go
  210. func NewNotSupported(err error, msg string) error
  211. ```
  212. NewNotSupported returns an error which wraps err and satisfies
  213. IsNotSupported().
  214. ## func NewNotValid
  215. ``` go
  216. func NewNotValid(err error, msg string) error
  217. ```
  218. NewNotValid returns an error which wraps err and satisfies IsNotValid().
  219. ## func NewUnauthorized
  220. ``` go
  221. func NewUnauthorized(err error, msg string) error
  222. ```
  223. NewUnauthorized returns an error which wraps err and satisfies
  224. IsUnauthorized().
  225. ## func NotFoundf
  226. ``` go
  227. func NotFoundf(format string, args ...interface{}) error
  228. ```
  229. NotFoundf returns an error which satisfies IsNotFound().
  230. ## func NotImplementedf
  231. ``` go
  232. func NotImplementedf(format string, args ...interface{}) error
  233. ```
  234. NotImplementedf returns an error which satisfies IsNotImplemented().
  235. ## func NotSupportedf
  236. ``` go
  237. func NotSupportedf(format string, args ...interface{}) error
  238. ```
  239. NotSupportedf returns an error which satisfies IsNotSupported().
  240. ## func NotValidf
  241. ``` go
  242. func NotValidf(format string, args ...interface{}) error
  243. ```
  244. NotValidf returns an error which satisfies IsNotValid().
  245. ## func Trace
  246. ``` go
  247. func Trace(other error) error
  248. ```
  249. Trace adds the location of the Trace call to the stack. The Cause of the
  250. resulting error is the same as the error parameter. If the other error is
  251. nil, the result will be nil.
  252. For example:
  253. if err := SomeFunc(); err != nil {
  254. return errors.Trace(err)
  255. }
  256. ## func Unauthorizedf
  257. ``` go
  258. func Unauthorizedf(format string, args ...interface{}) error
  259. ```
  260. Unauthorizedf returns an error which satisfies IsUnauthorized().
  261. ## func Wrap
  262. ``` go
  263. func Wrap(other, newDescriptive error) error
  264. ```
  265. Wrap changes the Cause of the error. The location of the Wrap call is also
  266. stored in the error stack.
  267. For example:
  268. if err := SomeFunc(); err != nil {
  269. newErr := &packageError{"more context", private_value}
  270. return errors.Wrap(err, newErr)
  271. }
  272. ## func Wrapf
  273. ``` go
  274. func Wrapf(other, newDescriptive error, format string, args ...interface{}) error
  275. ```
  276. Wrapf changes the Cause of the error, and adds an annotation. The location
  277. of the Wrap call is also stored in the error stack.
  278. For example:
  279. if err := SomeFunc(); err != nil {
  280. return errors.Wrapf(err, simpleErrorType, "invalid value %q", value)
  281. }
  282. ## type Err
  283. ``` go
  284. type Err struct {
  285. // contains filtered or unexported fields
  286. }
  287. ```
  288. Err holds a description of an error along with information about
  289. where the error was created.
  290. It may be embedded in custom error types to add extra information that
  291. this errors package can understand.
  292. ### func NewErr
  293. ``` go
  294. func NewErr(format string, args ...interface{}) Err
  295. ```
  296. NewErr is used to return an Err for the purpose of embedding in other
  297. structures. The location is not specified, and needs to be set with a call
  298. to SetLocation.
  299. For example:
  300. type FooError struct {
  301. errors.Err
  302. code int
  303. }
  304. func NewFooError(code int) error {
  305. err := &FooError{errors.NewErr("foo"), code}
  306. err.SetLocation(1)
  307. return err
  308. }
  309. ### func (\*Err) Cause
  310. ``` go
  311. func (e *Err) Cause() error
  312. ```
  313. The Cause of an error is the most recent error in the error stack that
  314. meets one of these criteria: the original error that was raised; the new
  315. error that was passed into the Wrap function; the most recently masked
  316. error; or nil if the error itself is considered the Cause. Normally this
  317. method is not invoked directly, but instead through the Cause stand alone
  318. function.
  319. ### func (\*Err) Error
  320. ``` go
  321. func (e *Err) Error() string
  322. ```
  323. Error implements error.Error.
  324. ### func (\*Err) Location
  325. ``` go
  326. func (e *Err) Location() (filename string, line int)
  327. ```
  328. Location is the file and line of where the error was most recently
  329. created or annotated.
  330. ### func (\*Err) Message
  331. ``` go
  332. func (e *Err) Message() string
  333. ```
  334. Message returns the message stored with the most recent location. This is
  335. the empty string if the most recent call was Trace, or the message stored
  336. with Annotate or Mask.
  337. ### func (\*Err) SetLocation
  338. ``` go
  339. func (e *Err) SetLocation(callDepth int)
  340. ```
  341. SetLocation records the source location of the error at callDepth stack
  342. frames above the call.
  343. ### func (\*Err) StackTrace
  344. ``` go
  345. func (e *Err) StackTrace() []string
  346. ```
  347. StackTrace returns one string for each location recorded in the stack of
  348. errors. The first value is the originating error, with a line for each
  349. other annotation or tracing of the error.
  350. ### func (\*Err) Underlying
  351. ``` go
  352. func (e *Err) Underlying() error
  353. ```
  354. Underlying returns the previous error in the error stack, if any. A client
  355. should not ever really call this method. It is used to build the error
  356. stack and should not be introspected by client calls. Or more
  357. specifically, clients should not depend on anything but the `Cause` of an
  358. error.
  359. - - -
  360. Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)