Skip to content

Commit

Permalink
errors: fix representation of empty lists
Browse files Browse the repository at this point in the history
  • Loading branch information
talal committed Mar 26, 2020
1 parent bf2c1db commit e87d5af
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions internal/api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,15 @@ func (h handler) GetResourceScrapeErrors(w http.ResponseWriter, r *http.Request)
return
}

var result struct {
ResourceScrapeErrors []ResourceScrapeError `json:"resource_scrape_errors"`
}
resScrapeErrs := []ResourceScrapeError{}
for _, res := range dbResources {
projectID := ""
// .ScopeUUID is either a domain- or project UUID.
if res.ScopeUUID != res.DomainUUID {
projectID = res.ScopeUUID
}

result.ResourceScrapeErrors = append(result.ResourceScrapeErrors,
resScrapeErrs = append(resScrapeErrs,
ResourceScrapeError{
ProjectUUID: projectID,
DomainUUID: res.DomainUUID,
Expand All @@ -95,7 +93,9 @@ func (h handler) GetResourceScrapeErrors(w http.ResponseWriter, r *http.Request)
})
}

respondwith.JSON(w, http.StatusOK, result)
respondwith.JSON(w, http.StatusOK, struct {
ResourceScrapeErrors []ResourceScrapeError `json:"resource_scrape_errors,keepempty"`
}{resScrapeErrs})
}

func (h handler) GetAssetScrapeErrors(w http.ResponseWriter, r *http.Request) {
Expand All @@ -108,17 +108,14 @@ func (h handler) GetAssetScrapeErrors(w http.ResponseWriter, r *http.Request) {
return
}

var result struct {
AssetScrapeErrors []AssetError `json:"asset_scrape_errors"`
}

var dbResources []db.Resource
_, err := h.DB.Select(&dbResources,
`SELECT * FROM resources ORDER BY id`)
if respondwith.ErrorText(w, err) {
return
}

assetScrapeErrs := []AssetError{}
for _, res := range dbResources {
var dbAssets []db.Asset
_, err := h.DB.Select(&dbAssets, `
Expand All @@ -137,7 +134,7 @@ func (h handler) GetAssetScrapeErrors(w http.ResponseWriter, r *http.Request) {
}

for _, a := range dbAssets {
result.AssetScrapeErrors = append(result.AssetScrapeErrors,
assetScrapeErrs = append(assetScrapeErrs,
AssetError{
AssetUUID: a.UUID,
ProjectUUID: projectID,
Expand All @@ -151,7 +148,9 @@ func (h handler) GetAssetScrapeErrors(w http.ResponseWriter, r *http.Request) {
}
}

respondwith.JSON(w, http.StatusOK, result)
respondwith.JSON(w, http.StatusOK, struct {
AssetScrapeErrors []AssetError `json:"asset_scrape_errors,keepempty"`
}{assetScrapeErrs})
}

func (h handler) GetAssetResizeErrors(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit e87d5af

Please sign in to comment.