Skip to content

Commit 35b9beb

Browse files
committed
fix(site): show error when renaming template to existing template
1 parent 040e5cf commit 35b9beb

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

coderd/templates.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,17 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
841841
return nil
842842
}, nil)
843843
if err != nil {
844-
httpapi.InternalServerError(rw, err)
844+
if database.IsUniqueViolation(err) {
845+
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
846+
Message: fmt.Sprintf("Template %q already exists.", req.Name),
847+
Validations: []codersdk.ValidationError{{
848+
Field: "name",
849+
Detail: "This value is already in use and should be unique.",
850+
}},
851+
})
852+
} else {
853+
httpapi.InternalServerError(rw, err)
854+
}
845855
return
846856
}
847857

site/src/pages/TemplateSettingsPage/TemplateGeneralSettingsPage/TemplateSettingsPage.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { API } from "api/api";
2+
import { getErrorMessage } from "api/errors";
23
import { templateByNameKey } from "api/queries/templates";
34
import type { UpdateTemplateMeta } from "api/typesGenerated";
4-
import { displaySuccess } from "components/GlobalSnackbar/utils";
5+
import { displayError, displaySuccess } from "components/GlobalSnackbar/utils";
56
import { useDashboard } from "modules/dashboard/useDashboard";
67
import { linkToTemplate, useLinks } from "modules/navigation";
78
import type { FC } from "react";
@@ -51,6 +52,9 @@ export const TemplateSettingsPage: FC = () => {
5152
displaySuccess("Template updated successfully");
5253
navigate(getLink(linkToTemplate(data.organization_name, data.name)));
5354
},
55+
onError: (error) => {
56+
displayError(getErrorMessage(error, "Failed to update template"));
57+
},
5458
},
5559
);
5660

0 commit comments

Comments
 (0)