@@ -6,23 +6,31 @@ import (
66 "net/http"
77
88 "github.com/DrFaust92/bitbucket-go-client"
9+ "golang.org/x/oauth2"
910)
1011
12+ type ClientScopeError struct {
13+ Error struct {
14+ Message string `json:"message"`
15+ Detail struct {
16+ Required []string `json:"required"`
17+ Granted []string `json:"granted"`
18+ } `json:"detail"`
19+ } `json:"error"`
20+ }
21+
1122func handleClientError (httpResponse * http.Response , err error ) error {
23+ if oauthError , ok := err .(* oauth2.RetrieveError ); ok {
24+ return fmt .Errorf ("%s: %s" , oauthError .Response .Status , oauthError .ErrorDescription )
25+ }
26+
1227 if httpResponse == nil || httpResponse .StatusCode < 400 {
1328 return nil
1429 }
1530
1631 clientHttpError , ok := err .(bitbucket.GenericSwaggerError )
1732 if ok {
18- var errorBody string
19- var bitbucketHttpError bitbucket.ModelError
20- if err := json .Unmarshal (clientHttpError .Body (), & bitbucketHttpError ); err != nil {
21- errorBody = string (clientHttpError .Body ()[:])
22- } else {
23- errorBody = bitbucketHttpError .Error_ .Message
24- }
25-
33+ errorBody := extractErrorMessage (clientHttpError .Body ())
2634 return fmt .Errorf ("%s: %s" , httpResponse .Status , errorBody )
2735 }
2836
@@ -32,3 +40,20 @@ func handleClientError(httpResponse *http.Response, err error) error {
3240
3341 return nil
3442}
43+
44+ func extractErrorMessage (body []byte ) string {
45+ var bitbucketHttpError bitbucket.ModelError
46+ if err := json .Unmarshal (body , & bitbucketHttpError ); err == nil {
47+ return bitbucketHttpError .Error_ .Message
48+ }
49+
50+ var clientScopeErr ClientScopeError
51+ if err := json .Unmarshal (body , & clientScopeErr ); err == nil {
52+ message := clientScopeErr .Error .Message
53+ required := clientScopeErr .Error .Detail .Required
54+ granted := clientScopeErr .Error .Detail .Granted
55+ return fmt .Sprintf ("%s Required: %v Granted: %v" , message , required , granted )
56+ }
57+
58+ return string (body [:])
59+ }
0 commit comments