Skip to content

Commit

Permalink
Merge pull request #782 from cloudflare/fix-api-error-handling
Browse files Browse the repository at this point in the history
Improve API error handling for `generate`
  • Loading branch information
jacobbednarz authored Jan 23, 2025
2 parents 8f9fc56 + af03ef9 commit 387c96e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/app/cf-terraforming/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -134,6 +135,13 @@ func generateResources() func(cmd *cobra.Command, args []string) {

err := client.Get(context.Background(), endpoint, nil, &result)
if err != nil {
var apierr *cloudflare.Error
if errors.As(err, &apierr) {
if apierr.StatusCode == http.StatusNotFound {
log.Debugf("no resources found at %s. skipping...", endpoint)
continue
}
}
log.Fatalf("failed to fetch API endpoint: %s", err)
}

Expand All @@ -143,6 +151,10 @@ func generateResources() func(cmd *cobra.Command, args []string) {
}

value := gjson.Get(string(body), "result")
if value.Type == gjson.Null {
log.Debugf("no result found at %s. skipping...", endpoint)
continue
}
err = json.Unmarshal([]byte(value.String()), &jsonStructData)
if err != nil {
log.Fatalf("failed to unmarshal result: %s", err)
Expand Down

0 comments on commit 387c96e

Please sign in to comment.