OSDN Git Service

Merge pull request #201 from Bytom/v0.1
[bytom/vapor.git] / vendor / github.com / jinzhu / gorm / errors.go
diff --git a/vendor/github.com/jinzhu/gorm/errors.go b/vendor/github.com/jinzhu/gorm/errors.go
new file mode 100755 (executable)
index 0000000..27c9a92
--- /dev/null
@@ -0,0 +1,72 @@
+package gorm
+
+import (
+       "errors"
+       "strings"
+)
+
+var (
+       // ErrRecordNotFound record not found error, happens when only haven't find any matched data when looking up with a struct, finding a slice won't return this error
+       ErrRecordNotFound = errors.New("record not found")
+       // ErrInvalidSQL invalid SQL error, happens when you passed invalid SQL
+       ErrInvalidSQL = errors.New("invalid SQL")
+       // ErrInvalidTransaction invalid transaction when you are trying to `Commit` or `Rollback`
+       ErrInvalidTransaction = errors.New("no valid transaction")
+       // ErrCantStartTransaction can't start transaction when you are trying to start one with `Begin`
+       ErrCantStartTransaction = errors.New("can't start transaction")
+       // ErrUnaddressable unaddressable value
+       ErrUnaddressable = errors.New("using unaddressable value")
+)
+
+// Errors contains all happened errors
+type Errors []error
+
+// IsRecordNotFoundError returns current error has record not found error or not
+func IsRecordNotFoundError(err error) bool {
+       if errs, ok := err.(Errors); ok {
+               for _, err := range errs {
+                       if err == ErrRecordNotFound {
+                               return true
+                       }
+               }
+       }
+       return err == ErrRecordNotFound
+}
+
+// GetErrors gets all happened errors
+func (errs Errors) GetErrors() []error {
+       return errs
+}
+
+// Add adds an error
+func (errs Errors) Add(newErrors ...error) Errors {
+       for _, err := range newErrors {
+               if err == nil {
+                       continue
+               }
+
+               if errors, ok := err.(Errors); ok {
+                       errs = errs.Add(errors...)
+               } else {
+                       ok = true
+                       for _, e := range errs {
+                               if err == e {
+                                       ok = false
+                               }
+                       }
+                       if ok {
+                               errs = append(errs, err)
+                       }
+               }
+       }
+       return errs
+}
+
+// Error format happened errors
+func (errs Errors) Error() string {
+       var errors = []string{}
+       for _, e := range errs {
+               errors = append(errors, e.Error())
+       }
+       return strings.Join(errors, "; ")
+}