Skip to content

Commit

Permalink
Merge pull request #2 from trocco-io/refine-error-message
Browse files Browse the repository at this point in the history
Refined error messages when API error occur.
  • Loading branch information
gtnao authored Aug 10, 2024
2 parents c70c917 + 6003a79 commit 58f640c
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
## 0.1.0
## 0.1.2

CHORE:

- Refined error messages when API error occur.

## 0.1.1

FEATURES:

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Terraform Provider TROCCO (Terraform Plugin Framework)

The [TROCCO Terraform Provider](https://registry.terraform.io/providers/trocco-io/trocco/latest) enables the management of [TROCCO](https://trocco.io/lp/index.html) resources using the TROCCO API feature, which is available only with our paid plans.

## Important

The API used by this Terraform Provider is currently in beta. The request and response data structures are subject to change. Consequently, the functionality and interface of the Terraform Provider may also change. Please use with caution.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The TROCCO Terraform Provider enables the management of TROCCO resources using t
terraform {
required_providers {
trocco = {
source = "registry.terraform.io/primenumber-dev/trocco"
source = "registry.terraform.io/trocco-io/trocco"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
trocco = {
source = "registry.terraform.io/primenumber-dev/trocco"
source = "registry.terraform.io/trocco-io/trocco"
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func NewDevTroccoClient(apiKey, baseURL string) *TroccoClient {
}
}

type ErrorOutput struct {
Error string `json:"error"`
}

func (client *TroccoClient) do(
method, path string, input interface{}, output interface{},
) error {
Expand All @@ -89,6 +93,18 @@ func (client *TroccoClient) do(
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
var errorOutput ErrorOutput
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(respBody, &errorOutput)
if err != nil {
return fmt.Errorf(resp.Status)
}
return fmt.Errorf(errorOutput.Error)
}
if output == nil {
return nil
}
Expand Down
41 changes: 41 additions & 0 deletions internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,44 @@ func TestDoWithOutput(t *testing.T) {
t.Errorf("Expected output.Name to be test, got %s", output.Name)
}
}

func TestDoWithErrorEmptyOutput(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte(""))
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
}))
defer server.Close()
client := NewDevTroccoClient("1234567890", server.URL)
type TestBody struct {
Name string `json:"name"`
}
output := TestBody{}
err := client.do("POST", "/api", nil, &output)
if err.Error() != "400 Bad Request" {
t.Errorf("Expected error to be Bad Request, got %s", err)
}
}

func TestDoWithErrorJSONOutput(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte(`{"error":"You are not authorized"}`))
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
}))
defer server.Close()
client := NewDevTroccoClient("1234567890", server.URL)
type TestBody struct {
Name string `json:"name"`
}
output := TestBody{}
err := client.do("POST", "/api", nil, &output)
if err.Error() != "You are not authorized" {
t.Errorf("Expected error to be You are not authorized, got %s", err)
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
flag.Parse()

opts := providerserver.ServeOpts{
Address: "registry.terraform.io/primenumber-dev/trocco",
Address: "registry.terraform.io/trocco-io/trocco",
Debug: debug,
}

Expand Down

0 comments on commit 58f640c

Please sign in to comment.