Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use access json header / extend error message parsing for stash #89

Merged
merged 1 commit into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
*.out
_*.md
.vscode
.idea
cmd
15 changes: 11 additions & 4 deletions scm/driver/stash/stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/url"
"strings"
Expand Down Expand Up @@ -64,15 +65,16 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface
req := &scm.Request{
Method: method,
Path: path,
Header: map[string][]string{
"Accept": {"application/json"},
},
}
// if we are posting or putting data, we need to
// write it to the body of the request.
if in != nil {
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(in)
req.Header = map[string][]string{
"Content-Type": {"application/json"},
}
req.Header["Content-Type"] = []string{"application/json"}
req.Body = buf
}

Expand Down Expand Up @@ -121,7 +123,9 @@ type pagination struct {

// Error represents a Stash error.
type Error struct {
Errors []struct {
Message string `json:"message"`
Status int `json:"status-code"`
Errors []struct {
Message string `json:"message"`
ExceptionName string `json:"exceptionName"`
CurrentVersion int `json:"currentVersion"`
Expand All @@ -131,6 +135,9 @@ type Error struct {

func (e *Error) Error() string {
if len(e.Errors) == 0 {
if len(e.Message) > 0 {
return fmt.Sprintf("bitbucket: status: %d message: %s", e.Status, e.Message)
}
return "bitbucket: undefined error"
}
return e.Errors[0].Message
Expand Down