better errors
This commit is contained in:
parent
a2e54b1f0e
commit
83cd654030
3 changed files with 20 additions and 9 deletions
15
filter.go
15
filter.go
|
|
@ -4,7 +4,6 @@
|
|||
package filter
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
|
@ -97,13 +96,13 @@ type Query struct {
|
|||
func NewQuery(s string) (q *Query, err error) {
|
||||
q = &Query{q: s}
|
||||
if s == "" {
|
||||
err = errors.New("query is empty")
|
||||
err = errEmpty
|
||||
return
|
||||
}
|
||||
qm := make(map[string]struct{})
|
||||
fields := clearFields(s)
|
||||
if len(fields) == 0 {
|
||||
err = errors.New("query is empty")
|
||||
err = errEmpty
|
||||
return
|
||||
}
|
||||
for _, v := range fields {
|
||||
|
|
@ -132,7 +131,7 @@ func NewQuery(s string) (q *Query, err error) {
|
|||
}
|
||||
continue
|
||||
default:
|
||||
err = errors.New("invalid sort: " + v)
|
||||
err = newErr("invalid sort: " + v)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -141,12 +140,12 @@ func NewQuery(s string) (q *Query, err error) {
|
|||
}
|
||||
m := strings.SplitN(v, ":", 2)
|
||||
if len(m) != 2 {
|
||||
err = errors.New("invalid match: " + v)
|
||||
err = newErr("invalid match: " + v)
|
||||
return
|
||||
}
|
||||
if q.l == and {
|
||||
if _, ok := qm[m[0]]; ok {
|
||||
err = errors.New("field for AND matching used more than once: " + m[0])
|
||||
err = newErr("field for AND matching used more than once: " + m[0])
|
||||
return
|
||||
}
|
||||
qm[m[0]] = struct{}{}
|
||||
|
|
@ -164,7 +163,7 @@ func NewQuery(s string) (q *Query, err error) {
|
|||
if len(q.m) == 0 && q.s.enabled {
|
||||
q.s.only = true
|
||||
} else if len(q.m) == 0 {
|
||||
err = errors.New("query is empty")
|
||||
err = errEmpty
|
||||
}
|
||||
if len(q.m) == 1 && q.l != not {
|
||||
q.l = or
|
||||
|
|
@ -281,7 +280,7 @@ func (q Query) sort(f Filter) {
|
|||
func (q Query) Filter(f Filter) error {
|
||||
v := reflect.ValueOf(f)
|
||||
if v.Kind() != reflect.Ptr {
|
||||
return errors.New("filter: non-pointer " + v.Type().String())
|
||||
return newErr("non-pointer " + v.Type().String())
|
||||
}
|
||||
|
||||
if q.s.enabled && q.s.only {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue