errors.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 errors, ok := err.(Errors); ok {
  28. errs = errs.Add(errors...)
  29. } else {
  30. ok = true
  31. for _, e := range errs {
  32. if err == e {
  33. ok = false
  34. }
  35. }
  36. if ok {
  37. errs = append(errs, err)
  38. }
  39. }
  40. }
  41. return errs
  42. }
  43. // Error format happened errors
  44. func (errs Errors) Error() string {
  45. var errors = []string{}
  46. for _, e := range errs {
  47. errors = append(errors, e.Error())
  48. }
  49. return strings.Join(errors, "; ")
  50. }