Skip to content

Commit ae5c668

Browse files
authored
Handle panic while extracting insufficient scopes (#1221)
1 parent cc12611 commit ae5c668

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

internal/cli/api.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"net/http"
99
"net/url"
10+
"regexp"
1011
"strings"
1112

1213
"github.com/spf13/cobra"
@@ -295,8 +296,19 @@ func isInsufficientScopeError(r *http.Response) error {
295296
return nil
296297
}
297298

298-
missingScopes := strings.Split(body.Message, "Insufficient scope, expected any of: ")[1]
299-
recommendedScopeToAdd := strings.Split(missingScopes, ",")[0]
299+
var recommendedScopeToAdd string
300+
301+
parts := strings.Split(body.Message, "Insufficient scope, expected any of: ")
302+
if len(parts) > 1 {
303+
missingScopes := parts[1]
304+
recommendedScopeToAdd = strings.Split(missingScopes, ",")[0]
305+
} else {
306+
re := regexp.MustCompile(`scope: ([\w:,_\s]+)`)
307+
matches := re.FindStringSubmatch(body.Message)
308+
if len(matches) > 1 {
309+
recommendedScopeToAdd = matches[1]
310+
}
311+
}
300312

301313
return fmt.Errorf(
302314
"request failed because access token lacks scope: %s.\n "+

0 commit comments

Comments
 (0)