Skip to content

Commit

Permalink
Create RequestError struct and ErrorResponse
Browse files Browse the repository at this point in the history
- Pass response body of error
- Remove 'unknown' error nomenclature

[#167050837]

Signed-off-by: Joshua Casey <[email protected]>
Co-authored-by: Joshua Casey <[email protected]>
  • Loading branch information
2 people authored and cf-uaa committed Aug 23, 2019
1 parent 347645d commit b2b1dd4
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion generated_client_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion generated_group_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion generated_identityzone_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion generated_mfaprovider_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion generated_user_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion generator/spec.gotemplate
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func test{{.ModelTypeName}}(t *testing.T, when spec.G, it spec.S) {
{{tolower .ModelTypeName}}, err := a.Get{{.ModelTypeName}}("00000000-0000-0000-0000-000000000001")
Expect(err).To(HaveOccurred())
Expect({{tolower .ModelTypeName}}).To(BeNil())
Expect(err.Error()).To(ContainSubstring("An unknown error occurred while calling"))
Expect(err.Error()).To(ContainSubstring("An error occurred while calling"))
})
})

Expand Down
2 changes: 1 addition & 1 deletion groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func testGroupsExtra(t *testing.T, when spec.G, it spec.S) {

_, err := a.GetGroupByName("uaa.admin", "")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("An unknown error"))
Expect(err.Error()).To(ContainSubstring("An error"))
})

it("returns an error when no groups are found", func() {
Expand Down
2 changes: 1 addition & 1 deletion info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func testInfo(t *testing.T, when spec.G, it spec.S) {
it("returns a helpful error", func() {
_, err := a.GetInfo()
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("An unknown error occurred while calling"))
Expect(err.Error()).To(ContainSubstring("An error occurred while calling"))
})
})

Expand Down
2 changes: 1 addition & 1 deletion me_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func testMe(t *testing.T, when spec.G, it spec.S) {
u, err := a.GetMe()
Expect(err).To(HaveOccurred())
Expect(u).To(BeNil())
Expect(err.Error()).To(ContainSubstring("An unknown error occurred while calling"))
Expect(err.Error()).To(ContainSubstring("An error occurred while calling"))
})

it("returns helpful error when /userinfo response can't be parsed", func() {
Expand Down
24 changes: 18 additions & 6 deletions request_errors.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package uaa

import "github.com/pkg/errors"
import (
"fmt"
"github.com/pkg/errors"
)

type RequestError struct {
Url string
ErrorResponse []byte
}

func (r RequestError) Error() string {
return fmt.Sprintf("An error occurred while calling %s", r.Url)
}

func requestErrorWithBody(url string, body []byte) error {
return RequestError{url, body }
}

func requestError(url string) error {
return errors.New("An unknown error occurred while calling " + url)
return errors.Errorf("An error occurred while calling %s", url)
}

func parseError(err error, url string, body []byte) error {
return errors.Wrapf(err, "An unknown error occurred while parsing response from %s. Response was %s", url, string(body))
}

func unknownError() error {
return errors.New("An unknown error occurred")
}
5 changes: 4 additions & 1 deletion roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,13 @@ func (a *API) doAndRead(req *http.Request, needsAuthentication bool) ([]byte, er
if a.Verbose {
fmt.Printf("%v\n\n", err)
}
return nil, unknownError()
return nil, requestError(req.URL.String())
}

if !is2XX(resp.StatusCode) {
if len(bytes) > 0 {
return nil, requestErrorWithBody(req.URL.String(), bytes)
}
return nil, requestError(req.URL.String())
}
return bytes, nil
Expand Down
2 changes: 1 addition & 1 deletion token_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func testTokenKey(t *testing.T, when spec.G, it spec.S) {

_, err := a.TokenKey()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("An unknown error occurred while calling"))
Expect(err.Error()).To(ContainSubstring("An error occurred while calling"))
})

it("returns helpful error when /token_key response can't be parsed", func() {
Expand Down
8 changes: 4 additions & 4 deletions users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func testUsers(t *testing.T, when spec.G, it spec.S) {

_, err := a.GetUserByUsername("marcus", "uaa", "")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("An unknown error"))
Expect(err.Error()).To(ContainSubstring("An error"))
})

it("returns an error if no results are found", func() {
Expand Down Expand Up @@ -214,7 +214,7 @@ func testUsers(t *testing.T, when spec.G, it spec.S) {
})
_, err := a.GetUserByUsername("marcus", "", "")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("An unknown error"))
Expect(err.Error()).To(ContainSubstring("An error"))
})

it("returns an error when no users are found", func() {
Expand Down Expand Up @@ -465,7 +465,7 @@ func testUsers(t *testing.T, when spec.G, it spec.S) {
})
err := a.ActivateUser("fb5f32e1-5cb3-49e6-93df-6df9c8c8bd7", 0)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("An unknown error occurred while calling"))
Expect(err.Error()).To(ContainSubstring("An error occurred while calling"))
Expect(called).To(Equal(1))
})
})
Expand Down Expand Up @@ -508,7 +508,7 @@ func testUsers(t *testing.T, when spec.G, it spec.S) {
})
err := a.DeactivateUser("fb5f32e1-5cb3-49e6-93df-6df9c8c8bd7", 0)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("An unknown error occurred while calling"))
Expect(err.Error()).To(ContainSubstring("An error occurred while calling"))
Expect(called).To(Equal(1))
})
})
Expand Down

0 comments on commit b2b1dd4

Please sign in to comment.