mirror of
https://github.com/ysoftdevs/terraform-provider-bitbucketserver.git
synced 2026-04-20 15:51:20 +02:00
Handle API errors correctly for Bitbucket server
This commit is contained in:
@@ -12,16 +12,25 @@ import (
|
|||||||
|
|
||||||
// Error represents a error from the bitbucket api.
|
// Error represents a error from the bitbucket api.
|
||||||
type Error struct {
|
type Error struct {
|
||||||
APIError struct {
|
Errors []struct {
|
||||||
Message string `json:"message,omitempty"`
|
Context string `json:"context,omitempty"`
|
||||||
} `json:"error,omitempty"`
|
Message string `json:"message,omitempty"`
|
||||||
Type string `json:"type,omitempty"`
|
Exception string `json:"exceptionName,omitempty"`
|
||||||
|
} `json:"errors,omitempty"`
|
||||||
StatusCode int
|
StatusCode int
|
||||||
Endpoint string
|
Endpoint string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Error) Error() string {
|
func (e Error) Error() string {
|
||||||
return fmt.Sprintf("API Error: %d %s %s", e.StatusCode, e.Endpoint, e.APIError.Message)
|
|
||||||
|
var errorMessages = ""
|
||||||
|
if e.Errors != nil {
|
||||||
|
for _, err := range e.Errors {
|
||||||
|
errorMessages += err.Message + "\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("API Error: %d %s %s", e.StatusCode, e.Endpoint, errorMessages)
|
||||||
}
|
}
|
||||||
|
|
||||||
type BitbucketClient struct {
|
type BitbucketClient struct {
|
||||||
@@ -72,11 +81,7 @@ func (c *BitbucketClient) Do(method, endpoint string, payload *bytes.Buffer) (*h
|
|||||||
|
|
||||||
log.Printf("[DEBUG] Resp Body: %s", string(body))
|
log.Printf("[DEBUG] Resp Body: %s", string(body))
|
||||||
|
|
||||||
err = json.Unmarshal(body, &apiError)
|
_ = json.Unmarshal(body, &apiError)
|
||||||
if err != nil {
|
|
||||||
apiError.APIError.Message = string(body)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resp, error(apiError)
|
return resp, error(apiError)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user