diff --git a/pkg/api/org.go b/pkg/api/org.go index 61af62311c6..26b2cf92fd8 100644 --- a/pkg/api/org.go +++ b/pkg/api/org.go @@ -152,6 +152,9 @@ func updateOrgAddressHelper(form dtos.UpdateOrgAddressForm, orgId int64) Respons // GET /api/orgs/:orgId func DeleteOrgById(c *middleware.Context) Response { if err := bus.Dispatch(&m.DeleteOrgCommand{Id: c.ParamsInt64(":orgId")}); err != nil { + if err == m.ErrOrgNotFound { + return ApiError(404, "Failed to delete organization. ID not found", nil) + } return ApiError(500, "Failed to update organization", err) } return ApiSuccess("Organization deleted") diff --git a/pkg/services/sqlstore/org.go b/pkg/services/sqlstore/org.go index 6ac171127c9..0573d675102 100644 --- a/pkg/services/sqlstore/org.go +++ b/pkg/services/sqlstore/org.go @@ -176,6 +176,12 @@ func UpdateOrgAddress(cmd *m.UpdateOrgAddressCommand) error { func DeleteOrg(cmd *m.DeleteOrgCommand) error { return inTransaction2(func(sess *session) error { + //Check if organization exists + if res, err := sess.Query("SELECT 1 from org WHERE id=?", cmd.Id); err != nil { + return err + } else if len(res) != 1 { + return m.ErrOrgNotFound + } deletes := []string{ "DELETE FROM star WHERE EXISTS (SELECT 1 FROM dashboard WHERE org_id = ? AND star.dashboard_id = dashboard.id)",