Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cloudflare): use softerror on internal server error with api #4931

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading