Skip to content

Commit

Permalink
refactor method name on FieldError
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Dec 21, 2020
1 parent 7a28525 commit 8adf1e8
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion fastly/acl_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (c *Client) BatchModifyACLEntries(i *BatchModifyACLEntriesInput) error {
}

if len(i.Entries) > BatchModifyMaximumOperations {
return NewFieldError("Entries").Custom("batch modify maximum operations exceeded")
return NewFieldError("Entries").Message("batch modify maximum operations exceeded")
}

path := fmt.Sprintf("/service/%s/acl/%s/entries", i.ServiceID, i.ACLID)
Expand Down
2 changes: 1 addition & 1 deletion fastly/dictionary_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (c *Client) BatchModifyDictionaryItems(i *BatchModifyDictionaryItemsInput)
}

if len(i.Items) > BatchModifyMaximumOperations {
return NewFieldError("Items").Custom("batch modify maximum operations exceeded")
return NewFieldError("Items").Message("batch modify maximum operations exceeded")
}

path := fmt.Sprintf("/service/%s/dictionary/%s/items", i.ServiceID, i.DictionaryID)
Expand Down
2 changes: 1 addition & 1 deletion fastly/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (c *Client) UpdateDomain(i *UpdateDomainInput) (*Domain, error) {
}

if i.NewName == nil && i.Comment == nil {
return nil, NewFieldError("Name or Comment").Custom("at least one of the available 'optional' fields is required")
return nil, NewFieldError("Name, Comment").Message("at least one of the available 'optional' fields is required")
}

path := fmt.Sprintf("/service/%s/version/%d/domain/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
Expand Down
2 changes: 1 addition & 1 deletion fastly/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func TestClient_UpdateDomain_validation(t *testing.T) {
ServiceVersion: 1,
Name: "bar",
})
if err.Error() != "problem with field 'Name or Comment': at least one of the available 'optional' fields is required" {
if err.Error() != "problem with field 'Name, Comment': at least one of the available 'optional' fields is required" {
t.Errorf("bad error: %s", err)
}
}
Expand Down
12 changes: 6 additions & 6 deletions fastly/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

// FieldError represents a custom error type for API data fields.
type FieldError struct {
kind string
custom string
kind string
message string
}

// Error fulfills the error interface.
Expand All @@ -26,15 +26,15 @@ type FieldError struct {
// Because of this we allow modifying the error message to reflect whether the
// missing field was either 'required' or just missing a value.
func (e *FieldError) Error() string {
if e.custom != "" {
return fmt.Sprintf("problem with field '%s': %s", e.kind, e.custom)
if e.message != "" {
return fmt.Sprintf("problem with field '%s': %s", e.kind, e.message)
}

return fmt.Sprintf("missing required field '%s'", e.kind)
}

func (e *FieldError) Custom(msg string) *FieldError {
e.custom = msg
func (e *FieldError) Message(msg string) *FieldError {
e.message = msg
return e
}

Expand Down
4 changes: 2 additions & 2 deletions fastly/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ func (c *Client) UpdateService(i *UpdateServiceInput) (*Service, error) {
}

if i.Name == nil && i.Comment == nil {
return nil, NewFieldError("Name or Comment").Custom("at least one of the available 'optional' fields is required")
return nil, NewFieldError("Name, Comment").Message("at least one of the available 'optional' fields is required")
}

if i.Name != nil && *i.Name == "" {
return nil, NewFieldError("Name").Custom("service name can't be an empty value")
return nil, NewFieldError("Name").Message("service name can't be an empty value")
}

path := fmt.Sprintf("/service/%s", i.ServiceID)
Expand Down
2 changes: 1 addition & 1 deletion fastly/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestClient_UpdateService_validation(t *testing.T) {
_, err = testClient.UpdateService(&UpdateServiceInput{
ServiceID: "foo",
})
if err.Error() != "problem with field 'Name or Comment': at least one of the available 'optional' fields is required" {
if err.Error() != "problem with field 'Name, Comment': at least one of the available 'optional' fields is required" {
t.Errorf("bad error: %s", err)
}

Expand Down
6 changes: 3 additions & 3 deletions fastly/waf_active_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (c *Client) CreateWAFActiveRules(i *CreateWAFActiveRulesInput) ([]*WAFActiv
}

if len(i.Rules) == 0 {
return nil, NewFieldError("Rules").Custom("expect at least one WAFActiveRule")
return nil, NewFieldError("Rules").Message("expect at least one WAFActiveRule")
}

path := fmt.Sprintf("/waf/firewalls/%s/versions/%d/active-rules", i.WAFID, i.WAFVersionNumber)
Expand Down Expand Up @@ -245,7 +245,7 @@ type BatchModificationWAFActiveRulesInput struct {
func (c *Client) BatchModificationWAFActiveRules(i *BatchModificationWAFActiveRulesInput) ([]*WAFActiveRule, error) {

if len(i.Rules) > BatchModifyMaximumOperations {
return nil, NewFieldError("Rules").Custom("batch modify maximum operations exceeded")
return nil, NewFieldError("Rules").Message("batch modify maximum operations exceeded")
}

switch i.OP {
Expand Down Expand Up @@ -288,7 +288,7 @@ func (c *Client) DeleteWAFActiveRules(i *DeleteWAFActiveRulesInput) error {
}

if len(i.Rules) == 0 {
return NewFieldError("Rules").Custom("expect at least one WAFActiveRule")
return NewFieldError("Rules").Message("expect at least one WAFActiveRule")
}

path := fmt.Sprintf("/waf/firewalls/%s/versions/%d/active-rules", i.WAFID, i.WAFVersionNumber)
Expand Down

0 comments on commit 8adf1e8

Please sign in to comment.