[dashboard api] manage error when data in dashboard table is not valid json (#29999)

* retrieve dashboard api

* Apply suggestions from code review

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

* Update pkg/api/dashboard.go

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
ying-jeanne
2021-01-04 14:41:17 +01:00
committed by GitHub
co-authored by Arve Knudsen
parent db67e70ba4
commit 388d00873f
2 changed files with 17 additions and 1 deletions
+14
View File
@@ -54,6 +54,20 @@ func (hs *HTTPServer) GetDashboard(c *models.ReqContext) Response {
return rsp
}
// When dash contains only keys id, uid that means dashboard data is not valid and json decode failed.
if dash.Data != nil {
isEmptyData := true
for k := range dash.Data.MustMap() {
if k != "id" && k != "uid" {
isEmptyData = false
break
}
}
if isEmptyData {
return Error(500, "Error while loading dashboard, dashboard data is invalid", nil)
}
}
guardian := guardian.New(dash.Id, c.OrgId, c.SignedInUser)
if canView, err := guardian.CanView(); err != nil || !canView {
return dashboardGuardianResponse(err)
+3 -1
View File
@@ -1009,7 +1009,9 @@ func TestDashboardAPIEndpoint(t *testing.T) {
return nil
})
bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
query.Result = &models.Dashboard{Id: 1, Data: &simplejson.Json{}}
dataValue, err := simplejson.NewJson([]byte(`{"id": 1, "editable": true, "style": "dark"}`))
require.NoError(t, err)
query.Result = &models.Dashboard{Id: 1, Data: dataValue}
return nil
})