Documentation
¶
Overview ¶
Package goerrors - Easy error informations and stack traces for Go with rich informations and a Try/Catch/Finally mechanism.
Example ¶
// A function which open a file openFile := func(name string) (*os.File, error) { f, err := os.Open(name) // Decorate the opening error if err != nil { return nil, DecorateError(err) } return f, nil } // A function which read one byte in the opened file readFile := func(f *os.File) (byte, error) { var b [1]byte n, err := f.Read(b[:]) // Decorate the read error if err != nil { return 0, DecorateError(err) } // Return custom error if n == 0 { return 0, MakeError("No data to read") } return b[0], nil } // Deactivate stack trace // (cause stacktrace produced for testing package is specific to go installation and may change with Go version) SetDebug(false) // Make an unfindable filename const name = ".a_file_5123351069599224559.txt" // Call the checked open function f, err := openFile(name) if err != nil { fmt.Println(err) return } // Here, in this example, this code won't never be executed if the file can't be opened defer f.Close() _, err = readFile(f) if err != nil { fmt.Println(err) return }
Output: StandardError: open .a_file_5123351069599224559.txt: no such file or directory
Index ¶
- Constants
- func Catch(err *error, catch, finally ErrorHandler)
- func CheckedMain(handler MainHandler)
- func DiscardPanic()
- func GetDebug() bool
- func GetSource(err error) error
- func Raise(message string, args ...interface{})
- func RaiseError(err error)
- func RaiseWithInfos(errorCode int64, data interface{}, message string, args ...interface{})
- func SetDebug(debug bool)
- func Try(try, catch, finally ErrorHandler) (err error)
- type ErrorHandler
- type GoError
- func (goErr *GoError) Catch(err *error, catch, finally ErrorHandler)
- func (goErr *GoError) Error() string
- func (goErr *GoError) GetData() interface{}
- func (goErr *GoError) GetMessage() string
- func (goErr *GoError) GetName() string
- func (goErr *GoError) GetSource() error
- func (goErr *GoError) Init(value interface{}, message string, data interface{}, source error, ...) IError
- func (goErr *GoError) IsParentOf(err error) bool
- func (goErr *GoError) Raise()
- func (goErr *GoError) Try(try, catch, finally ErrorHandler) (err error)
- type IError
- type IStandardError
- func AddInfo(err error, info string, args ...interface{}) IStandardError
- func DecorateError(err error) IStandardError
- func DecorateErrorWithDatas(err error, code int64, data interface{}, msg string, args ...interface{}) IStandardError
- func MakeError(message string, args ...interface{}) IStandardError
- func MakeErrorWithDatas(code int64, data interface{}, message string, args ...interface{}) IStandardError
- type MainHandler
Examples ¶
Constants ¶
const STACKTRACE_MAXLEN = 65536
STACKTRACE_MAXLEN this version of stack trace asks to have a limit which arbitrary set.
Variables ¶
This section is empty.
Functions ¶
func Catch ¶
func Catch(err *error, catch, finally ErrorHandler)
Catch is the global function to catch an error
func CheckedMain ¶
func CheckedMain(handler MainHandler)
CheckedMain should be called in the main function body. It assumes the uncatched error to see errors with stacktraces.
func DiscardPanic ¶
func DiscardPanic()
DiscardPanic discard a panic.
This is an helper function that should be called with a defer instruction to blocks a panic and permits to continuing the excution instead of exiting the program.
func GetDebug ¶
func GetDebug() bool
GetDebug returns the Debug boolean flag which indicates that the stack trace will be provided in errors
func GetSource ¶
GetSource gets the error source from an error, or returns nil if the error passed in argument is not an IError
func Raise ¶
func Raise(message string, args ...interface{})
Raise is the global function to raise an anonymous error
func RaiseError ¶
func RaiseError(err error)
RaiseError is the global function to raise the error passed in argument
func RaiseWithInfos ¶
RaiseWithInfos is like Raise with error code and custom data
func SetDebug ¶
func SetDebug(debug bool)
SetDebug modifies the Debug boolean flag for enable or disable the stack trace in errors. If the `debug` parameter is true, so the stack trace will be provided in errors.
func Try ¶
func Try(try, catch, finally ErrorHandler) (err error)
Try is the global function to call a try block
Types ¶
type ErrorHandler ¶
ErrorHandler Handler for executing Try, Catch, or Finally block
func SetUncatchedErrorHandler ¶
func SetUncatchedErrorHandler(handler ErrorHandler) ErrorHandler
SetUncatchedErrorHandler defines the handler called when an error is not catched.
But the handled is called only if the CheckedMain function is called (indeed, the handler is called by CheckedMain).
type GoError ¶
type GoError struct {
// contains filtered or unexported fields
}
GoError Basic error structure
func (*GoError) Catch ¶
func (goErr *GoError) Catch(err *error, catch, finally ErrorHandler)
Catch catchs error (used as a defered call)
func (*GoError) GetMessage ¶
GetMessage gets error message
func (*GoError) Init ¶
func (goErr *GoError) Init(value interface{}, message string, data interface{}, source error, pruneLevels uint) IError
Init for initializing customized error
func (*GoError) IsParentOf ¶
IsParentOf tests if this error is one of parents of error `err` passed in parameter
func (*GoError) Try ¶
func (goErr *GoError) Try(try, catch, finally ErrorHandler) (err error)
Try completes try/catch/finally block
type IError ¶
type IError interface { // This is an error error // Get error name GetName() string // Get original error GetSource() error // Get error message GetMessage() string // Get custon data GetData() interface{} // Complete try/catch/finally block Try(try, catch, finally ErrorHandler) error // Catch error (used as a defered call) Catch(err *error, catch, finally ErrorHandler) // Raise error Raise() // Test if this error is one of parents of error `err` passed in parameter IsParentOf(err error) bool // contains filtered or unexported methods }
IError Interface for extended Go errors
type IStandardError ¶
type IStandardError interface { // Base interface IError // Add more informations on that error AddInfo(info string, args ...interface{}) IStandardError // Get error code GetCode() int64 }
IStandardError Interface for a standard error which decorate another basic go error (`error` go interface) with an error code, message and other additionnal informations
func AddInfo ¶
func AddInfo(err error, info string, args ...interface{}) IStandardError
AddInfo is the global function to add information in an error whatever. This function just call the "AddInfo" method of an standard error.
func DecorateError ¶
func DecorateError(err error) IStandardError
DecorateError «decorates» the error passed as "err" parameter. The error returned will be an standard error with additionnal informations and stack trace.
func DecorateErrorWithDatas ¶
func DecorateErrorWithDatas(err error, code int64, data interface{}, msg string, args ...interface{}) IStandardError
DecorateErrorWithDatas is like `DecorateError` with error code and custom data
func MakeError ¶
func MakeError(message string, args ...interface{}) IStandardError
MakeError makes an standard error from a message passed as "message" parameter
func MakeErrorWithDatas ¶
func MakeErrorWithDatas(code int64, data interface{}, message string, args ...interface{}) IStandardError
MakeErrorWithDatas is like `MakeError` with error code and custom data
type MainHandler ¶
type MainHandler func() error
MainHandler is the handler for main function used with the CheckedMain function