-
Notifications
You must be signed in to change notification settings - Fork 37
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
feat(webapp): add go controller for cves endpoint #1193
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"fmt" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
@@ -105,6 +106,25 @@ func respStatusError(c *gin.Context, code int, err error) { | |
}) | ||
} | ||
|
||
func processBadRequestErrMessage(err error) error { | ||
errMessage := err.Error() | ||
if strings.HasPrefix(errMessage, "parsing time") { | ||
parts := strings.Split(errMessage, `"`) | ||
if len(parts) < 2 { | ||
return errors.New("Wrong date format (not ISO format with timezone)") | ||
} | ||
return errors.New("Wrong date format (not ISO format with timezone): " + parts[1]) | ||
} | ||
if strings.HasSuffix(errMessage, "looking for beginning of value") { | ||
parts := strings.Split(errMessage, ` `) | ||
if len(parts) < 3 { | ||
return errors.New("malformed input") | ||
} | ||
return errors.New("malformed input: invalid character " + parts[2]) | ||
} | ||
return err | ||
} | ||
|
||
Comment on lines
+109
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parsing errors based on error message and then changing the error message is not optimal. I'd prefer wrapping original error.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original error
I will move the logic. Should I also wrap the errors instead? |
||
func LogAndRespError(c *gin.Context, err error) { | ||
if errors.Is(err, vmaas.ErrProcessingInput) { | ||
// if error is from processing the request, we should return 400 | ||
|
@@ -116,6 +136,7 @@ func LogAndRespError(c *gin.Context, err error) { | |
} | ||
|
||
func LogAndRespBadRequest(c *gin.Context, err error) { | ||
err = processBadRequestErrMessage(err) | ||
LogWarn("err", err.Error()) | ||
respStatusError(c, http.StatusBadRequest, err) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package controllers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/redhatinsights/vmaas-lib/vmaas" | ||
"github.com/redhatinsights/vmaas/base/core" | ||
"github.com/redhatinsights/vmaas/base/utils" | ||
) | ||
|
||
func CvesHandler(c *gin.Context) { | ||
if !isCacheLoaded(c) { | ||
return | ||
} | ||
cve := c.Param("cve") | ||
req := vmaas.CvesRequest{Cves: []string{cve}} | ||
|
||
res, err := core.VmaasAPI.Cves(&req) | ||
if err != nil { | ||
utils.LogAndRespError(c, err) | ||
return | ||
} | ||
c.JSON(http.StatusOK, res) | ||
} | ||
|
||
func CvesPostHandler(c *gin.Context) { | ||
if !isCacheLoaded(c) { | ||
return | ||
} | ||
req := vmaas.CvesRequest{} | ||
err := bindValidateJSON(c, &req) | ||
if err != nil { | ||
utils.LogAndRespBadRequest(c, err) | ||
return | ||
} | ||
|
||
cves, err := core.VmaasAPI.Cves(&req) | ||
if err != nil { | ||
utils.LogAndRespError(c, err) | ||
return | ||
} | ||
c.JSON(http.StatusOK, cves) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the purpose of splitting the error message? When it has only 1
"
then the error will beWrong date format (not ISO format with timezone
and when there are more"
then the second part is wrapped by the new message and other parts are discarded?If the point is to only wrap the current error, you can use
errors.Wrap
fromgithub.com/pkg/errors
which is already used in this module or https://pkg.go.dev/errors#JoinThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose is to get the original timestamp being parsed, the error message without one is just a fallback.