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

fix: NewFailureResponse() should return type error #257

Merged
merged 1 commit into from
Sep 10, 2023
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
4 changes: 2 additions & 2 deletions domain/apiresponses/failure_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ type FailureResponse struct {
errorKey string
}

// NewFailureResponse returns a pointer to a new instance of FailureResponse.
// NewFailureResponse returns an error of type FailureResponse.
// err will by default be used as both a logging message and HTTP response description.
// statusCode is the HTTP status code to be returned, must be 4xx or 5xx
// loggerAction is a short description which will be used as the action if the error is logged.
func NewFailureResponse(err error, statusCode int, loggerAction string) *FailureResponse {
func NewFailureResponse(err error, statusCode int, loggerAction string) error {
return &FailureResponse{
error: err,
statusCode: statusCode,
Expand Down
16 changes: 11 additions & 5 deletions domain/apiresponses/failure_responses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ = Describe("FailureResponse", func() {
Describe("ErrorResponse", func() {
It("returns a ErrorResponse containing the error message", func() {
failureResponse := apiresponses.NewFailureResponse(errors.New("my error message"), http.StatusForbidden, "log-key")
failureResponse := asFailureResponse(apiresponses.NewFailureResponse(errors.New("my error message"), http.StatusForbidden, "log-key"))
Expect(failureResponse.ErrorResponse()).To(Equal(apiresponses.ErrorResponse{
Description: "my error message",
}))
Expand Down Expand Up @@ -71,7 +71,7 @@ var _ = Describe("FailureResponse", func() {

Describe("ValidatedStatusCode", func() {
It("returns the status code that was passed in", func() {
failureResponse := apiresponses.NewFailureResponse(errors.New("my error message"), http.StatusForbidden, "log-key")
failureResponse := asFailureResponse(apiresponses.NewFailureResponse(errors.New("my error message"), http.StatusForbidden, "log-key"))
Expect(failureResponse.ValidatedStatusCode(nil)).To(Equal(http.StatusForbidden))
})

Expand All @@ -82,15 +82,15 @@ var _ = Describe("FailureResponse", func() {

Context("when the status code is invalid", func() {
It("returns 500", func() {
failureResponse := apiresponses.NewFailureResponse(errors.New("my error message"), 600, "log-key")
failureResponse := asFailureResponse(apiresponses.NewFailureResponse(errors.New("my error message"), 600, "log-key"))
Expect(failureResponse.ValidatedStatusCode(nil)).To(Equal(http.StatusInternalServerError))
})

It("logs that the status has been changed", func() {
log := gbytes.NewBuffer()
logger := lager.NewLogger("test")
logger.RegisterSink(lager.NewWriterSink(log, lager.DEBUG))
failureResponse := apiresponses.NewFailureResponse(errors.New("my error message"), 600, "log-key")
failureResponse := asFailureResponse(apiresponses.NewFailureResponse(errors.New("my error message"), 600, "log-key"))
failureResponse.ValidatedStatusCode(logger)
Expect(log).To(gbytes.Say("Invalid failure http response code: 600, expected 4xx or 5xx, returning internal server error: 500."))
})
Expand All @@ -104,8 +104,14 @@ var _ = Describe("FailureResponse", func() {
})

It("when error key is provided it returns the logger action that was passed in", func() {
failureResponse := apiresponses.NewFailureResponse(errors.New("my error message"), http.StatusForbidden, "log-key")
failureResponse := asFailureResponse(apiresponses.NewFailureResponse(errors.New("my error message"), http.StatusForbidden, "log-key"))
Expect(failureResponse.LoggerAction()).To(Equal("log-key"))
})
})
})

func asFailureResponse(err error) *apiresponses.FailureResponse {
GinkgoHelper()
Expect(err).To(BeAssignableToTypeOf(&apiresponses.FailureResponse{}))
return err.(*apiresponses.FailureResponse)
}
6 changes: 3 additions & 3 deletions failure_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
type FailureResponse = apiresponses.FailureResponse

// Deprecated: Use github.com/pivotal-cf/brokerapi/domain/apiresponses
// NewFailureResponse returns a pointer to a new instance of FailureResponse.
// NewFailureResponse returns an error of type FailureResponse.
// err will by default be used as both a logging message and HTTP response description.
// statusCode is the HTTP status code to be returned, must be 4xx or 5xx
// loggerAction is a short description which will be used as the action if the error is logged.
func NewFailureResponse(err error, statusCode int, loggerAction string) *FailureResponse {
return (*FailureResponse)(apiresponses.NewFailureResponse(err, statusCode, loggerAction))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: thanks for simplifying the code

func NewFailureResponse(err error, statusCode int, loggerAction string) error {
return apiresponses.NewFailureResponse(err, statusCode, loggerAction)
}

// Deprecated: Use github.com/pivotal-cf/brokerapi/domain/apiresponses
Expand Down
19 changes: 13 additions & 6 deletions failure_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package brokerapi_test

import (
"github.com/pivotal-cf/brokerapi/v10"
"github.com/pivotal-cf/brokerapi/v10/domain/apiresponses"

"errors"

Expand All @@ -31,15 +32,15 @@ import (
var _ = Describe("FailureResponse", func() {
Describe("ErrorResponse", func() {
It("returns a ErrorResponse containing the error message", func() {
failureResponse := brokerapi.NewFailureResponse(errors.New("my error message"), http.StatusForbidden, "log-key")
failureResponse := asFailureResponse(brokerapi.NewFailureResponse(errors.New("my error message"), http.StatusForbidden, "log-key"))
Expect(failureResponse.ErrorResponse()).To(Equal(brokerapi.ErrorResponse{
Description: "my error message",
}))
})

Context("when the error key is provided", func() {
It("returns a ErrorResponse containing the error message and the error key", func() {
failureResponse := brokerapi.NewFailureResponseBuilder(errors.New("my error message"), http.StatusForbidden, "log-key").WithErrorKey("error key").Build()
failureResponse := asFailureResponse(brokerapi.NewFailureResponseBuilder(errors.New("my error message"), http.StatusForbidden, "log-key").WithErrorKey("error key").Build())
Expect(failureResponse.ErrorResponse()).To(Equal(brokerapi.ErrorResponse{
Description: "my error message",
Error: "error key",
Expand Down Expand Up @@ -87,7 +88,7 @@ var _ = Describe("FailureResponse", func() {

Describe("ValidatedStatusCode", func() {
It("returns the status code that was passed in", func() {
failureResponse := brokerapi.NewFailureResponse(errors.New("my error message"), http.StatusForbidden, "log-key")
failureResponse := asFailureResponse(brokerapi.NewFailureResponse(errors.New("my error message"), http.StatusForbidden, "log-key"))
Expect(failureResponse.ValidatedStatusCode(nil)).To(Equal(http.StatusForbidden))
})

Expand All @@ -98,15 +99,15 @@ var _ = Describe("FailureResponse", func() {

Context("when the status code is invalid", func() {
It("returns 500", func() {
failureResponse := brokerapi.NewFailureResponse(errors.New("my error message"), 600, "log-key")
failureResponse := asFailureResponse(brokerapi.NewFailureResponse(errors.New("my error message"), 600, "log-key"))
Expect(failureResponse.ValidatedStatusCode(nil)).To(Equal(http.StatusInternalServerError))
})

It("logs that the status has been changed", func() {
log := gbytes.NewBuffer()
logger := lager.NewLogger("test")
logger.RegisterSink(lager.NewWriterSink(log, lager.DEBUG))
failureResponse := brokerapi.NewFailureResponse(errors.New("my error message"), 600, "log-key")
failureResponse := asFailureResponse(brokerapi.NewFailureResponse(errors.New("my error message"), 600, "log-key"))
failureResponse.ValidatedStatusCode(logger)
Expect(log).To(gbytes.Say("Invalid failure http response code: 600, expected 4xx or 5xx, returning internal server error: 500."))
})
Expand All @@ -120,8 +121,14 @@ var _ = Describe("FailureResponse", func() {
})

It("when error key is provided it returns the logger action that was passed in", func() {
failureResponse := brokerapi.NewFailureResponse(errors.New("my error message"), http.StatusForbidden, "log-key")
failureResponse := asFailureResponse(brokerapi.NewFailureResponse(errors.New("my error message"), http.StatusForbidden, "log-key"))
Expect(failureResponse.LoggerAction()).To(Equal("log-key"))
})
})
})

func asFailureResponse(err error) *apiresponses.FailureResponse {
GinkgoHelper()
Expect(err).To(BeAssignableToTypeOf(&apiresponses.FailureResponse{}))
return err.(*apiresponses.FailureResponse)
}