Created
December 2, 2013 02:34
-
-
Save ToQoz/7744078 to your computer and use it in GitHub Desktop.
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" | |
"reflect" | |
) | |
var SimpleValidationError = errors.New("Validation error") | |
type ValidationError struct { | |
Field string | |
Message string | |
} | |
func (e ValidationError) Error() string { | |
return e.Field + " " + e.Message | |
} | |
func main() { | |
var err error | |
// 0. Compare error message | |
err = func() error { | |
return errors.New("Some error") | |
}() | |
if err != nil { | |
if err.Error() == "Some error" { | |
fmt.Println(err.Error()) | |
} | |
} | |
// 1. Compare global error object | |
err = func() error { | |
return SimpleValidationError | |
}() | |
if err != nil { | |
if err == SimpleValidationError { | |
fmt.Println(err.Error()) | |
} | |
// 2. Compare type of error object | |
err = func() error { | |
return ValidationError{Field: "name", Message: "is too long"} | |
}() | |
if err != nil { | |
if reflect.TypeOf(err) == reflect.TypeOf(ValidationError{}) { | |
fmt.Println(err.Error()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment