Skip to content

Commit 21461eb

Browse files
authored
Merge pull request #3067 from 08volt/user-error
fix: categorize IP out of range error as UserError
2 parents 83d9931 + e452b39 commit 21461eb

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

pkg/l4/resources/user_error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func IsUserError(err error) bool {
1414

1515
return utils.IsNetworkTierError(err) ||
1616
utils.IsIPConfigurationError(err) ||
17+
utils.IsIPOutOfRangeError(err) ||
1718
utils.IsConflictingPortsConfigurationError(err) ||
1819
utils.IsInvalidSubnetConfigurationError(err) ||
1920
utils.IsInvalidLoadBalancerSourceRangesSpecError(err) ||

pkg/l4/resources/user_error_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ func TestIsUserError(t *testing.T) {
6363
},
6464
want: true,
6565
},
66+
{
67+
68+
err: &googleapi.Error{Code: 400, Message: "Requested internal IP address is outside the network/subnetwork range"},
69+
want: true,
70+
},
6671
}
6772
for _, tC := range testCases {
6873
tC := tC

pkg/utils/utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ func IsIPConfigurationError(err error) bool {
279279
return errors.As(err, &ipConfigError)
280280
}
281281

282+
// IsIPOutOfRangeError checks if error is a GCE IP out of range error.
283+
func IsIPOutOfRangeError(err error) bool {
284+
return IsHTTPErrorCode(err, http.StatusBadRequest) && strings.Contains(err.Error(), "Requested internal IP address is outside the network/subnetwork range")
285+
}
286+
282287
// IsConflictingPortsConfigurationError checks if wrapped error is an conflicting ports configuration error.
283288
func IsConflictingPortsConfigurationError(err error) bool {
284289
var portsConflictConfigError *ConflictingPortsConfigurationError

pkg/utils/utils_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,3 +1917,40 @@ func TestIsConstraintViolationError(t *testing.T) {
19171917
})
19181918
}
19191919
}
1920+
1921+
func TestIsIPOutOfRangeError(t *testing.T) {
1922+
testCases := []struct {
1923+
desc string
1924+
err error
1925+
want bool
1926+
}{
1927+
{
1928+
desc: "nil error",
1929+
err: nil,
1930+
want: false,
1931+
},
1932+
{
1933+
desc: "other googleapi error",
1934+
err: &googleapi.Error{Code: http.StatusBadRequest, Message: "Some other error"},
1935+
want: false,
1936+
},
1937+
{
1938+
desc: "not a googleapi error",
1939+
err: fmt.Errorf("Requested internal IP address is outside the network/subnetwork range"),
1940+
want: false,
1941+
},
1942+
{
1943+
desc: "correct googleapi error",
1944+
err: &googleapi.Error{Code: http.StatusBadRequest, Message: "Invalid value for field 'resource.ipAddress': '10.24.120.53'. Requested internal IP address is outside the network/subnetwork range., invalid"},
1945+
want: true,
1946+
},
1947+
}
1948+
1949+
for _, tc := range testCases {
1950+
t.Run(tc.desc, func(t *testing.T) {
1951+
if got := utils.IsIPOutOfRangeError(tc.err); got != tc.want {
1952+
t.Errorf("IsIPOutOfRangeError(%v) = %v, want %v", tc.err, got, tc.want)
1953+
}
1954+
})
1955+
}
1956+
}

0 commit comments

Comments
 (0)