Skip to content

Instantly share code, notes, and snippets.

@nilesh-akhade
Last active July 29, 2022 06:22
Show Gist options
  • Save nilesh-akhade/314aa27cd87931ff8b26dffcec829db2 to your computer and use it in GitHub Desktop.
Save nilesh-akhade/314aa27cd87931ff8b26dffcec829db2 to your computer and use it in GitHub Desktop.
Go custom error handling example
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