Skip to content

Commit

Permalink
Centralize and unify error handling #7
Browse files Browse the repository at this point in the history
  • Loading branch information
iShafayet committed Aug 29, 2022
1 parent 6b73189 commit 78a1505
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 213 deletions.
13 changes: 8 additions & 5 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import { createDebouncedMethod } from "./utility/misc-utils.js";
import { decryptText, encryptText } from "./utility/crypto-utils.js";
import { performUserLogout } from "./lib/session.js";
import { handleErrorIfAny } from "./lib/error-handling.js";
import { handleAnyError } from "./lib/error-handling.js";
let topAppBar: TopAppBarComponentDev;
Expand All @@ -64,10 +64,13 @@
const loadBucketList = createDebouncedMethod(async () => {
incrementActiveGlobalObtrusiveTaskCount();
let response = await callBucketListApi({});
if (await handleErrorIfAny(response)) return;
bucketList.set(response.bucketList);
decrementActiveGlobalObtrusiveTaskCount();
try {
let response = await callBucketListApi({});
bucketList.set(response.bucketList);
decrementActiveGlobalObtrusiveTaskCount();
} catch (ex) {
return await handleAnyError(ex);
}
}, 100);
const logoutClicked = async () => {
Expand Down
43 changes: 24 additions & 19 deletions src/component/page/CreateBucketPage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import { encryptText } from "../../utility/crypto-utils.js";
import { BUCKET_CRYPTO_SPEC } from "../../constant/crypto-specs.js";
import { bucketList } from "../../store/content.js";
import { handleErrorIfAny } from "../../lib/error-handling.js";
import { handleAnyError } from "../../lib/error-handling.js";
const NEW_BUCKET_ID = "new";
Expand All @@ -41,31 +41,36 @@
const finalForm = form(name, encryptionPassword);
const saveClicked = async () => {
let bucketPassword = $encryptionPassword.value;
try {
let bucketPassword = $encryptionPassword.value;
incrementActiveGlobalObtrusiveTaskCount();
incrementActiveGlobalObtrusiveTaskCount();
let cryptDataObject = await encryptText(BUCKET_CRYPTO_SPEC, bucketPassword);
let cryptDataObject = await encryptText(
BUCKET_CRYPTO_SPEC,
bucketPassword
);
let cryptData = JSON.stringify(cryptDataObject);
let cryptData = JSON.stringify(cryptDataObject);
let response = await callBucketCreateApi({
name: $name.value,
cryptSpec: BUCKET_CRYPTO_SPEC,
cryptData,
metaData: {
createFrom: "nkrypt.xyz app",
},
});
if (await handleErrorIfAny(response)) return;
let response = await callBucketCreateApi({
name: $name.value,
cryptSpec: BUCKET_CRYPTO_SPEC,
cryptData,
metaData: {
createFrom: "nkrypt.xyz app",
},
});
let response2 = await callBucketListApi({});
if (await handleErrorIfAny(response)) return;
let response2 = await callBucketListApi({});
bucketList.set(response2.bucketList);
bucketList.set(response2.bucketList);
replace("/dashboard");
decrementActiveGlobalObtrusiveTaskCount();
replace("/dashboard");
decrementActiveGlobalObtrusiveTaskCount();
} catch (ex) {
return await handleAnyError(ex);
}
};
</script>

Expand Down
52 changes: 29 additions & 23 deletions src/component/page/ExplorePage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
callDirectoryCreateApi,
callDirectoryGetApi,
} from "../../integration/content-apis.js";
import { handleErrorIfAny } from "../../lib/error-handling.js";
import { handleAnyError } from "../../lib/error-handling.js";
import {
getPasswordForBucket,
setPasswordForBucket,
Expand Down Expand Up @@ -76,14 +76,17 @@
};
const getDirectory = async (directoryId: string) => {
incrementActiveGlobalObtrusiveTaskCount();
let response = await callDirectoryGetApi({
bucketId: currentBucket._id,
directoryId: directoryId,
});
if (await handleErrorIfAny(response)) return;
decrementActiveGlobalObtrusiveTaskCount();
return response;
try {
incrementActiveGlobalObtrusiveTaskCount();
let response = await callDirectoryGetApi({
bucketId: currentBucket._id,
directoryId: directoryId,
});
decrementActiveGlobalObtrusiveTaskCount();
return response;
} catch (ex) {
return await handleAnyError(ex);
}
};
const loadRootDirectory = async (): Promise<void> => {
Expand Down Expand Up @@ -160,21 +163,24 @@
});
const createDirectoryClicked = async () => {
let name = await showPrompt("Create directory", "Enter directory name");
if (!name) return;
incrementActiveGlobalObtrusiveTaskCount();
let response = await callDirectoryCreateApi({
bucketId: currentBucket._id,
name,
parentDirectoryId: (currentDirectory as any)._id,
metaData: {},
encryptedMetaData: await encryptObject({}, currentBucketPassword),
});
if (await handleErrorIfAny(response)) return;
decrementActiveGlobalObtrusiveTaskCount();
try {
let name = await showPrompt("Create directory", "Enter directory name");
if (!name) return;
incrementActiveGlobalObtrusiveTaskCount();
let response = await callDirectoryCreateApi({
bucketId: currentBucket._id,
name,
parentDirectoryId: (currentDirectory as any)._id,
metaData: {},
encryptedMetaData: await encryptObject({}, currentBucketPassword),
});
decrementActiveGlobalObtrusiveTaskCount();
explorePath(currentPath);
explorePath(currentPath);
} catch (ex) {
return await handleAnyError(ex);
}
};
const childDirectoryClicked = async (childDirectory) => {
Expand Down
3 changes: 1 addition & 2 deletions src/component/page/ExplorePage/FileOperationModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@
import Button, { Label } from "@smui/button";
import CircularProgress from "@smui/circular-progress";
import { expressBytesPrettified } from "../../../utility/value-utils.js";
import { CodedError, handleErrorIfAny } from "../../../lib/error-handling.js";
import {
downloadAndDecryptFile,
encryptAndUploadFile,
} from "../../../lib/crypto-transit.js";
import LinearProgress from "@smui/linear-progress";
import { clientError } from "../../../constant/client-errors.js";
import { clientErrorDef } from "../../../constant/client-errors.js";
const FileOperationModalState = {
IDLE: "IDLE",
Expand Down
78 changes: 40 additions & 38 deletions src/component/page/ExplorePage/FileUploadModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import Button, { Label } from "@smui/button";
import CircularProgress from "@smui/circular-progress";
import { expressBytesPrettified } from "../../../utility/value-utils.js";
import { handleErrorIfAny } from "../../../lib/error-handling.js";
import { handleAnyError } from "../../../lib/error-handling.js";
import { encryptAndUploadFile } from "../../../lib/crypto-transit.js";
import LinearProgress from "@smui/linear-progress";
Expand Down Expand Up @@ -156,51 +156,53 @@
};
const startUploadClicked = async () => {
let fileId = null;
let { bucketId, _id: parentDirectoryId } = directory;
if (requiresOverwrite) {
let matchingFile = childFileList.find((file) => file.name === fileName);
fileId = matchingFile._id;
} else {
incrementActiveGlobalObtrusiveTaskCount();
let response = await callFileCreateApi({
try {
let fileId = null;
let { bucketId, _id: parentDirectoryId } = directory;
if (requiresOverwrite) {
let matchingFile = childFileList.find((file) => file.name === fileName);
fileId = matchingFile._id;
} else {
incrementActiveGlobalObtrusiveTaskCount();
let response = await callFileCreateApi({
bucketId,
name: fileName,
parentDirectoryId,
metaData: {},
encryptedMetaData: await encryptObject({}, bucketPassword),
});
if (response.hasError) {
setAnswer(null);
return;
}
decrementActiveGlobalObtrusiveTaskCount();
({ fileId } = response);
}
state = FileUploadModalState.FILE_UPLOAD;
uploadProgress = {
totalBytes: 0,
encryptedBytes: 0,
};
let response = await encryptAndUploadFile(
bucketId,
name: fileName,
parentDirectoryId,
metaData: {},
encryptedMetaData: await encryptObject({}, bucketPassword),
});
fileId,
uploadCandidate,
bucketPassword,
updateProgressFn,
selectedUploadMethod
);
if (response.hasError) {
setAnswer(null);
await handleErrorIfAny(response);
return;
}
decrementActiveGlobalObtrusiveTaskCount();
({ fileId } = response);
}
state = FileUploadModalState.FILE_UPLOAD;
uploadProgress = {
totalBytes: 0,
encryptedBytes: 0,
};
let response = await encryptAndUploadFile(
bucketId,
fileId,
uploadCandidate,
bucketPassword,
updateProgressFn,
selectedUploadMethod
);
if (response.hasError) {
setAnswer(null);
await handleErrorIfAny(response);
setAnswer(response);
return;
} catch (ex) {
return await handleAnyError(ex);
}
setAnswer(response);
return;
};
let shouldShowDialog = false;
Expand Down
27 changes: 15 additions & 12 deletions src/component/page/LoginPage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,27 @@
decrementActiveGlobalObtrusiveTaskCount,
incrementActiveGlobalObtrusiveTaskCount,
} from "../../store/ui.js";
import { handleErrorIfAny } from "../../lib/error-handling.js";
import { handleAnyError } from "../../lib/error-handling.js";
const loginClicked = async () => {
let payload = extract($finalForm.summary, ["userName", "password"]);
incrementActiveGlobalObtrusiveTaskCount();
let response = await callUserLoginApi($server.value, payload);
if (await handleErrorIfAny(response)) return;
decrementActiveGlobalObtrusiveTaskCount();
try {
let payload = extract($finalForm.summary, ["userName", "password"]);
incrementActiveGlobalObtrusiveTaskCount();
let response = await callUserLoginApi($server.value, payload);
decrementActiveGlobalObtrusiveTaskCount();
let { apiKey } = response;
let { userName, displayName, _id: userId } = response.user;
let { apiKey } = response;
let { userName, displayName, _id: userId } = response.user;
storedUser.set({ userName, displayName, userId });
storedSession.set({ apiKey, serverUrl: $server.value });
storedUser.set({ userName, displayName, userId });
storedSession.set({ apiKey, serverUrl: $server.value });
suggestedServerUrl.set($server.value);
suggestedServerUrl.set($server.value);
replace("/dashboard");
replace("/dashboard");
} catch (ex) {
return await handleAnyError(ex);
}
};
const server = standardField("server", $suggestedServerUrl, [minlength(4)]);
Expand Down
34 changes: 25 additions & 9 deletions src/constant/client-errors.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
export const clientError = {
DECRYPTION_FAILED: {
code: "DECRYPTION_FAILED",
export const clientErrorDef = {
NKCE_DECRYPTION_FAILED: {
code: "NKCE_DECRYPTION_FAILED",
message:
"Failed to decrypt the file. Most likely the file has been corrupted during transmission to or storage on the server.",
shorthand: "Decryption failed",
},
ENCRYPTED_DOWNLOAD_FAILED: {
code: "ENCRYPTED_DOWNLOAD_FAILED",
message: "Failed to download encrypted file from the server",
NKCE_ENCRYPTED_DOWNLOAD_FAILED: {
code: "NKCE_ENCRYPTED_DOWNLOAD_FAILED",
message: "Failed to download encrypted file from the server.",
shorthand: "Download failed",
},
FAILED_TO_SAVE_FILE: {
code: "FAILED_TO_SAVE_FILE",
message: "Failed to save file after decryption",
NKCE_FAILED_TO_SAVE_FILE: {
code: "NKCE_FAILED_TO_SAVE_FILE",
message: "Failed to save file after decryption.",
shorthand: "Filesystem failure",
},
NKCE_BLOB_ID_MISSING: {
code: "NKCE_BLOB_ID_MISSING",
message: "BlobId is missing in server response.",
shorthand: "Filesystem failure",
},
NKRE_MALFORMATTED_RESPONSE: {
code: "NKRE_MALFORMATTED_RESPONSE",
message: "Server returned a malformatted response.",
shorthand: "Malformatted response",
},
NKRE_CONNECTIVITY_ISSUE: {
code: "NKRE_CONNECTIVITY_ISSUE",
message:
"Failed to establish a connection with the server. Please make sure you have a working internet connection. If you believe, everything is in working order on your end, please contact server administrator.",
shorthand: "Connection failed",
},
};
Loading

0 comments on commit 78a1505

Please sign in to comment.