Skip to content

Commit

Permalink
fix: Azure provider error parsing
Browse files Browse the repository at this point in the history
Fixes an issue in the cleanup of azure error messages that could cause a
panic.

Signed-off-by: Michael Nairn <[email protected]>
  • Loading branch information
mikenairn committed Nov 22, 2024
1 parent 929fe20 commit 397dda5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 5 additions & 3 deletions internal/external-dns/provider/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,10 @@ func CleanAzureError(err error) error {
reg := regexp.MustCompile(`\"message\": \".*\"`)
errMsg := err.Error()
msg := reg.FindString(errMsg)
msgBits := strings.SplitAfterN(msg, ":", 2)
msgBits = strings.Split(msgBits[1], `"`)
msg = msgBits[1]
if msgBits := strings.SplitAfterN(msg, ":", 2); len(msgBits) > 1 {
if msgBits = strings.Split(msgBits[1], `"`); len(msgBits) > 1 {
msg = msgBits[1]
}
}
return errors.New(msg)
}
8 changes: 8 additions & 0 deletions internal/external-dns/provider/azure/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,14 @@ func TestCleanAzureError(t *testing.T) {
Expect(err.Error()).To(Equal("The following locations specified in the geoMapping property for endpoint ‘foo-example-com’ are not supported: NOTAGEOCODE. For a list of supported locations, see the Traffic Manager documentation."))
},
},
{
name: "cleans up error that doesn't match expected format",
err: fmt.Errorf("some other error"),
Verify: func(err error) {
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(Equal(""))
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 397dda5

Please sign in to comment.