Documentation
¶
Overview ¶
This example demonstrates how to use the errors generated by ohnogen tool when generated without passing the -ohno flag. When you generate errors without the -ohno flag you will be able to use the enums as errors without being able to add additional context like a message, extra data, timestamp, source location etc. Lets begin by defining a custom type MyFabulousError
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MyFabulousError ¶
type MyFabulousError int
We first define a custom type like the one below
Example (Check) ¶
With the error generated using ohnogen you can use the errors package method to check if an error matches a specific error
package main import ( "errors" "fmt" "github.com/A-0-5/ohno/examples/usage_without_ohno" ) func main() { err := Foo() if err != nil { if errors.Is(err, usage_without_ohno.Fatal) { fmt.Printf("oh no its fatal!!!\n\t%s", err.Error()) } } } // This is a function which returns an error of type [MyFabulousError] func Foo() error { return usage_without_ohno.Fatal }
Output: oh no its fatal!!! [0x6a]usage_without_ohno.Fatal: Help!!! Im dying!!!
Example (Join) ¶
You can also join multiple errors using the errors.Join method
package main import ( "errors" "fmt" "github.com/A-0-5/ohno/examples/usage_without_ohno" ) func main() { // Here we join 4 errors err := errors.Join( usage_without_ohno.AlreadyExists, usage_without_ohno.Busy, usage_without_ohno.Internal, usage_without_ohno.NotFound, ) // When we print the output will be as follows fmt.Println(err.Error()) }
Output: [0x65]usage_without_ohno.AlreadyExists: I have this already! [0x68]usage_without_ohno.Busy: I'm busy rn, can we do this later? [0x66]usage_without_ohno.Internal: Its not you, its me :( [0x64]usage_without_ohno.NotFound: I didn't find what you were looking for!
Example (Switch) ¶
You can even directly use switch case here to check against multiple errors
package main import ( "fmt" "github.com/A-0-5/ohno/examples/usage_without_ohno" ) func main() { err := Foo() prefix := "" if err != nil { switch err { case usage_without_ohno.AlreadyExists: prefix = "well its there already" case usage_without_ohno.NotFound: prefix = "nah I didn't find it" case usage_without_ohno.Busy: prefix = "not now!!" case usage_without_ohno.Fatal: prefix = "oh no its fatal!!!" default: prefix = "i don't know what this is" } fmt.Printf("%s\n\t%s", prefix, err.Error()) } } // This is a function which returns an error of type [MyFabulousError] func Foo() error { return usage_without_ohno.Fatal }
Output: oh no its fatal!!! [0x6a]usage_without_ohno.Fatal: Help!!! Im dying!!!
Example (Usage) ¶
In this example we are simply defining a function Foo() which can return an error which needs to printed to the console.
package main import ( "fmt" "github.com/A-0-5/ohno/examples/usage_without_ohno" ) func main() { // We just print what ever foo returns fmt.Println(Foo().Error()) } // This is a function which returns an error of type [MyFabulousError] func Foo() error { return usage_without_ohno.Fatal }
Output: [0x6a]usage_without_ohno.Fatal: Help!!! Im dying!!!
Example (WrapUnwrap) ¶
You can wrap these errors like any other error using fmt.Errorf
package main import ( "errors" "fmt" "github.com/A-0-5/ohno/examples/usage_without_ohno" ) func main() { err := fmt.Errorf("Ive wrapped\n\t%w\nin this error", usage_without_ohno.Busy) fmt.Println(err.Error()) fmt.Println() busyError := errors.Unwrap(err) fmt.Println("after unwrapping it we get") fmt.Println(busyError.Error()) }
Output: Ive wrapped [0x68]usage_without_ohno.Busy: I'm busy rn, can we do this later? in this error after unwrapping it we get [0x68]usage_without_ohno.Busy: I'm busy rn, can we do this later?
const ( NotFound MyFabulousError = 100 + iota // I didn't find what you were looking for! AlreadyExists // I have this already! Internal // Its not you, its me :( Unknown // I don't know what happened Busy // I'm busy rn, can we do this later? Fatal // Help!!! Im dying!!! )
Now here we define all the possible error values this package can return and run the command. (here formatbase=16 will make all the codes print in hex representation)
ohnogen -type=MyFabulousError -formatbase=16 -output=example_errors.go
As this example purely concentrates on using the enums directly without depending on the ohno package the -ohno flag is omitted
func (MyFabulousError) Code ¶
func (i MyFabulousError) Code() string
Returns the integer code string as per the format base provided
func (MyFabulousError) Description ¶
func (i MyFabulousError) Description() string
Returns the description string
func (MyFabulousError) Error ¶
func (i MyFabulousError) Error() string
Returns the error's string representation [CODE]PACKAGE_NAME.ERROR_NAME: DESCRIPTION
func (MyFabulousError) String ¶
func (i MyFabulousError) String() string
Returns the error name as string