Skip to content

Commit

Permalink
fixing field errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ardan-bkennedy committed Dec 10, 2024
1 parent cbf46d0 commit 982523c
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 37 deletions.
2 changes: 1 addition & 1 deletion app/domain/homeapp/homeapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (a *app) query(ctx context.Context, r *http.Request) web.Encoder {

filter, err := parseFilter(qp)
if err != nil {
return err.(errs.FieldErrors)
return err.(*errs.Error)
}

orderBy, err := order.Parse(orderByFields, qp.OrderBy, homebus.DefaultOrderBy)
Expand Down
2 changes: 1 addition & 1 deletion app/domain/productapp/productapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (a *app) query(ctx context.Context, r *http.Request) web.Encoder {

filter, err := parseFilter(qp)
if err != nil {
return err.(errs.FieldErrors)
return err.(*errs.Error)
}

orderBy, err := order.Parse(orderByFields, qp.OrderBy, productbus.DefaultOrderBy)
Expand Down
2 changes: 1 addition & 1 deletion app/domain/userapp/userapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (a *app) query(ctx context.Context, r *http.Request) web.Encoder {

filter, err := parseFilter(qp)
if err != nil {
return err.(errs.FieldErrors)
return err.(*errs.Error)
}

orderBy, err := order.Parse(orderByFields, qp.OrderBy, userbus.DefaultOrderBy)
Expand Down
2 changes: 1 addition & 1 deletion app/domain/vproductapp/vproductapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (a *app) query(ctx context.Context, r *http.Request) web.Encoder {

filter, err := parseFilter(qp)
if err != nil {
return err.(errs.FieldErrors)
return err.(*errs.Error)
}

orderBy, err := order.Parse(orderByFields, qp.OrderBy, vproductbus.DefaultOrderBy)
Expand Down
36 changes: 3 additions & 33 deletions app/sdk/errs/errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ type FieldError struct {
type FieldErrors []FieldError

// NewFieldsError creates an fields error.
func NewFieldsError(field string, err error) FieldErrors {
return FieldErrors{
func NewFieldsError(field string, err error) *Error {
return New(InvalidArgument, FieldErrors{
{
Field: field,
Err: err.Error(),
},
}
})
}

// Error implements the error interface.
Expand All @@ -143,33 +143,3 @@ func (fe FieldErrors) Error() string {
}
return string(d)
}

// Encode implements the encoder interface.
func (fe FieldErrors) Encode() ([]byte, string, error) {
d, err := json.Marshal(fe)
return d, "application/json", err
}

// Fields returns the fields that failed validation
func (fe FieldErrors) Fields() map[string]string {
m := make(map[string]string, len(fe))
for _, fld := range fe {
m[fld.Field] = fld.Err
}
return m
}

// IsFieldErrors checks if an error of type FieldErrors exists.
func IsFieldErrors(err error) bool {
var fe FieldErrors
return errors.As(err, &fe)
}

// GetFieldErrors returns a copy of the FieldErrors pointer.
func GetFieldErrors(err error) FieldErrors {
var fe FieldErrors
if !errors.As(err, &fe) {
return nil
}
return fe
}

0 comments on commit 982523c

Please sign in to comment.