Documentation ¶
Overview ¶
Package errt package defines some basic errors to use, see example.
The idea is to encapsulate any error in a hidden struct that implements a public interface.
That way, type assertion will give you the kind of error you encountered.
Also, you're free to redefine your custom errors in interfaces to avoid tight coupling between your program/library and this one.
Example ¶
package main import ( "errors" "fmt" "git.canopsis.net/canopsis/go-engines/lib/errt" ) type myError struct { errt.ErrT moreInfos bool } type MyError interface { errt.ErrT IsMyError() MoreInfos() bool } // IsMyError function does nothing because it's only here to implement the MyError interface. // This makes type assertion possible. func (e myError) IsMyError() { } func (e myError) MoreInfos() bool { return e.moreInfos } func NewMyError(err error, moreInfos bool) MyError { // Always return nil if the root error is nil if err == nil { return nil } return myError{ ErrT: errt.NewErrT(err), moreInfos: moreInfos, } } func IReturnAnError() error { oerr := errors.New("this is my error") return NewMyError(oerr, true) } func main() { err := IReturnAnError() if err != nil { switch referr := err.(type) { case MyError: fmt.Printf("i have more infos: %v", referr.MoreInfos()) default: fmt.Printf("unknown error: %v", referr) } } }
Output:
Index ¶
- func IsFatal(err error) bool
- func NewDuplicated(err error) error
- func NewFatal(err error, rcode int) error
- func NewIOError(err error) error
- func NewNotFound(err error) error
- func NewUnknownError(err error) error
- func NewUnmanagedEventError(err error) error
- type Duplicated
- type ErrT
- type Fatal
- type IOError
- type NotFound
- type UnknownError
- type UnmanagedEventError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsFatal ¶
IsFatal returns true if the error is of type errt.Fatal, using type assertion. This is only a shortcut for type assertion since Fatal error might be used in multiple places.
func NewFatal ¶
NewFatal returns a new Fatal Error
param rcode is the optional return code that can be used for os.Exit() for example.
func NewUnmanagedEventError ¶
NewUnmanagedEventError ...
Types ¶
type Duplicated ¶
type Duplicated interface { IsDuplicated() ErrT }
Duplicated can be used when something is a duplicate of something else.
type IOError ¶
type IOError interface { IsIOError() ErrT }
IOError of any kind: timeout, host unreachable, file permissions...
type UnknownError ¶
type UnknownError interface { ErrT IsUnknownError() }
UnknownError should be used to encapsulate any non fatal error with no consequences other than "it doesn't worked"
type UnmanagedEventError ¶
type UnmanagedEventError interface { ErrT IsUnmanagedEventError() }
UnmanagedEventError can be returned from any method that doesn't manage a given types.Event