Skip to content

Commit 9645382

Browse files
committed
feat: construct and return user fiendly error when org delete fails
1 parent 5c0fb09 commit 9645382

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

enterprise/coderd/organizations.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"fmt"
66
"net/http"
7+
"strings"
78

89
"github.com/google/uuid"
910
"golang.org/x/xerrors"
@@ -161,10 +162,41 @@ func (api *API) deleteOrganization(rw http.ResponseWriter, r *http.Request) {
161162
return nil
162163
}, nil)
163164
if err != nil {
165+
orgResourcesRow, queryErr := api.Database.GetOrganizationResourceCountByID(ctx, organization.ID)
166+
if queryErr != nil {
167+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
168+
Message: "Internal error deleting organization.",
169+
Detail: fmt.Sprintf("delete organization: %s", err.Error()),
170+
})
171+
172+
return
173+
}
174+
175+
detailParts := make([]string, 0)
176+
177+
addDetailPart := func(resource string, count int64) {
178+
if count == 1 {
179+
detailParts = append(detailParts, fmt.Sprintf("1 %s", resource))
180+
} else if count > 1 {
181+
detailParts = append(detailParts, fmt.Sprintf("%d %ss", count, resource))
182+
}
183+
}
184+
185+
addDetailPart("workspace", orgResourcesRow.WorkspaceCount)
186+
addDetailPart("template", orgResourcesRow.TemplateCount)
187+
188+
// There will always be one member and group so instead we need to check that
189+
// the count is greater than one.
190+
addDetailPart("member", orgResourcesRow.MemberCount-1)
191+
addDetailPart("group", orgResourcesRow.GroupCount-1)
192+
193+
addDetailPart("provisioner key", orgResourcesRow.ProvisionerKeyCount)
194+
164195
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
165-
Message: "Internal error deleting organization.",
166-
Detail: fmt.Sprintf("delete organization: %s", err.Error()),
196+
Message: "Error deleting organization.",
197+
Detail: fmt.Sprintf("This organization has %s that must be deleted first.", strings.Join(detailParts, ", ")),
167198
})
199+
168200
return
169201
}
170202

0 commit comments

Comments
 (0)