Skip to content

Instantly share code, notes, and snippets.

@ToQoz
Created December 2, 2013 02:34
Show Gist options
  • Save ToQoz/7744078 to your computer and use it in GitHub Desktop.
Save ToQoz/7744078 to your computer and use it in GitHub Desktop.
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