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

HMS-2837 fix: middleware validation error response #23

Merged
merged 1 commit into from
Oct 26, 2023
Merged
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
28 changes: 14 additions & 14 deletions internal/infrastructure/middleware/validate_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,7 @@ func RequestResponseValidatorWithConfig(config *RequestResponseValidatorConfig)
requestValidationInput,
)
if err != nil {
c.Response().Header().Set(echo.HeaderContentType, "text/plain")
c.String(http.StatusBadRequest, err.Error())
return nil // stop processing
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
}

Expand All @@ -273,6 +271,12 @@ func RequestResponseValidatorWithConfig(config *RequestResponseValidatorConfig)
c.Response().Writer = resRec

defer func() {
// reset the response, using the original ResponseWriter
c.SetResponse(echo.NewResponse(rw, c.Echo()))
}()

err = next(c)
if err == nil {
responseValidationInput := &openapi3filter.ResponseValidationInput{
RequestValidationInput: requestValidationInput,
Status: resRec.status,
Expand All @@ -285,19 +289,15 @@ func RequestResponseValidatorWithConfig(config *RequestResponseValidatorConfig)
responseValidationInput,
); err != nil {
// write error response
c.Response().Header().Set(echo.HeaderContentType, "text/plain")
c.String(http.StatusInternalServerError, err.Error())
} else {
// Write original response
rw.WriteHeader(resRec.status)
resRec.buffer.WriteTo(rw)
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
}()

defer func() {
// reset the response, using the original ResponseWriter
c.SetResponse(echo.NewResponse(rw, c.Echo()))
}()
// Write original response
rw.WriteHeader(resRec.status)
resRec.buffer.WriteTo(rw)
}

return err
}

return next(c)
Expand Down
Loading