Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( UnknownError = defaultGen.NewClass("UnknownError") IOError = defaultGen.NewClass("IOError") FloatingPointError = defaultGen.NewClass("FloatingPointError") IndexError = defaultGen.NewClass("IndexError") KeyError = defaultGen.NewClass("KeyError") NotImplementedError = defaultGen.NewClass("NotImplementedError") ValueError = defaultGen.NewClass("ValueError") ParameterError = defaultGen.NewClass("ParameterError") TypeError = defaultGen.NewClass("TypeError") AssertionError = defaultGen.NewClass("AssertionError") )
Functions ¶
Types ¶
type Class ¶
type Class struct {
// contains filtered or unexported fields
}
Class is a special error with error number and error name. Error number is a unique number of Class and helps to determine which module the error Class belongs to.
The main purpose of error Class is creating a XyError, so that it should not be used for returning.
A Class can be created by one or many parent Classes. The Class without parent is called root Class..
Example ¶
package main import ( "fmt" "github.com/xybor/xyplatform/xyerror" ) var exampleGen = xyerror.Register("example", 400000) func main() { // To create a root Class, call Generator.NewClass with the name of Class. var RootError = exampleGen.NewClass("RootError") // You can create a class from another one. var ChildError = RootError.NewClass("ChildError") fmt.Println(RootError) fmt.Println(ChildError) }
Output: [400001] RootError [400002] ChildError
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator is used to generate root Class for every module. It is determined by the identifier of module.
type Group ¶
type Group []Class
Example ¶
package main import ( "errors" "fmt" "github.com/xybor/xyplatform/xyerror" ) var exampleGen = xyerror.Register("example", 400000) func main() { // Group allows you to create a class with multiparents. var KeyValueError = xyerror. Combine(xyerror.KeyError, xyerror.ValueError). NewClass(exampleGen, "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
type XyError ¶
type XyError struct {
// contains filtered or unexported fields
}
XyError is an error supporting to check if an error belongs to a class or not.
errors.Is(err, cls) returns true if err is created by cls itself or cls's child class.
Example ¶
package main import ( "errors" "fmt" "github.com/xybor/xyplatform/xyerror" ) func main() { // You can compare a XyError with an Class by using the built-in method // errors.Is. var NegativeIndexError = xyerror.IndexError.NewClass("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.New("some negative index error") 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
Click to show internal directories.
Click to hide internal directories.