Skip to content

Commit

Permalink
Display failure reason when a file could not be read
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimbatt committed Sep 20, 2022
1 parent a94499c commit 56c22ec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
16 changes: 10 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,16 @@ function Finished() {
UpdateBlob();
var button = document.getElementById("create_torrent_button");
button.textContent = "Download torrent file";
if (window.navigator.msSaveOrOpenBlob) {
var msSaveOrOpenBlob = window.navigator.msSaveOrOpenBlob;
if (msSaveOrOpenBlob) {
button.onclick = function () {
if (torrentChanged) {
if (!SetTorrentData())
return;
UpdateBlob();
}
if (torrentObject && torrentObject.info)
window.navigator.msSaveOrOpenBlob(blob, torrentObject.info.name + ".torrent");
msSaveOrOpenBlob(blob, torrentObject.info.name + ".torrent");
};
}
else {
Expand Down Expand Up @@ -333,9 +334,12 @@ function Finished() {
creationInProgress = false;
DisableElements(false);
}
function Failed(fileName) {
function Failed(fileName, err) {
var errorTextDiv = document.getElementById("error_text");
errorTextDiv.textContent = fileName ? ("Failed to read file: " + fileName) : "Error reading file";
var errorText = fileName ? ("Failed to read file: " + fileName) : "Error reading file";
if (err)
errorText += "\nReason: " + err.message + " (" + err.name + ")";
errorTextDiv.textContent = errorText;
errorTextDiv.style.display = "block";
var progressBar = document.getElementById("progressbar");
progressBar.style.display = "none";
Expand Down Expand Up @@ -450,7 +454,7 @@ function CreateFromFile(obj) {
});
};
fr.onerror = function () {
Failed(singleFile && singleFile.name);
Failed(singleFile && singleFile.name, fr.error);
};
reader();
}
Expand Down Expand Up @@ -587,7 +591,7 @@ function CreateFromFolder(obj) {
});
};
fr.onerror = function () {
Failed(currentFile.name);
Failed(currentFile.name, fr.error);
};
progressBarText.innerHTML = "Reading file: " + currentFile.name;
progressBarProcessingText.textContent = "Processing: 0%";
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@
oninput="TorrentParamsChanged('source')">
<br />

<div id="error_text" style="display: none; font-size: 25px; color: #ff3d58; margin-left: 20px; margin-bottom: 20px;"></div>
<div id="error_text" style="display: none; font-size: 25px; color: #ff3d58; margin-left: 20px; margin-bottom: 20px; white-space: pre-line;"></div>

<div id="progressbar_container" class="progress-bar-hidden" style="margin-left: 20px; border: 2px solid #bcbcbc; border-radius: 5px; width: calc(100% - 50px); position: relative;">
<div id="progressbar" style="background: #23b235; width: 0%; height: 100%; border-radius: 3px;"></div>
Expand Down
18 changes: 12 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ function Finished()

const button = <HTMLButtonElement>document.getElementById("create_torrent_button");
button.textContent = "Download torrent file";
if (window.navigator.msSaveOrOpenBlob)
const msSaveOrOpenBlob: ((blob: any, defaultName?: string, retVal?: boolean) => boolean) | undefined = (window.navigator as any).msSaveOrOpenBlob;
if (msSaveOrOpenBlob)
{
button.onclick = function()
{
Expand All @@ -381,7 +382,7 @@ function Finished()
}

if (torrentObject && torrentObject.info)
window.navigator.msSaveOrOpenBlob(blob, torrentObject.info.name + ".torrent");
msSaveOrOpenBlob(blob, torrentObject.info.name + ".torrent");
};
}
else
Expand Down Expand Up @@ -417,10 +418,15 @@ function Finished()
DisableElements(false);
}

function Failed(fileName: string | null)
function Failed(fileName: string | null, err: DOMException | null)
{
const errorTextDiv = document.getElementById("error_text")!;
errorTextDiv.textContent = fileName ? ("Failed to read file: " + fileName) : "Error reading file";
let errorText = fileName ? ("Failed to read file: " + fileName) : "Error reading file";

if (err)
errorText += "\nReason: " + err.message + " (" + err.name + ")";

errorTextDiv.textContent = errorText;
errorTextDiv.style.display = "block";

const progressBar = document.getElementById("progressbar")!;
Expand Down Expand Up @@ -564,7 +570,7 @@ function CreateFromFile(obj: TorrentObject)

fr.onerror = function()
{
Failed(singleFile && singleFile.name);
Failed(singleFile && singleFile.name, fr.error);
};

reader();
Expand Down Expand Up @@ -743,7 +749,7 @@ function CreateFromFolder(obj: TorrentObject)

fr.onerror = function()
{
Failed(currentFile.name);
Failed(currentFile.name, fr.error);
};

progressBarText.innerHTML = "Reading file: " + currentFile.name;
Expand Down

0 comments on commit 56c22ec

Please sign in to comment.