errors.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package gorm
  2. import (
  3. "errors"
  4. "strings"
  5. )
  6. var (
  7. // ErrRecordNotFound record not found error, happens when haven't find any matched data when looking up with a struct
  8. ErrRecordNotFound = errors.New("record not found")
  9. // ErrInvalidSQL invalid SQL error, happens when you passed invalid SQL
  10. ErrInvalidSQL = errors.New("invalid SQL")
  11. // ErrInvalidTransaction invalid transaction when you are trying to `Commit` or `Rollback`
  12. ErrInvalidTransaction = errors.New("no valid transaction")
  13. // ErrCantStartTransaction can't start transaction when you are trying to start one with `Begin`
  14. ErrCantStartTransaction = errors.New("can't start transaction")
  15. // ErrUnaddressable unaddressable value
  16. ErrUnaddressable = errors.New("using unaddressable value")
  17. )
  18. // Errors contains all happened errors
  19. type Errors []error
  20. // GetErrors gets all happened errors
  21. func (errs Errors) GetErrors() []error {
  22. return errs
  23. }
  24. // Add adds an error
  25. func (errs Errors) Add(newErrors ...error) Errors {
  26. for _, err := range newErrors {
  27. if err == nil {
  28. continue
  29. }
  30. if errors, ok := err.(Errors); ok {
  31. errs = errs.Add(errors...)
  32. } else {
  33. ok = true
  34. for _, e := range errs {
  35. if err == e {
  36. ok = false
  37. }
  38. }
  39. if ok {
  40. errs = append(errs, err)
  41. }
  42. }
  43. }
  44. return errs
  45. }
  46. // Error format happened errors
  47. func (errs Errors) Error() string {
  48. var errors = []string{}
  49. for _, e := range errs {
  50. errors = append(errors, e.Error())
  51. }
  52. return strings.Join(errors, "; ")
  53. }