2017-11-02 19:09:17 +02:00
|
|
|
package jira
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-01-01 01:55:54 +02:00
|
|
|
"strings"
|
2017-11-02 19:09:17 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-05-14 17:18:31 +02:00
|
|
|
// Error message from Jira
|
2017-11-02 19:09:17 +02:00
|
|
|
// See https://docs.atlassian.com/jira/REST/cloud/#error-responses
|
|
|
|
type Error struct {
|
|
|
|
HTTPError error
|
|
|
|
ErrorMessages []string `json:"errorMessages"`
|
|
|
|
Errors map[string]string `json:"errors"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewJiraError creates a new jira Error
|
2017-11-03 01:53:36 +02:00
|
|
|
func NewJiraError(resp *Response, httpError error) error {
|
2017-11-15 03:57:26 +02:00
|
|
|
if resp == nil {
|
|
|
|
return errors.Wrap(httpError, "No response returned")
|
|
|
|
}
|
|
|
|
|
2017-11-02 19:09:17 +02:00
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, httpError.Error())
|
|
|
|
}
|
|
|
|
jerr := Error{HTTPError: httpError}
|
2019-01-01 01:55:54 +02:00
|
|
|
contentType := resp.Header.Get("Content-Type")
|
|
|
|
if strings.HasPrefix(contentType, "application/json") {
|
|
|
|
err = json.Unmarshal(body, &jerr)
|
|
|
|
if err != nil {
|
style: Fix staticcheck (static analysis) errors for this library (#283)
* style: Fix staticcheck errors for "error strings should not be capitalized (ST1005)"
staticcheck is a static analysis tool for go.
It reports several "error strings should not be capitalized (ST1005)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "printf-style function with dynamic format ... (SA1006)"
staticcheck is a static analysis tool for go.
It reports several "printf-style function with dynamic format string and no further arguments should use print-style function instead (SA1006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "type X is unused (U1000)"
staticcheck is a static analysis tool for go.
It reports several "type X is unused (U1000)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "should use X instead (S1003 & SA6005)"
staticcheck is a static analysis tool for go.
It reports several
- should use !bytes.Contains(b, []byte(`"password":"bar"`)) instead (S1003)
- should use strings.EqualFold instead (SA6005)
messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "unnecessary use of fmt.Sprintf (S1039)"
staticcheck is a static analysis tool for go.
It report several "unnecessary use of fmt.Sprintf (S1039)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "this value of X is never used (SA4006)"
staticcheck is a static analysis tool for go.
It report several "this value of X is never used (SA4006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "redundant return statement (S1023)"
staticcheck is a static analysis tool for go.
It report several "redundant return statement (S1023)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "possible nil pointer dereference (SA5011)"
staticcheck is a static analysis tool for go.
It report several
file.go:Line:character: possible nil pointer dereference (SA5011)
file.go:Line:character: this check suggests that the pointer can be nil
messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "this value of X is never used (SA4006)"
staticcheck is a static analysis tool for go.
It report several "this value of X is never used (SA4006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
2020-05-02 23:08:01 +02:00
|
|
|
httpError = errors.Wrap(errors.New("could not parse JSON"), httpError.Error())
|
2019-01-01 01:55:54 +02:00
|
|
|
return errors.Wrap(err, httpError.Error())
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-29 09:12:34 +02:00
|
|
|
if httpError == nil {
|
style: Fix staticcheck (static analysis) errors for this library (#283)
* style: Fix staticcheck errors for "error strings should not be capitalized (ST1005)"
staticcheck is a static analysis tool for go.
It reports several "error strings should not be capitalized (ST1005)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "printf-style function with dynamic format ... (SA1006)"
staticcheck is a static analysis tool for go.
It reports several "printf-style function with dynamic format string and no further arguments should use print-style function instead (SA1006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "type X is unused (U1000)"
staticcheck is a static analysis tool for go.
It reports several "type X is unused (U1000)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "should use X instead (S1003 & SA6005)"
staticcheck is a static analysis tool for go.
It reports several
- should use !bytes.Contains(b, []byte(`"password":"bar"`)) instead (S1003)
- should use strings.EqualFold instead (SA6005)
messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "unnecessary use of fmt.Sprintf (S1039)"
staticcheck is a static analysis tool for go.
It report several "unnecessary use of fmt.Sprintf (S1039)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "this value of X is never used (SA4006)"
staticcheck is a static analysis tool for go.
It report several "this value of X is never used (SA4006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "redundant return statement (S1023)"
staticcheck is a static analysis tool for go.
It report several "redundant return statement (S1023)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "possible nil pointer dereference (SA5011)"
staticcheck is a static analysis tool for go.
It report several
file.go:Line:character: possible nil pointer dereference (SA5011)
file.go:Line:character: this check suggests that the pointer can be nil
messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "this value of X is never used (SA4006)"
staticcheck is a static analysis tool for go.
It report several "this value of X is never used (SA4006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
2020-05-02 23:08:01 +02:00
|
|
|
return fmt.Errorf("got response status %s:%s", resp.Status, string(body))
|
2020-04-29 09:12:34 +02:00
|
|
|
}
|
2019-01-01 01:55:54 +02:00
|
|
|
return errors.Wrap(httpError, fmt.Sprintf("%s: %s", resp.Status, string(body)))
|
2017-11-02 19:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &jerr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error is a short string representing the error
|
|
|
|
func (e *Error) Error() string {
|
|
|
|
if len(e.ErrorMessages) > 0 {
|
|
|
|
// return fmt.Sprintf("%v", e.HTTPError)
|
|
|
|
return fmt.Sprintf("%s: %v", e.ErrorMessages[0], e.HTTPError)
|
|
|
|
}
|
|
|
|
if len(e.Errors) > 0 {
|
|
|
|
for key, value := range e.Errors {
|
|
|
|
return fmt.Sprintf("%s - %s: %v", key, value, e.HTTPError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e.HTTPError.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LongError is a full representation of the error as a string
|
|
|
|
func (e *Error) LongError() string {
|
|
|
|
var msg bytes.Buffer
|
|
|
|
if e.HTTPError != nil {
|
|
|
|
msg.WriteString("Original:\n")
|
|
|
|
msg.WriteString(e.HTTPError.Error())
|
|
|
|
msg.WriteString("\n")
|
|
|
|
}
|
|
|
|
if len(e.ErrorMessages) > 0 {
|
|
|
|
msg.WriteString("Messages:\n")
|
|
|
|
for _, v := range e.ErrorMessages {
|
|
|
|
msg.WriteString(" - ")
|
|
|
|
msg.WriteString(v)
|
|
|
|
msg.WriteString("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(e.Errors) > 0 {
|
|
|
|
for key, value := range e.Errors {
|
|
|
|
msg.WriteString(" - ")
|
|
|
|
msg.WriteString(key)
|
|
|
|
msg.WriteString(" - ")
|
|
|
|
msg.WriteString(value)
|
|
|
|
msg.WriteString("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return msg.String()
|
|
|
|
}
|