
gorm.Errors, which usefully implements `error` for an `[]error` as returned by `DB.GetError()` was already exported, but because it used a private field `errors`, it was not able to be created due to the compile-time error: implicit assignment of unexported field 'errors' in gorm.Errors literal The trivial solution would be to export the `errors` field on `gorm.Errors`, but this led to the issue that the common pattern of checking `err != nil` failed because a struct{error: nil} != nil. We can take advantage of type aliasing here to make Errors an []error, which can in fact be nil and would pass `err != nil` on the happy path. * Remove `(Errors) GetErrors()`, as it's less useful when Errors is an []error which can be iterated over. While this is technically a breaking change, we never expose an Errors and its difficult to build one (it can be done with the existing `(Errors) Add(error)`), but awkwardly. This removal can be reverted without issue and we can make it an identity method, but it seemed an opportune time to reduce API surface area on something that likely isn't used. * Remove errorsInterface, as it's not useful without `(Errors) GetErrors()` * Change `(*Errors) Add(error)` => `(Errors) Add(error...) Errors` because we can't modify even a *Errors when it's a type alias. This is more idiomatic as it follows the pattern of `slice = append(slice, element)` Go developers are familiar with.
57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package gorm
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
// ErrRecordNotFound record not found error, happens when haven't find any matched data when looking up with a struct
|
|
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
|
|
|
|
// 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 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, "; ")
|
|
}
|