diff --git a/cloudconnexa/cloudconnexa.go b/cloudconnexa/cloudconnexa.go index fa08b53..8873ffb 100644 --- a/cloudconnexa/cloudconnexa.go +++ b/cloudconnexa/cloudconnexa.go @@ -48,6 +48,15 @@ type Credentials struct { AccessToken string `json:"access_token"` } +type ErrClientResponse struct { + status int + body string +} + +func (e ErrClientResponse) Error() string { + return fmt.Sprintf("status code: %d, response body: %s", e.status, e.body) +} + func NewClient(baseURL, clientId, clientSecret string) (*Client, error) { if clientId == "" || clientSecret == "" { return nil, ErrCredentialsRequired @@ -127,7 +136,7 @@ func (c *Client) DoRequest(req *http.Request) ([]byte, error) { } if res.StatusCode < 200 || res.StatusCode >= 300 { - return nil, fmt.Errorf("status code: %d, response body: %s", res.StatusCode, string(body)) + return nil, &ErrClientResponse{status: res.StatusCode, body: string(body)} } return body, nil diff --git a/cloudconnexa/dns_records.go b/cloudconnexa/dns_records.go index 11237fd..14c50b0 100644 --- a/cloudconnexa/dns_records.go +++ b/cloudconnexa/dns_records.go @@ -3,10 +3,15 @@ package cloudconnexa import ( "bytes" "encoding/json" + "errors" "fmt" "net/http" ) +var ( + ErrDnsRecordNotFound = errors.New("dns record not found") +) + type DnsRecord struct { Id string `json:"id"` Domain string `json:"domain"` @@ -68,7 +73,7 @@ func (c *DNSRecordsService) GetDnsRecord(recordId string) (*DnsRecord, error) { } page++ } - return nil, fmt.Errorf("DNS record with ID %s not found", recordId) + return nil, ErrDnsRecordNotFound } func (c *DNSRecordsService) Create(record DnsRecord) (*DnsRecord, error) { diff --git a/cloudconnexa/user_groups.go b/cloudconnexa/user_groups.go index 03b6f16..8a14901 100644 --- a/cloudconnexa/user_groups.go +++ b/cloudconnexa/user_groups.go @@ -3,10 +3,15 @@ package cloudconnexa import ( "bytes" "encoding/json" + "errors" "fmt" "net/http" ) +var ( + ErrUserGroupNotFound = errors.New("user group not found") +) + type UserGroupPageResponse struct { Content []UserGroup `json:"content"` NumberOfElements int `json:"numberOfElements"` @@ -81,7 +86,7 @@ func (c *UserGroupsService) GetByName(name string) (*UserGroup, error) { return &ug, nil } } - return nil, fmt.Errorf("group %s does not exist", name) + return nil, ErrUserGroupNotFound } func (c *UserGroupsService) Get(id string) (*UserGroup, error) { @@ -95,7 +100,7 @@ func (c *UserGroupsService) Get(id string) (*UserGroup, error) { return &ug, nil } } - return nil, fmt.Errorf("group %s does not exist", id) + return nil, ErrUserGroupNotFound } func (c *UserGroupsService) Create(userGroup *UserGroup) (*UserGroup, error) { diff --git a/cloudconnexa/users.go b/cloudconnexa/users.go index 7a371ad..74d368b 100644 --- a/cloudconnexa/users.go +++ b/cloudconnexa/users.go @@ -3,10 +3,15 @@ package cloudconnexa import ( "bytes" "encoding/json" + "errors" "fmt" "net/http" ) +var ( + ErrUserNotFound = errors.New("user not found") +) + type User struct { Id string `json:"id"` Username string `json:"username"` @@ -82,7 +87,7 @@ func (c *UsersService) List(username string, role string) (*User, error) { } page++ } - return nil, fmt.Errorf("user with username %s and role %s not found", username, role) + return nil, ErrUserNotFound } func (c *UsersService) Get(userId string) (*User, error) { @@ -130,7 +135,7 @@ func (c *UsersService) GetByUsername(username string) (*User, error) { } page++ } - return nil, fmt.Errorf("user with username %s not found", username) + return nil, ErrUserNotFound } func (c *UsersService) Create(user User) (*User, error) {