Skip to content
Closed
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<a name="unreleased"></a>
## [Unreleased]
### Feat
- improve error handling and validation in API handlers (`application.go`, `audit.go`)
- update Swagger comments for error responses


<a name="v2.2.0"></a>
Expand Down
43 changes: 38 additions & 5 deletions manager/handlers/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ func (h *Handlers) CreateApplication(ctx *gin.Context) {
return
}

if h.service == nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"errors": "service unavailable"})
return
}

application, err := h.service.CreateApplication(ctx.Request.Context(), json)
if err != nil {
ctx.Error(err) // nolint: errcheck
ctx.JSON(http.StatusInternalServerError, gin.H{"errors": err.Error()})
return
}

Expand All @@ -71,8 +76,13 @@ func (h *Handlers) DestroyApplication(ctx *gin.Context) {
return
}

if h.service == nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"errors": "service unavailable"})
return
}

if err := h.service.DestroyApplication(ctx.Request.Context(), params.ID); err != nil {
ctx.Error(err) // nolint: errcheck
ctx.JSON(http.StatusInternalServerError, gin.H{"errors": err.Error()})
return
}

Expand Down Expand Up @@ -104,9 +114,14 @@ func (h *Handlers) UpdateApplication(ctx *gin.Context) {
return
}

if h.service == nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"errors": "service unavailable"})
return
}

application, err := h.service.UpdateApplication(ctx.Request.Context(), params.ID, json)
if err != nil {
ctx.Error(err) // nolint: errcheck
ctx.JSON(http.StatusInternalServerError, gin.H{"errors": err.Error()})
return
}

Expand All @@ -131,9 +146,14 @@ func (h *Handlers) GetApplication(ctx *gin.Context) {
return
}

if h.service == nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"errors": "service unavailable"})
return
}

application, err := h.service.GetApplication(ctx.Request.Context(), params.ID)
if err != nil {
ctx.Error(err) // nolint: errcheck
ctx.JSON(http.StatusNotFound, gin.H{"errors": err.Error()})
return
}

Expand All @@ -159,10 +179,23 @@ func (h *Handlers) GetApplications(ctx *gin.Context) {
return
}

// Validate pagination parameters
if query.Page < 0 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The parameter verification has been completed in the annotation, refer to https://github.com/dragonflyoss/dragonfly/blob/main/manager/types/audit.go#L40.

query.Page = 0
}
if query.PerPage < 2 || query.PerPage > 50 {
query.PerPage = 10
}

if h.service == nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"errors": "service unavailable"})
return
}

h.setPaginationDefault(&query.Page, &query.PerPage)
applications, count, err := h.service.GetApplications(ctx.Request.Context(), query)
if err != nil {
ctx.Error(err) // nolint: errcheck
ctx.JSON(http.StatusInternalServerError, gin.H{"errors": err.Error()})
return
}

Expand Down
18 changes: 16 additions & 2 deletions manager/handlers/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import (
// @Success 200 {object} []models.Audit
// @Failure 400
// @Failure 404
// @Failure 500
// @Failure 422 {object} map[string]string "Validation error"
// @Failure 500 {object} map[string]string "Internal server error"
// @Router /api/v1/audits [get]
func (h *Handlers) GetAudits(ctx *gin.Context) {
var query types.GetAuditsQuery
Expand All @@ -45,10 +46,23 @@ func (h *Handlers) GetAudits(ctx *gin.Context) {
return
}

// Validate pagination parameters
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The parameter verification has been completed in the annotation, refer to https://github.com/dragonflyoss/dragonfly/blob/main/manager/types/audit.go#L40.

if query.Page < 0 {
query.Page = 0
}
if query.PerPage < 2 || query.PerPage > 50 {
query.PerPage = 10
}

if h.service == nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"errors": "service unavailable"})
return
}

h.setPaginationDefault(&query.Page, &query.PerPage)
audits, count, err := h.service.GetAudits(ctx.Request.Context(), query)
if err != nil {
ctx.Error(err) //nolint: errcheck
ctx.JSON(http.StatusInternalServerError, gin.H{"errors": err.Error()})
return
}

Expand Down
Loading