Documentation
¶
Overview ¶
Package xyerror supports to define and compare errors conveniently.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( BaseException = Exception{/* contains filtered or unexported fields */} IOError = BaseException.NewException("IOError") FloatingPointError = BaseException.NewException("FloatingPointError") IndexError = BaseException.NewException("IndexError") KeyError = BaseException.NewException("KeyError") ValueError = BaseException.NewException("ValueError") ParameterError = BaseException.NewException("ParameterError") TypeError = BaseException.NewException("TypeError") AssertionError = BaseException.NewException("AssertionError") )
Predefined Exceptions.
Functions ¶
Types ¶
type CombinedException ¶ added in v1.0.0
type CombinedException []Exception
CombinedException is an array of Exception. It supports to creates an Exception inherited from many parents.
Example ¶
package main import ( "errors" "fmt" "github.com/xybor-x/xyerror" ) func main() { // CombinedException allows you to create an Exception with multiparents. var KeyValueError = xyerror. Combine(xyerror.KeyError, xyerror.ValueError). NewException("KeyValueError") var err = KeyValueError.New("something is wrong") if errors.Is(err, xyerror.KeyError) { fmt.Println("err is a KeyError") } if errors.Is(err, xyerror.ValueError) { fmt.Println("err is a ValueError") } }
Output: err is a KeyError err is a ValueError
func Combine ¶
func Combine(cs ...Exception) CombinedException
Combine supports creating a group of Exceptions. This group can be used to create the Exception with multiparents.
func (CombinedException) NewException ¶ added in v1.0.0
func (combined CombinedException) NewException(name string) Exception
NewException creates an Exception with multiparents.
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is a special error belongs to a generic error class.
Example ¶
package main import ( "errors" "fmt" "github.com/xybor-x/xyerror" ) func main() { // You can compare an Error with an Exception by using the built-in method // errors.Is. var NegativeIndexError = xyerror.IndexError.NewException("NegativeIndexError") var err1 = xyerror.ValueError.New("some value error") if errors.Is(err1, xyerror.ValueError) { fmt.Println("err1 is a ValueError") } if !errors.Is(err1, NegativeIndexError) { fmt.Println("err1 is not a NegativeIndexError") } var err2 = NegativeIndexError.Newf("some negative index error %d", -1) if errors.Is(err2, NegativeIndexError) { fmt.Println("err2 is a NegativeIndexError") } if errors.Is(err2, xyerror.IndexError) { fmt.Println("err2 is a IndexError") } if !errors.Is(err2, xyerror.ValueError) { fmt.Println("err2 is not a ValueError") } }
Output: err1 is a ValueError err1 is not a NegativeIndexError err2 is a NegativeIndexError err2 is a IndexError err2 is not a ValueError
type Exception ¶ added in v1.0.0
type Exception struct {
// contains filtered or unexported fields
}
Exception is the generic type of Errors. It does not contain the error message. You should use an Error by creating from Exception, instead of using Exception directly.
Example ¶
package main import ( "fmt" "github.com/xybor-x/xyerror" ) func main() { // To create a root Exception, call xyerror.NewException with the its name. var RootError = xyerror.NewException("RootError") // You can create an Exception by inheriting from another one. var ChildError = RootError.NewException("ChildError") fmt.Println(RootError) fmt.Println(ChildError) }
Output: RootError ChildError
func NewException ¶ added in v1.0.0
NewException creates an Exception inherited from the BaseException.
func (Exception) Is ¶ added in v1.0.0
Is returns true if the Exception is inherited from the target.
func (Exception) NewException ¶ added in v1.0.0
NewException creates a new Exception by inheriting the called Exception.