项目作者: synw

项目描述 :
Trace errors across the call stack
高级语言: Go
项目地址: git://github.com/synw/terr.git
创建时间: 2017-02-24T12:17:58Z
项目社区:https://github.com/synw/terr

开源协议:MIT License

下载


Traced errors

Propagate detailled errors up the call stack

Data structure

  1. type Terr struct {
  2. From string
  3. Level string
  4. Error error
  5. File string
  6. Line int
  7. }
  8. type Trace struct {
  9. Errors []*Terr
  10. }

Api

New (errObj interface{}, level …string) *Trace : create a trace
from an error or a string

  1. func foo() *terr.Trace {
  2. tr := terr.New("Error one")
  3. return tr
  4. }
  5. func bar() *terr.Trace {
  6. err := errors.New("Error one")
  7. tr := terr.New(err)
  8. return tr
  9. }

Trace.Add (errObj interface{}, level …string) *Trace : adds to a trace
from an error message string

  1. func myfunc2() *terr.Trace {
  2. tr := myfunc()
  3. tr := tr.Add("Error two", "warning")
  4. return tr
  5. }

Trace.Pass (level …string) *Trace : adds a trace with no new error message: it just records
the function call

  1. tr := myfunc2()
  2. tr = tr.Pass()

Trace.Check: prints the trace if some errors are present

  1. tr := myfunc2()
  2. tr.Check()

Trace.Err () error: returns the full trace as a standard error

  1. tr := myfunc2()
  2. err := tr.Err()

Trace.Print: print the trace’s coloured error message

  1. tr := myfunc2()
  2. tr.Print()

Trace.Msg () string: returns the trace’s coloured error message

  1. tr := myfunc2()
  2. fmt.Println(tr.Msg())

Trace.Log () string: returns the last error message of the trace

  1. tr := myfunc2()
  2. tr.Log()

Trace.Stack (errObj interface{}, level …string):
same as Trace.Add but adds the stack trace message of the error in the message

  1. func myfunc3() *terr.Trace {
  2. tr := myfunc2()
  3. tr := tr.Stack("Error two", "debug")
  4. return tr
  5. }

Trace.Fatal (errObj interface{}, level …string):
same as Trace.Stack and exits the program

  1. func myfunc4() *terr.Trace {
  2. tr := myfunc3()
  3. tr := tr.Fatal("Error two")
  4. return tr
  5. }

Error levels

debug, info, warning, error (default), fatal, panic

Examples

Check the examples

  1. package main
  2. import (
  3. "errors"
  4. "github.com/synw/terr"
  5. )
  6. func f1() *terr.Trace {
  7. tr := terr.New("First error")
  8. return tr
  9. }
  10. func f2() *terr.Trace {
  11. tr := f1()
  12. err := errors.New("Second error")
  13. tr = tr.Add(err)
  14. return tr
  15. }
  16. func main() {
  17. tr := f2()
  18. tr.Check()
  19. }

Output:

  1. 0 [error ] Second error from main.f2 line 18 in /home/me/terr/example/simple.go
  2. 1 [error ] First error from main.f1 line 11 in /home/me/terr/example/simple.go