Skip to content

Commit

Permalink
Update: Handle error message, handle error blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoud-Emad committed Apr 21, 2024
1 parent bd73ce3 commit d097eea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
11 changes: 2 additions & 9 deletions packages/playground/src/utils/ssh.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { InsufficientBalanceError } from "@threefold/types";
import crypto from "crypto";

import { useProfileManager } from "@/stores/profile_manager";
import type { SSHKeyData } from "@/types";

import { createCustomToast, ToastType } from "./custom_toast";
import { getGrid, loadBalance, storeSSH } from "./grid";
import { getGrid, storeSSH } from "./grid";
import { downloadAsJson } from "./helpers";

/**
Expand Down Expand Up @@ -83,18 +82,12 @@ class SSHKeysManagement {
async update(keys: SSHKeyData[]): Promise<void> {
const profileManager = useProfileManager();
const grid = await getGrid(profileManager.profile!);

if (!grid) {
createCustomToast(`Error occurred because the grid has not initialized yet.`, ToastType.danger);
return;
}

const balance = await loadBalance(grid!);
if (balance.free === balance.locked || balance.free < this.updateCost) {
throw new Error(
"Your wallet balance is insufficient to save your SSH key. To avoid losing your SSH key, please recharge your wallet.",
);
}

const copiedKeys = keys.map(
({ fingerPrint, activating, deleting, ...keyWithoutSensitiveProps }) => keyWithoutSensitiveProps,
);
Expand Down
43 changes: 39 additions & 4 deletions packages/playground/src/views/sshkey_view.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
</template>

<script lang="ts" setup>
import { InsufficientBalanceError } from "@threefold/types";
import { defineComponent, onMounted, ref } from "vue";
import { generateKeyPair } from "web-ssh-keygen";
Expand Down Expand Up @@ -192,7 +193,15 @@ const updateActivation = async (key: SSHKeyData) => {
try {
await sshKeysManagement.update(allKeys.value);
} catch (e: any) {
createCustomToast(e.message, ToastType.danger);
if (e instanceof InsufficientBalanceError) {
createCustomToast(
"Your wallet balance is insufficient to save your SSH key. To avoid losing your SSH key, please recharge your wallet.",
ToastType.danger,
);
} else {
createCustomToast(e.message, ToastType.danger);
}
activating.value = key.activating = false;
return;
}
Expand All @@ -215,7 +224,15 @@ const deleteKey = async (selectedKeys: SSHKeyData[]) => {
try {
await sshKeysManagement.update(keysToNotDelete);
} catch (e: any) {
createCustomToast(e.message, ToastType.danger);
if (e instanceof InsufficientBalanceError) {
createCustomToast(
"Your wallet balance is insufficient to save your SSH key. To avoid losing your SSH key, please recharge your wallet.",
ToastType.danger,
);
} else {
createCustomToast(e.message, ToastType.danger);
}
deleting.value = false;
return;
}
Expand All @@ -236,13 +253,23 @@ const addKey = async (key: SSHKeyData) => {
}
const copiedAllkeys = [...allKeys.value, key];
try {
await sshKeysManagement.update(copiedAllkeys);
} catch (e: any) {
createCustomToast(e.message, ToastType.danger);
if (e instanceof InsufficientBalanceError) {
createCustomToast(
"Your wallet balance is insufficient to save your SSH key. To avoid losing your SSH key, please recharge your wallet.",
ToastType.danger,
);
} else {
createCustomToast(e.message, ToastType.danger);
}
savingKey.value = false;
return;
}
loading.value = true;
allKeys.value = copiedAllkeys;
savingKey.value = false;
Expand All @@ -267,7 +294,15 @@ const generateSSHKeys = async (key: SSHKeyData) => {
try {
await sshKeysManagement.update(copiedAllkeys);
} catch (e: any) {
createCustomToast(e.message, ToastType.danger);
if (e instanceof InsufficientBalanceError) {
createCustomToast(
"Your wallet balance is insufficient to save your SSH key. To avoid losing your SSH key, please recharge your wallet.",
ToastType.danger,
);
} else {
createCustomToast(e.message, ToastType.danger);
}
generatingSSH.value = false;
loading.value = false;
return;
Expand Down

0 comments on commit d097eea

Please sign in to comment.