Last active
July 29, 2022 06:22
-
-
Save nilesh-akhade/314aa27cd87931ff8b26dffcec829db2 to your computer and use it in GitHub Desktop.
Go custom error handling example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
) | |
var MyCustomError *ErrCustom | |
type ErrCustom struct{} | |
func (e *ErrCustom) Error() string { | |
return "CustomError occurred" | |
} | |
func (e *ErrCustom) Is(target error) bool { | |
if _, ok := target.(*ErrCustom); ok { | |
return true | |
} | |
return false | |
} | |
func main() { | |
err := process() | |
if errors.Is(err, MyCustomError) { | |
fmt.Println("Error is of type MyCustomError") | |
} | |
} | |
func process() error { | |
e := &ErrCustom{} | |
return fmt.Errorf("Wrapped error:%w", e) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment