terr

package module
v0.0.0-...-eb36dfa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2022 License: MIT Imports: 6 Imported by: 36

README

Traced errors

Propagate detailled errors up the call stack

Data structure

type Terr struct {
   From  string
   Level string
   Error error
   File  string
   Line  int
}

type Trace struct {
   Errors []*Terr
}

Api

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

func foo() *terr.Trace {
   tr := terr.New("Error one")
   return tr
}

func bar() *terr.Trace {
   err := errors.New("Error one")
   tr := terr.New(err)
   return tr
}

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

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

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

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

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

tr := myfunc2()
tr.Check()

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

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

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

tr := myfunc2()
tr.Print()

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

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

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

tr := myfunc2()
tr.Log()

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

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

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

func myfunc4() *terr.Trace {
   tr := myfunc3()
   tr := tr.Fatal("Error two")
   return tr
}
Error levels

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

Examples

Check the examples

package main

import (
   "errors"
   "github.com/synw/terr"
)

func f1() *terr.Trace {
   tr := terr.New("First error")
   return tr
}

func f2() *terr.Trace {
   tr := f1()
   err := errors.New("Second error")
   tr = tr.Add(err)
   return tr
}

func main() {
   tr := f2()
   tr.Check()
}

Output:

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

print debug info about something

Types

type Terr

type Terr struct {
	From  string
	Level string
	Error error
	File  string
	Line  int
}

type Trace

type Trace struct {
	Errors []*Terr
}

func New

func New(errObj interface{}, level ...string) *Trace

constructor

func (Trace) Add

func (trace Trace) Add(errObj interface{}, level ...string) *Trace

add a new error to the trace

func (Trace) Check

func (trace Trace) Check()

prints the trace if some errors are found

func (Trace) Err

func (trace Trace) Err() error

returns the trace as a standard error

func (Trace) Error

func (trace Trace) Error() string

get the error message from the trace

func (Trace) Fatal

func (trace Trace) Fatal(errObj interface{})

same as Stack but stops the program

func (Trace) Log

func (trace Trace) Log() string

returns the last error message with no line number and file information

func (Trace) Msg

func (trace Trace) Msg() string

get the coloured error message from the trace

func (Trace) Pass

func (trace Trace) Pass(level ...string) *Trace

add a new error to the trace with no message

func (Trace) Print

func (trace Trace) Print()

print the trace

func (Trace) Stack

func (trace Trace) Stack(errObj interface{}, level ...string) *Trace

same as Add but the error message will contain the stack trace

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL