Skip to content

Commit

Permalink
fix: soft error on internal server error
Browse files Browse the repository at this point in the history
  • Loading branch information
vishuvenu committed Dec 5, 2024
1 parent 6e9ec7b commit 4b84534
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
5 changes: 3 additions & 2 deletions provider/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -244,7 +245,7 @@ func (p *CloudFlareProvider) Zones(ctx context.Context) ([]cloudflare.Zone, erro
if err != nil {
var apiErr *cloudflare.Error
if errors.As(err, &apiErr) {
if apiErr.ClientRateLimited() {
if apiErr.ClientRateLimited() || apiErr.StatusCode >= http.StatusInternalServerError {
// Handle rate limit error as a soft error
return nil, provider.NewSoftError(err)
}
Expand Down Expand Up @@ -498,7 +499,7 @@ func (p *CloudFlareProvider) listDNSRecordsWithAutoPagination(ctx context.Contex
if err != nil {
var apiErr *cloudflare.Error
if errors.As(err, &apiErr) {
if apiErr.ClientRateLimited() {
if apiErr.ClientRateLimited() || apiErr.StatusCode >= http.StatusInternalServerError {
// Handle rate limit error as a soft error
return nil, provider.NewSoftError(err)
}
Expand Down
38 changes: 35 additions & 3 deletions provider/cloudflare/cloudflare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,26 @@ func TestCloudflareListZonesRateLimited(t *testing.T) {
}
}

func TestCloudflareListZoneInternalErrors(t *testing.T) {
// Create a mock client that returns a internal server error
client := NewMockCloudFlareClient()
client.listZonesContextError = &cloudflare.Error{
StatusCode: 500,
ErrorCodes: []int{20000},
Type: cloudflare.ErrorTypeService,
}
p := &CloudFlareProvider{Client: client}

// Call the Zones function
_, err := p.Zones(context.Background())

// Assert that a soft error was returned
t.Log(err)
if !errors.Is(err, provider.SoftError) {
t.Errorf("expected a internal error")
}
}

func TestCloudflareRecords(t *testing.T) {
client := NewMockCloudFlareClientWithRecords(map[string][]cloudflare.DNSRecord{
"001": ExampleDomain,
Expand Down Expand Up @@ -704,6 +724,18 @@ func TestCloudflareRecords(t *testing.T) {
if !errors.Is(err, provider.SoftError) {
t.Error("expected a rate limit error")
}

client.listZonesContextError = &cloudflare.Error{
StatusCode: 500,
ErrorCodes: []int{10000},
Type: cloudflare.ErrorTypeService,
}
_, err = p.Records(ctx)
// Assert that a soft error was returned
if !errors.Is(err, provider.SoftError) {
t.Error("expected a internal server error")
}

client.listZonesContextError = errors.New("failed to list zones")
_, err = p.Records(ctx)
if err == nil {
Expand Down Expand Up @@ -1312,11 +1344,11 @@ func TestCloudflareComplexUpdate(t *testing.T) {
},
},
{
Name: "UpdateDataLocalizationRegionalHostname",
Name: "UpdateDataLocalizationRegionalHostname",
ZoneId: "001",
RecordData: cloudflare.DNSRecord{
Name: "foobar.bar.com",
TTL: 0,
Name: "foobar.bar.com",
TTL: 0,
Proxiable: false,
},
},
Expand Down

0 comments on commit 4b84534

Please sign in to comment.