Skip to content

Commit

Permalink
Handle other errors when creating tenant
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek3255 committed Feb 2, 2024
1 parent f9e2b88 commit 4166fdf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build/static/js/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/static/js/bundle.js.map

Large diffs are not rendered by default.

22 changes: 17 additions & 5 deletions src/api/tenants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

import { getApiUrl, useFetchData } from "../../utils";

export const useTenantsService = () => {
const fetchData = useFetchData();
export const useTenantCreateService = () => {
const fetchData = useFetchData(true);

const createOrUpdateTenant = async (
tenantId: string
Expand All @@ -28,6 +28,10 @@ export const useTenantsService = () => {
| {
status: "MULTITENANCY_NOT_ENABLED_ERROR";
}
| {
status: "INVALID_TENANT_ID_ERROR";
message: string;
}
| undefined
> => {
const response = await fetchData({
Expand All @@ -45,10 +49,18 @@ export const useTenantsService = () => {
return body;
}

if (response.status === 500) {
const text = await response.text();
if (text.includes(`Cannot use '${tenantId}' as a tenantId`)) {
return {
status: "INVALID_TENANT_ID_ERROR",
message: `Cannot use '${tenantId}' as a Tenant Id.`,
};
}
}

return undefined;
};

return {
createOrUpdateTenant,
};
return createOrUpdateTenant;
};
10 changes: 8 additions & 2 deletions src/ui/components/tenants/creatNewTenant/CreateNewTenant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Button from "../../button";
import { Dialog, DialogContent, DialogFooter } from "../../dialog";
import InputField from "../../inputField/InputField";

import { useTenantsService } from "../../../../api/tenants";
import { useTenantCreateService } from "../../../../api/tenants";
import "./createNewTenant.scss";

export const CreateNewTenantDialog = ({
Expand All @@ -29,7 +29,7 @@ export const CreateNewTenantDialog = ({
onCloseDialog: () => void;
currentTenantIds: Array<string>;
}) => {
const { createOrUpdateTenant } = useTenantsService();
const createOrUpdateTenant = useTenantCreateService();

const [tenantCreationError, setTenantCreationError] = useState<string | undefined>(undefined);
const [isCreatingTenant, setIsCreatingTenant] = useState(false);
Expand All @@ -46,6 +46,10 @@ export const CreateNewTenantDialog = ({
setTenantCreationError("Tenant Id can only contain letters, numbers and hyphens");
return;
}
if (tenantId.startsWith("appid-")) {
setTenantCreationError("Tenant Id cannot start with 'appid-'");
return;
}
if (currentTenantIds.includes(tenantId)) {
setTenantCreationError("Tenant Id already exists");
return;
Expand All @@ -60,6 +64,8 @@ export const CreateNewTenantDialog = ({
setTenantCreationError(
"Multitenancy is not enabled for your SuperTokens instance. Please add license key to enable it."
);
} else if (resp?.status === "INVALID_TENANT_ID_ERROR") {
setTenantCreationError(resp.message);
} else {
throw new Error("Failed to create tenant");
}
Expand Down

0 comments on commit 4166fdf

Please sign in to comment.