Skip to content

Commit

Permalink
Merge pull request #32 from OpenVPN/feature/CAPI-269-dns-record-impro…
Browse files Browse the repository at this point in the history
…vements

Error refacotring
  • Loading branch information
sahaqaa authored Aug 9, 2024
2 parents b70c68b + 79c2d08 commit 4f6f8ac
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
11 changes: 10 additions & 1 deletion cloudconnexa/cloudconnexa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion cloudconnexa/dns_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions cloudconnexa/user_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions cloudconnexa/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4f6f8ac

Please sign in to comment.