Skip to content

Commit

Permalink
add try catch to loading data files
Browse files Browse the repository at this point in the history
  • Loading branch information
terryzfeng committed Oct 23, 2024
1 parent d9742cd commit 9ee5eaf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/components/examples/moreExamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ export default class MoreExamples {
);
MoreExamples.previewExample.data.forEach(async (url: string) => {
const file = await fetchDataFile(url);
ProjectSystem.addNewFile(file.name, file.data);
if (file !== null) {
ProjectSystem.addNewFile(file.name, file.data);
}
});
MoreExamples.hideModal();
}
Expand Down
8 changes: 5 additions & 3 deletions src/components/fileExplorer/projectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ export async function loadChuckFileFromURL(url: string) {
* @param url url to data file
*/
export async function loadDataFileFromURL(url: string) {
const dataFile: FileData = await fetchDataFile(url);
ProjectSystem.addNewFile(dataFile.name, dataFile.data as Uint8Array);
Console.print(`loaded file: ${dataFile.name}`);
const dataFile: FileData | null = await fetchDataFile(url);
if (dataFile !== null) {
ProjectSystem.addNewFile(dataFile.name, dataFile.data as Uint8Array);
Console.print(`loaded file: ${dataFile.name}`);
}
}
33 changes: 20 additions & 13 deletions src/utils/fileLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,36 @@ export interface File {
* @param url
*/
export async function fetchTextFile(url: string): Promise<File> {
const filename = url.split("/").pop();
const filename = url.split("/").pop()!;
const response = await fetch(url);
const text = await response.text();
return { name: filename!, data: text };
return { name: filename, data: text };
}

/**
* Load a binary file, good for loading wav files
*/
export async function fetchDataFile(url: string): Promise<File> {
export async function fetchDataFile(url: string): Promise<File | null> {
// const fileName = url.split('/').pop();
// let response = await fetch(url)
// let blob = await response.blob();
// let data = new Uint8Array(await blob.arrayBuffer());
// return { name: fileName!, data: data};
let file: File;
await fetch(url)
.then((response) => response.arrayBuffer())
.then((buffer) => {
file = {
name: url.split("/").pop()!, // file name
data: new Uint8Array(buffer), // file data
};
});
return file!;

let file: File | null = null;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to load file: ${url}`);
}
const buffer = await response.arrayBuffer();
file = {
name: url.split("/").pop()!, // file name
data: new Uint8Array(buffer), // file data
};
} catch (error) {
console.error(error);
}

return file;
}

0 comments on commit 9ee5eaf

Please sign in to comment.