Skip to content

Fix resolve problem details with string message #7

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

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
7 changes: 5 additions & 2 deletions problem_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ func Map[T error](funcProblem func() ProblemDetailErr) {

// ResolveProblemDetails retrieve and resolve error with format problem details error
func ResolveProblemDetails(w http.ResponseWriter, r *http.Request, err error) (ProblemDetailErr, error) {

var statusCode int = http.StatusInternalServerError
var echoError *echo.HTTPError
var ginError *gin.Error
Expand All @@ -142,7 +141,11 @@ func ResolveProblemDetails(w http.ResponseWriter, r *http.Request, err error) (P
err = errors.New(fiberError.Message)
} else if errors.As(err, &echoError) {
statusCode = err.(*echo.HTTPError).Code
err = err.(*echo.HTTPError).Message.(error)
if messageErr, ok := err.(*echo.HTTPError).Message.(error); ok {
err = messageErr
} else if messageStr, ok := err.(*echo.HTTPError).Message.(string); ok {
err = errors.New(messageStr)
}
} else if errors.As(err, &ginError) {
var rw, ok = w.(gin.ResponseWriter)
if ok && rw.Written() {
Expand Down
32 changes: 31 additions & 1 deletion problem_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,33 @@ func TestMap_Status_Echo(t *testing.T) {
p, _ := ResolveProblemDetails(c.Response(), c.Request(), err)

assert.Equal(t, c.Response().Status, http.StatusUnauthorized)
assert.Equal(t, err.(*echo.HTTPError).Message.(error).Error(), p.GetDetails())
assert.Equal(t, "We have a specific status code error in our endpoint", p.GetDetails())
assert.Equal(t, "unauthorized", p.GetTitle())
assert.Equal(t, "https://httpstatuses.io/401", p.GetType())
assert.Equal(t, http.StatusUnauthorized, p.GetStatus())
}

func TestMap_Status2_Echo(t *testing.T) {

e := echo.New()
req := httptest.NewRequest(http.MethodGet, "http://echo_endpoint2", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

err := echo_endpoint2b(c)

MapStatus(http.StatusBadGateway, func() ProblemDetailErr {
return &ProblemDetail{
Status: http.StatusUnauthorized,
Title: "unauthorized",
Detail: err.Error(),
}
})

p, _ := ResolveProblemDetails(c.Response(), c.Request(), err)

assert.Equal(t, c.Response().Status, http.StatusUnauthorized)
assert.Equal(t, "We have a specific status code error in our endpoint", p.GetDetails())
assert.Equal(t, "unauthorized", p.GetTitle())
assert.Equal(t, "https://httpstatuses.io/401", p.GetType())
assert.Equal(t, http.StatusUnauthorized, p.GetStatus())
Expand Down Expand Up @@ -397,6 +423,10 @@ func echo_endpoint2(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadGateway, err)
}

func echo_endpoint2b(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadGateway, "We have a specific status code error in our endpoint")
}

func echo_endpoint3(c echo.Context) error {
err := errors.New("We have a unhandeled error in our endpoint")
return err
Expand Down