diff --git a/pkg/api/dashboard.go b/pkg/api/dashboard.go index 278264f22d7..9ea5089ee7d 100644 --- a/pkg/api/dashboard.go +++ b/pkg/api/dashboard.go @@ -87,6 +87,10 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) { c.JSON(412, util.DynMap{"status": "version-mismatch", "message": err.Error()}) return } + if err == m.ErrDashboardNotFound { + c.JSON(404, util.DynMap{"status": "not-found", "message": err.Error()}) + return + } c.JsonApiErr(500, "Failed to save dashboard", err) return } diff --git a/pkg/models/dashboards.go b/pkg/models/dashboards.go index 159d4e1a3c6..234ae74f04a 100644 --- a/pkg/models/dashboards.go +++ b/pkg/models/dashboards.go @@ -10,7 +10,7 @@ import ( // Typed errors var ( - ErrDashboardNotFound = errors.New("Account not found") + ErrDashboardNotFound = errors.New("Dashboard not found") ErrDashboardWithSameNameExists = errors.New("A dashboard with the same name already exists") ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else") ) diff --git a/pkg/services/sqlstore/dashboard.go b/pkg/services/sqlstore/dashboard.go index 7dbebd94e4c..f4be6d55edf 100644 --- a/pkg/services/sqlstore/dashboard.go +++ b/pkg/services/sqlstore/dashboard.go @@ -48,13 +48,19 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error { } } + affectedRows := int64(0) + if dash.Id == 0 { metrics.M_Models_Dashboard_Insert.Inc(1) - _, err = sess.Insert(dash) + affectedRows, err = sess.Insert(dash) } else { dash.Version += 1 dash.Data["version"] = dash.Version - _, err = sess.Id(dash.Id).Update(dash) + affectedRows, err = sess.Id(dash.Id).Update(dash) + } + + if affectedRows == 0 { + return m.ErrDashboardNotFound } // delete existing tabs diff --git a/pkg/services/sqlstore/dashboard_test.go b/pkg/services/sqlstore/dashboard_test.go index c7a8053d528..4c6614ed774 100644 --- a/pkg/services/sqlstore/dashboard_test.go +++ b/pkg/services/sqlstore/dashboard_test.go @@ -51,6 +51,21 @@ func TestDashboardDataAccess(t *testing.T) { So(query.Result.Slug, ShouldEqual, "test-dash-23") }) + Convey("Should return error if no dashboard is updated", func() { + cmd := m.SaveDashboardCommand{ + OrgId: 1, + Overwrite: true, + Dashboard: map[string]interface{}{ + "id": float64(123412321), + "title": "Expect error", + "tags": []interface{}{}, + }, + } + + err := SaveDashboard(&cmd) + So(err, ShouldNotBeNil) + }) + Convey("Should be able to search for dashboard", func() { query := m.SearchDashboardsQuery{ Title: "test",