Skip to content

Commit

Permalink
service_authorization: Use dedicated errors
Browse files Browse the repository at this point in the history
Instead of re-using amibuous error messages, create and use a dedicated
error when CreateServiceAuthorization is used incorrectly.
  • Loading branch information
noseglid committed Jun 20, 2022
1 parent a386cf6 commit 3b516a2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
8 changes: 8 additions & 0 deletions fastly/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ var ErrMissingServerSideEncryptionKMSKeyID = NewFieldError("ServerSideEncryption
// requires a "ServiceID" key, but one was not set.
var ErrMissingServiceID = NewFieldError("ServiceID")

// ErrMissingServiceAuthorizationsService is an error that is returned when an input struct
// requires a "ServiceAuthorizationService" key, but one was not set.
var ErrMissingServiceAuthorizationsService = NewFieldError("Service").Message("SAService requires an ID")

// ErrMissingServiceAuthorizationsUser is an error that is returned when an input struct
// requires a "ServiceAuthorizationUser" key, but one was not set.
var ErrMissingServiceAuthorizationsUser = NewFieldError("User").Message("SAUser requires an ID")

// ErrMissingUserID is an error that is returned when an input struct
// requires a "UserID" key, but one was not set
var ErrMissingUserID = NewFieldError("UserID")
Expand Down
4 changes: 2 additions & 2 deletions fastly/service_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ type CreateServiceAuthorizationInput struct {
// CreateServiceAuthorization creates a new service authorization granting granular service and user permissions.
func (c *Client) CreateServiceAuthorization(i *CreateServiceAuthorizationInput) (*ServiceAuthorization, error) {
if i.Service == nil || i.Service.ID == "" {
return nil, ErrMissingServiceID
return nil, ErrMissingServiceAuthorizationsService
}
if i.User == nil || i.User.ID == "" {
return nil, ErrMissingUserID
return nil, ErrMissingServiceAuthorizationsUser
}

resp, err := c.PostJSONAPI("/service-authorizations", i, nil)
Expand Down
4 changes: 2 additions & 2 deletions fastly/service_authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ func TestClient_CreateServiceAuthorization_validation(t *testing.T) {
Service: &SAService{ID: ""},
User: &SAUser{ID: ""},
})
if err != ErrMissingServiceID {
if err != ErrMissingServiceAuthorizationsService {
t.Errorf("bad error: %s", err)
}

_, err = testClient.CreateServiceAuthorization(&CreateServiceAuthorizationInput{
Service: &SAService{ID: "my-service-id"},
User: &SAUser{ID: ""},
})
if err != ErrMissingUserID {
if err != ErrMissingServiceAuthorizationsUser {
t.Errorf("bad error: %s", err)
}
}
Expand Down

0 comments on commit 3b516a2

Please sign in to comment.