OSDN Git Service

feat: init cross_tx keepers (#146)
[bytom/vapor.git] / vendor / github.com / jinzhu / gorm / errors.go
1 package gorm
2
3 import (
4         "errors"
5         "strings"
6 )
7
8 var (
9         // 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
10         ErrRecordNotFound = errors.New("record not found")
11         // ErrInvalidSQL invalid SQL error, happens when you passed invalid SQL
12         ErrInvalidSQL = errors.New("invalid SQL")
13         // ErrInvalidTransaction invalid transaction when you are trying to `Commit` or `Rollback`
14         ErrInvalidTransaction = errors.New("no valid transaction")
15         // ErrCantStartTransaction can't start transaction when you are trying to start one with `Begin`
16         ErrCantStartTransaction = errors.New("can't start transaction")
17         // ErrUnaddressable unaddressable value
18         ErrUnaddressable = errors.New("using unaddressable value")
19 )
20
21 // Errors contains all happened errors
22 type Errors []error
23
24 // IsRecordNotFoundError returns current error has record not found error or not
25 func IsRecordNotFoundError(err error) bool {
26         if errs, ok := err.(Errors); ok {
27                 for _, err := range errs {
28                         if err == ErrRecordNotFound {
29                                 return true
30                         }
31                 }
32         }
33         return err == ErrRecordNotFound
34 }
35
36 // GetErrors gets all happened errors
37 func (errs Errors) GetErrors() []error {
38         return errs
39 }
40
41 // Add adds an error
42 func (errs Errors) Add(newErrors ...error) Errors {
43         for _, err := range newErrors {
44                 if err == nil {
45                         continue
46                 }
47
48                 if errors, ok := err.(Errors); ok {
49                         errs = errs.Add(errors...)
50                 } else {
51                         ok = true
52                         for _, e := range errs {
53                                 if err == e {
54                                         ok = false
55                                 }
56                         }
57                         if ok {
58                                 errs = append(errs, err)
59                         }
60                 }
61         }
62         return errs
63 }
64
65 // Error format happened errors
66 func (errs Errors) Error() string {
67         var errors = []string{}
68         for _, e := range errs {
69                 errors = append(errors, e.Error())
70         }
71         return strings.Join(errors, "; ")
72 }