Skip to content

Commit

Permalink
Gracefully handle 500s from pivnet. [#133519059]
Browse files Browse the repository at this point in the history
- the structure of the json body is different and we need to handle that special case.
  • Loading branch information
robdimsdale committed Nov 5, 2016
1 parent b87e52d commit edca8e3
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 10 deletions.
37 changes: 27 additions & 10 deletions pivnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ const (
)

type pivnetErr struct {
Status int `json:"status"`
Message string `json:"message"`
Errors []string `json:"errors"`
}

type pivnetInternalServerErr struct {
Error string `json:"error"`
}

type ErrPivnetOther struct {
ResponseCode int `json:"response_code" yaml:"response_code"`
Message string `json:"message" yaml:"message"`
Expand Down Expand Up @@ -210,9 +213,23 @@ func (c Client) MakeRequest(

if expectedStatusCode > 0 && resp.StatusCode != expectedStatusCode {
var pErr pivnetErr
err = json.Unmarshal(b, &pErr)
if err != nil {
return nil, nil, err

// We have to handle 500 differently because it has a different structure
if resp.StatusCode == http.StatusInternalServerError {
var internalServerError pivnetInternalServerErr
err = json.Unmarshal(b, &internalServerError)
if err != nil {
return nil, nil, err
}

pErr = pivnetErr{
Message: internalServerError.Error,
}
} else {
err = json.Unmarshal(b, &pErr)
if err != nil {
return nil, nil, err
}
}

switch resp.StatusCode {
Expand All @@ -222,12 +239,12 @@ func (c Client) MakeRequest(
return nil, nil, newErrNotFound(pErr.Message)
case http.StatusUnavailableForLegalReasons:
return nil, nil, newErrUnavailableForLegalReasons()
}

return nil, nil, ErrPivnetOther{
ResponseCode: resp.StatusCode,
Message: pErr.Message,
Errors: pErr.Errors,
default:
return nil, nil, ErrPivnetOther{
ResponseCode: resp.StatusCode,
Message: pErr.Message,
Errors: pErr.Errors,
}
}
}

Expand Down
65 changes: 65 additions & 0 deletions pivnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,71 @@ var _ = Describe("PivnetClient", func() {
})
})

Context("when Pivnet returns a 500", func() {
var (
body []byte
)

BeforeEach(func() {
body = []byte(`{"status":"500","error":"foo message"}`)
})

It("returns an error", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(
"GET",
fmt.Sprintf("%s/foo", apiPrefix),
),
ghttp.RespondWith(http.StatusInternalServerError, body),
),
)

_, _, err := client.MakeRequest(
"GET",
"/foo",
http.StatusOK,
nil,
nil,
)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(
pivnet.ErrPivnetOther{
ResponseCode: http.StatusInternalServerError,
Message: "foo message",
},
))
})

Context("when unmarshalling the response from Pivnet returns an error", func() {
BeforeEach(func() {
body = []byte(`{"error":1234}`)
})

It("returns an error", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(
"GET",
fmt.Sprintf("%s/foo", apiPrefix),
),
ghttp.RespondWith(http.StatusInternalServerError, body),
),
)

_, _, err := client.MakeRequest(
"GET",
"/foo",
http.StatusOK,
nil,
nil,
)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("json: cannot unmarshal"))
})
})
})

Context("when an unexpected status code comes back from Pivnet", func() {
var (
body []byte
Expand Down

0 comments on commit edca8e3

Please sign in to comment.