Skip to content

Commit

Permalink
refactor: err typing for better client assertion (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgejr568 committed Oct 11, 2023
1 parent 042c33e commit a770741
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
9 changes: 4 additions & 5 deletions api_keys.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package resend

import (
"errors"
"net/http"
)

Expand Down Expand Up @@ -44,7 +43,7 @@ func (s *ApiKeysSvcImpl) Create(params *CreateApiKeyRequest) (CreateApiKeyRespon
// Prepare request
req, err := s.client.NewRequest(http.MethodPost, path, params)
if err != nil {
return CreateApiKeyResponse{}, errors.New("[ERROR]: Failed to create ApiKeys.Create request")
return CreateApiKeyResponse{}, ErrFailedToCreateApiKeysCreateRequest
}

// Build response recipient obj
Expand All @@ -60,15 +59,15 @@ func (s *ApiKeysSvcImpl) Create(params *CreateApiKeyRequest) (CreateApiKeyRespon
return *apiKeysResp, nil
}

// List list all API Keys in the project
// List all API Keys in the project
// https://resend.com/docs/api-reference/api-keys/list-api-keys
func (s *ApiKeysSvcImpl) List() (ListApiKeysResponse, error) {
path := "api-keys"

// Prepare request
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return ListApiKeysResponse{}, errors.New("[ERROR]: Failed to create ApiKeys.List request")
return ListApiKeysResponse{}, ErrFailedToCreateApiKeysListRequest
}

// Build response recipient obj
Expand All @@ -92,7 +91,7 @@ func (s *ApiKeysSvcImpl) Remove(apiKeyId string) (bool, error) {
// Prepare request
req, err := s.client.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return false, errors.New("[ERROR]: Failed to create ApiKeys.List request")
return false, ErrFailedToCreateApiKeysRemoveRequest
}

// Send Request
Expand Down
5 changes: 2 additions & 3 deletions emails.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package resend

import (
"encoding/json"
"errors"
"net/http"
)

Expand Down Expand Up @@ -97,7 +96,7 @@ func (s *EmailsSvcImpl) Send(params *SendEmailRequest) (*SendEmailResponse, erro
// Prepare request
req, err := s.client.NewRequest(http.MethodPost, path, params)
if err != nil {
return nil, errors.New("[ERROR]: Failed to create SendEmail request")
return nil, ErrFailedToCreateEmailsSendRequest
}

// Build response recipient obj
Expand All @@ -121,7 +120,7 @@ func (s *EmailsSvcImpl) Get(emailID string) (*Email, error) {
// Prepare request
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, errors.New("[ERROR]: Failed to create GetEmail request")
return nil, ErrFailedToCreateEmailsGetRequest
}

// Build response recipient obj
Expand Down
17 changes: 17 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package resend

import "errors"

// ApiKeySvc errors
var (
ErrFailedToCreateApiKeysCreateRequest = errors.New("[ERROR]: Failed to create ApiKeys.Create request")
ErrFailedToCreateApiKeysListRequest = errors.New("[ERROR]: Failed to create ApiKeys.List request")
ErrFailedToCreateApiKeysRemoveRequest = errors.New("[ERROR]: Failed to create ApiKeys.Remove request")
)

// EmailsSvc errors

var (
ErrFailedToCreateEmailsSendRequest = errors.New("[ERROR]: Failed to create SendEmail request")
ErrFailedToCreateEmailsGetRequest = errors.New("[ERROR]: Failed to create GetEmail request")
)

0 comments on commit a770741

Please sign in to comment.