gorm/errors.go

59 lines
1.5 KiB
Go
Raw Normal View History

2013-11-15 14:36:39 +04:00
package gorm
2015-08-13 11:42:13 +03:00
import (
"errors"
"strings"
)
2013-11-15 14:36:39 +04:00
var (
2016-03-07 16:09:05 +03:00
// ErrRecordNotFound record not found error, happens when haven't find any matched data when looking up with a struct
2016-03-07 09:54:20 +03:00
ErrRecordNotFound = errors.New("record not found")
2016-03-07 16:09:05 +03:00
// ErrInvalidSQL invalid SQL error, happens when you passed invalid SQL
2016-03-07 09:54:20 +03:00
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")
2013-11-15 14:36:39 +04:00
)
2015-08-13 11:42:13 +03:00
2015-08-14 09:53:26 +03:00
type errorsInterface interface {
GetErrors() []error
}
2016-03-07 09:54:20 +03:00
// Errors contains all happened errors
2015-08-13 11:42:13 +03:00
type Errors struct {
errors []error
}
2016-03-07 09:54:20 +03:00
// GetErrors get all happened errors
2015-08-14 07:29:53 +03:00
func (errs Errors) GetErrors() []error {
2015-08-13 11:42:13 +03:00
return errs.errors
}
2016-03-07 09:54:20 +03:00
// Add add an error
2015-08-13 11:42:13 +03:00
func (errs *Errors) Add(err error) {
2015-08-18 06:06:10 +03:00
if errors, ok := err.(errorsInterface); ok {
for _, err := range errors.GetErrors() {
errs.Add(err)
}
} else {
for _, e := range errs.errors {
if err == e {
return
}
}
errs.errors = append(errs.errors, err)
}
2015-08-13 11:42:13 +03:00
}
2016-03-07 09:54:20 +03:00
// Error format happened errors
2015-08-13 11:42:13 +03:00
func (errs Errors) Error() string {
var errors = []string{}
for _, e := range errs.errors {
errors = append(errors, e.Error())
}
return strings.Join(errors, "; ")
}