Skip to content

Commit

Permalink
fix: add custome error class
Browse files Browse the repository at this point in the history
  • Loading branch information
Chakravarthy7102 committed Sep 1, 2023
1 parent 979adb7 commit 0b1e818
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/api/user/password/reset/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const usePasswordResetService = (): IUsePasswordResetService => {
}),
},
});

return await response.json();
};

Expand Down
49 changes: 33 additions & 16 deletions src/ui/components/userDetail/userDetailForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Tenant } from "../../../api/tenants/list";
import { useUserService } from "../../../api/user";
import usePasswordResetService from "../../../api/user/password/reset";
import { getImageUrl } from "../../../utils";
import { ForbiddenError } from "../../../utils/customErrors";
import { getTenantsObjectsForIds } from "../../../utils/user";
import { PopupContentContext } from "../../contexts/PopupContentContext";
import { useTenantsListContext } from "../../contexts/TenantsListContext";
Expand Down Expand Up @@ -289,18 +290,26 @@ export const UserDetailChangeEmailForm: FC<UserDetailChangeEmailFormProps> = (
return;
}

const response = await updateUserInformation({
userId,
email,
recipeId,
tenantId,
});
let response;

try {
response = await updateUserInformation({
userId,
email,
recipeId,
tenantId,
});
} catch (error) {
if (ForbiddenError.isThisError(error)) {
void onCancel();
}
}

if (response.status === "INVALID_EMAIL_ERROR") {
if (response?.status === "INVALID_EMAIL_ERROR") {
setApiError(response.error);
} else if (response.status === "EMAIL_ALREADY_EXISTS_ERROR") {
} else if (response?.status === "EMAIL_ALREADY_EXISTS_ERROR") {
setApiError("A user with this email already exists");
} else if (response.status === "OK") {
} else if (response?.status === "OK") {
showToast(getUpdateEmailToast(true));
await onEmailChange(true);
}
Expand Down Expand Up @@ -383,15 +392,23 @@ export const UserDetailChangePasswordForm: FC<UserDetailChangePasswordFormProps>
return;
}

const response = await updatePassword(
userId,
password,
matchingTenantIds.length > 0 ? matchingTenantIds[0].tenantId : undefined
);
let response;

try {
response = await updatePassword(
userId,
password,
matchingTenantIds.length > 0 ? matchingTenantIds[0].tenantId : undefined
);
} catch (error) {
if (ForbiddenError.isThisError(error)) {
void onCancel();
}
}

if (response.status === "INVALID_PASSWORD_ERROR") {
if (response?.status === "INVALID_PASSWORD_ERROR") {
setApiError(response.error);
} else if (response.status === "OK") {
} else if (response?.status === "OK") {
showToast(getUpdatePasswordToast(true));
await onPasswordChange();
}
Expand Down
11 changes: 11 additions & 0 deletions src/utils/customErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class ForbiddenError extends Error {
statusCode = 403;
status = "FORBIDDEN_REQUEST";
constructor(message: string) {
super(message);
}

static isThisError(err: any): boolean {
return err.status === "FORBIDDEN_REQUEST";
}
}
11 changes: 7 additions & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import NetworkManager from "../services/network";
import { localStorageHandler } from "../services/storage";
import { HttpMethod } from "../types";
import { UserRecipeType } from "../ui/pages/usersList/types";
import { ForbiddenError } from "./customErrors";

export function getStaticBasePath(): string {
return (window as any).staticBasePath;
Expand Down Expand Up @@ -124,11 +125,13 @@ export const useFetchData = () => {
}

if (response.status === HTTPStatusCodes.FORBIDDEN) {
const message = (await response.clone().json())?.message;
let message = (await response.clone().json())?.message;
if (message === undefined) {
message = "You do not have access to this page";
}
window.dispatchEvent(getAccessDeniedEvent(message));

window.dispatchEvent(
getAccessDeniedEvent(message === undefined ? "You do not have access to this page" : message)
);
throw new ForbiddenError(message);
}

const logoutAndRedirect = shouldRedirectOnUnauthorised && HTTPStatusCodes.UNAUTHORIZED === response.status;
Expand Down

0 comments on commit 0b1e818

Please sign in to comment.