Commit 388d0087 by ying-jeanne Committed by GitHub

[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>
parent db67e70b
......@@ -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)
......
......@@ -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
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment