Skip to content

Commit 945a2da

Browse files
authored
Merge pull request #87 from ovh/dev/aamstutz/handle-iam-errors
feat: Add missing IAM permissions in error string
2 parents c71b450 + c03f290 commit 945a2da

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

ovh/error.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,22 @@ func (err APIError) Error() string {
3535
fmt.Fprint(&sb, err.Class, ": ")
3636
}
3737

38+
// Catch missing IAM permissions, if any
39+
var missingIAMActionsDetails []string
40+
if missingAuthenticationActions, ok := err.Details["unauthorizedActionsByAuthentication"]; ok && missingAuthenticationActions != "" {
41+
missingIAMActionsDetails = append(missingIAMActionsDetails, missingAuthenticationActions)
42+
}
43+
if missingIAMActions, ok := err.Details["unauthorizedActionsByIAM"]; ok && missingIAMActions != "" {
44+
missingIAMActionsDetails = append(missingIAMActionsDetails, missingIAMActions)
45+
}
46+
47+
message := err.Message
48+
if len(missingIAMActionsDetails) > 0 {
49+
message += fmt.Sprintf(" (missing IAM permissions: %s)", strings.Join(missingIAMActionsDetails, ", "))
50+
}
51+
3852
// Real error message, quoted
39-
fmt.Fprintf(&sb, "%q", err.Message)
53+
fmt.Fprintf(&sb, "%q", message)
4054

4155
// QueryID if any
4256
if err.QueryID != "" {

ovh/error_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,33 @@ func TestErrorString(t *testing.T) {
3131
td.Cmp(t, err.Error(), expected)
3232
td.Cmp(t, err.String(), expected)
3333

34+
err = APIError{
35+
Code: http.StatusForbidden,
36+
Message: `User not granted for this request`,
37+
Class: "Client::Forbidden",
38+
QueryID: "EU.ext-99.foobar",
39+
Details: map[string]string{
40+
"unauthorizedActionsByAuthentication": "",
41+
"unauthorizedActionsByIAM": "account:apiovh:me/installationTemplate/get",
42+
},
43+
}
44+
45+
expected = `OVHcloud API error (status code 403): Client::Forbidden: "User not granted for this request (missing IAM permissions: account:apiovh:me/installationTemplate/get)" (X-OVH-Query-Id: EU.ext-99.foobar)`
46+
td.Cmp(t, err.Error(), expected)
47+
td.Cmp(t, err.String(), expected)
48+
49+
err = APIError{
50+
Code: http.StatusForbidden,
51+
Message: `User not granted for this request`,
52+
Class: "Client::Forbidden",
53+
QueryID: "EU.ext-99.foobar",
54+
Details: map[string]string{
55+
"unauthorizedActionsByAuthentication": "account:apiovh:me/accessRestriction/ip/get",
56+
"unauthorizedActionsByIAM": "account:apiovh:me/installationTemplate/get",
57+
},
58+
}
59+
60+
expected = `OVHcloud API error (status code 403): Client::Forbidden: "User not granted for this request (missing IAM permissions: account:apiovh:me/accessRestriction/ip/get, account:apiovh:me/installationTemplate/get)" (X-OVH-Query-Id: EU.ext-99.foobar)`
61+
td.Cmp(t, err.Error(), expected)
62+
td.Cmp(t, err.String(), expected)
3463
}

0 commit comments

Comments
 (0)