1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-17 18:09:17 +00:00

error handling in remove password component

This commit is contained in:
Maciej Zieniuk
2025-03-13 17:06:23 +00:00
parent 0569b2243b
commit 285a61a997
2 changed files with 58 additions and 20 deletions

View File

@@ -129,7 +129,25 @@ describe("RemovePasswordComponent", () => {
expect(mockRouter.navigate).toHaveBeenCalledWith([""]);
});
it("should handle response errors and show error toast", async () => {
it("should handle errors and show error toast", async () => {
const errorMessage = "Can't migrate user error";
mockKeyConnectorService.migrateUser.mockRejectedValue(new Error(errorMessage));
mockI18nService.t.mockReturnValue("error occurred");
await component.convert();
expect(component.continuing).toBe(false);
expect(mockKeyConnectorService.migrateUser).toHaveBeenCalledWith(userId);
expect(mockToastService.showToast).toHaveBeenCalledWith({
variant: "error",
title: "error occurred",
message: errorMessage,
});
expect(mockKeyConnectorService.removeConvertAccountRequired).not.toHaveBeenCalled();
expect(mockRouter.navigate).not.toHaveBeenCalled();
});
it("should handle error response and show error toast", async () => {
const errorMessage = "Can't migrate user error";
mockKeyConnectorService.migrateUser.mockRejectedValue(
new ErrorResponse(
@@ -178,7 +196,26 @@ describe("RemovePasswordComponent", () => {
expect(mockRouter.navigate).toHaveBeenCalledWith([""]);
});
it("should handle response errors and show error toast", async () => {
it("should handle error response and show error toast", async () => {
const errorMessage = "Can't leave organization error";
mockDialogService.openSimpleDialog.mockResolvedValue(true);
mockOrganizationApiService.leave.mockRejectedValue(new Error(errorMessage));
mockI18nService.t.mockReturnValue("error occurred");
await component.leave();
expect(component.leaving).toBe(false);
expect(mockOrganizationApiService.leave).toHaveBeenCalledWith(organization.id);
expect(mockToastService.showToast).toHaveBeenCalledWith({
variant: "error",
title: "error occurred",
message: errorMessage,
});
expect(mockKeyConnectorService.removeConvertAccountRequired).not.toHaveBeenCalled();
expect(mockRouter.navigate).not.toHaveBeenCalled();
});
it("should handle error response and show error toast", async () => {
const errorMessage = "Can't leave organization error";
mockDialogService.openSimpleDialog.mockResolvedValue(true);
mockOrganizationApiService.leave.mockRejectedValue(

View File

@@ -61,7 +61,7 @@ export class RemovePasswordComponent implements OnInit {
return this.continuing || this.leaving;
}
convert = async () => {
async convert() {
this.continuing = true;
try {
@@ -77,17 +77,11 @@ export class RemovePasswordComponent implements OnInit {
} catch (e) {
this.continuing = false;
if (e instanceof ErrorResponse) {
this.toastService.showToast({
variant: "error",
title: this.i18nService.t("errorOccurred"),
message: e.message,
});
}
this.handleActionError(e);
}
};
}
leave = async () => {
async leave() {
const confirmed = await this.dialogService.openSimpleDialog({
title: this.organization.name,
content: { key: "leaveOrganizationConfirmation" },
@@ -112,13 +106,20 @@ export class RemovePasswordComponent implements OnInit {
} catch (e) {
this.leaving = false;
if (e instanceof ErrorResponse) {
this.toastService.showToast({
variant: "error",
title: this.i18nService.t("errorOccurred"),
message: e.message,
});
}
this.handleActionError(e);
}
};
}
handleActionError(e: unknown) {
let message = "";
if (e instanceof ErrorResponse || e instanceof Error) {
message = e.message;
}
this.toastService.showToast({
variant: "error",
title: this.i18nService.t("errorOccurred"),
message: message,
});
}
}