Skip to content

Commit 43f99e8

Browse files
committed
Improve error handling: catch errors and loging to FS
1 parent 42d0411 commit 43f99e8

File tree

2 files changed

+61
-27
lines changed

2 files changed

+61
-27
lines changed

Native/static/application.js

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1+
const fs = await navigator.storage.getDirectory();
2+
13
class Logger {
24
#output;
5+
#file;
6+
#stream;
37

4-
constructor(outputId) {
8+
constructor(outputId, file) {
59
this.#output = document.getElementById(outputId);
10+
this.#file = file;
11+
return file.createWritable().then((stream) => {
12+
this.#stream = stream;
13+
return this;
14+
});
615
}
716

817
log(...args) {
918
const lines = args.map(Logger.#serialize);
10-
this.#output.textContent += lines.join(' ') + '\n';
19+
const data = lines.join(' ') + '\n';
20+
this.#output.textContent += data;
1121
this.#output.scrollTop = this.#output.scrollHeight;
22+
const { size } = this.#file;
23+
this.#stream.write({ type: 'write', position: size, data });
1224
}
1325

1426
static #serialize(x) {
1527
return typeof x === 'object' ? JSON.stringify(x, null, 2) : x;
1628
}
1729
}
1830

19-
const logger = new Logger('output');
20-
21-
const fs = await navigator.storage.getDirectory();
31+
const logDir = await fs.getDirectoryHandle('logs', { create: true });
32+
const logFile = await logDir.getFileHandle('app.log', { create: true });
33+
const logger = await new Logger('output', logFile);
2234

2335
const data = [
2436
{ name: 'Marcus', age: 20 },
@@ -27,42 +39,64 @@ const data = [
2739
];
2840

2941
document.getElementById('create').onclick = async () => {
30-
const fileHandle = await fs.getFileHandle('data.json', { create: true });
31-
const writable = await fileHandle.createWritable();
32-
await writable.write(JSON.stringify(data));
33-
await writable.close();
3442
logger.log('File saved: data.json');
43+
try {
44+
const fileHandle = await fs.getFileHandle('data.json', { create: true });
45+
const writable = await fileHandle.createWritable();
46+
await writable.write(JSON.stringify(data));
47+
await writable.close();
48+
} catch (error) {
49+
logger.log(error.message);
50+
}
3551
};
3652

3753
document.getElementById('read').onclick = async () => {
38-
const fileHandle = await fs.getFileHandle('data.json');
39-
const file = await fileHandle.getFile();
40-
const content = await file.text();
41-
logger.log(content);
42-
const data = JSON.parse(content);
4354
logger.log('Read file: data.json');
44-
logger.log(data);
55+
try {
56+
const fileHandle = await fs.getFileHandle('data.json');
57+
const file = await fileHandle.getFile();
58+
const content = await file.text();
59+
logger.log(content);
60+
const data = JSON.parse(content);
61+
logger.log(data);
62+
} catch (error) {
63+
logger.log(error.message);
64+
}
4565
};
4666

4767
document.getElementById('append').onclick = async () => {
48-
const fileHandle = await fs.getFileHandle('data.json', { create: true });
49-
const writable = await fileHandle.createWritable({ keepExistingData: true });
50-
const file = await fileHandle.getFile();
51-
const marcus = { name: 'Marcus', age: 20 };
52-
const buffer = ',' + JSON.stringify(marcus) + ']';
53-
logger.log(buffer);
54-
await writable.write({ type: 'write', position: file.size - 1, buffer });
55-
await writable.close();
5668
logger.log('Append to: data.json');
69+
try {
70+
const fileHandle = await fs.getFileHandle('data.json', { create: true });
71+
const writable = await fileHandle.createWritable({
72+
keepExistingData: true,
73+
});
74+
const file = await fileHandle.getFile();
75+
const marcus = { name: 'Marcus', age: 20 };
76+
const data = `,${JSON.stringify(marcus)}]`;
77+
await writable.write({ type: 'write', position: file.size - 1, data });
78+
await writable.close();
79+
} catch (error) {
80+
logger.log(error.message);
81+
}
5782
};
5883

5984
document.getElementById('list').onclick = async () => {
60-
for await (const [name, handle] of fs.entries()) {
61-
logger.log(`${name} ${handle.kind}`);
85+
logger.log('List files:');
86+
try {
87+
for await (const [name, handle] of fs.entries()) {
88+
logger.log(`${name} ${handle.kind}`);
89+
}
90+
} catch (error) {
91+
logger.log(error.message);
6292
}
6393
};
6494

6595
document.getElementById('delete').onclick = async () => {
66-
await fs.removeEntry('data.json');
6796
logger.log('Deleted: data.json');
97+
try {
98+
await fs.removeEntry('data.json');
99+
} catch (error) {
100+
logger.log(error.message);
101+
}
68102
};

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Origin private file system
1+
## Origin Private File System
22

33
> Usage Examples and Wrappers
44

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy