Skip to content

Commit 433fed9

Browse files
bill-h4rperMartinFillon
authored andcommitted
feat(web): create html error handling
1 parent 8294deb commit 433fed9

File tree

6 files changed

+60
-8
lines changed

6 files changed

+60
-8
lines changed

apps/web/src/cache/cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class GameCache {
3838
const res = [];
3939
setLoadingTotalFiles(files.length);
4040
for (const [i, file] of files.entries()) {
41-
setLoadingStatus(file.path, i);
41+
setLoadingStatus(`Download: ${file.path.replace(/^\/+/, "")}`, i);
4242
res.push(await this._updateCacheFile(file));
4343
}
4444
return res;

apps/web/src/ids.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
export const IDS = {
22
canvas: "nanoforge-canvas",
33
loader: "nanoforge-loader",
4-
loaderStatus: "nanoforge-loader-status",
4+
5+
loadingStatus: "loading-status",
6+
loadingStep: "loading-step",
57
loadingBar: "loading-bar-fill",
8+
9+
loaderError: "loader-error",
10+
loaderErrorMessage: "loader-error-message",
611
};

apps/web/src/index.html

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
<script type="module" src="index.ts"></script>
1010
<div id="nanoforge-loader">
1111
<img id="loader-logo" src="assets/logo.png" alt="logo" />
12-
<div id="loading-progress">
13-
<div id="nanoforge-loader-status"></div>
14-
<div id="loading-bar">
15-
<div id="loading-bar-fill"></div>
12+
<div>
13+
<div id="loader-error" hidden="">
14+
<div id="loader-error-message"></div>
15+
<a id="back-home" href="/"> Back home </a>
16+
</div>
17+
<div id="loading-status">
18+
<div id="loading-step"></div>
19+
<div id="loading-bar">
20+
<div id="loading-bar-fill"></div>
21+
</div>
1622
</div>
1723
</div>
1824
</div>

apps/web/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ import { runGame } from "./game";
33
import { loadGameFiles } from "./loader";
44
import { getManifest } from "./manifest";
55
import { Logger } from "./utils/logger.utils";
6+
import { setError, setLoadingStatus } from "./window";
67

78
const logger = new Logger("Loader");
89

910
const runLoad = async () => {
1011
logger.info("Starting loading game");
12+
1113
const manifest = await getManifest();
1214
const cache = new GameCache();
1315
const extendedManifest = await cache.updateCache(manifest, true);
1416
const [files, mainModule] = await loadGameFiles(extendedManifest);
17+
setLoadingStatus("Starting game");
1518
runGame(mainModule, { files });
1619
};
1720

@@ -20,5 +23,6 @@ runLoad()
2023
logger.info("Game loaded !");
2124
})
2225
.catch((e) => {
26+
setError(e);
2327
logger.error(`Failed to load game : ${e}`);
2428
});

apps/web/src/style.css

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
left: 0;
55
top: 0;
66
background-color: oklch(0.21 0.034 264.665);
7+
font-weight: bold;
78
color: white;
89
position: absolute;
910
display: flex;
@@ -18,14 +19,18 @@
1819
width: 125px;
1920
}
2021

21-
#loading-progress {
22+
#loading-status {
2223
width: 100%;
2324
display: flex;
2425
flex-direction: column;
2526
align-items: center;
2627
gap: 10px;
2728
}
2829

30+
#loading-status[hidden] {
31+
display: none !important;
32+
}
33+
2934
#loading-bar {
3035
height: 12px;
3136
width: 500px;
@@ -42,6 +47,28 @@
4247
transition: width 0.3s ease-in-out;
4348
}
4449

50+
#loader-error {
51+
width: 100%;
52+
display: flex;
53+
flex-direction: column;
54+
align-items: center;
55+
gap: 10px;
56+
}
57+
58+
#loader-error[hidden] {
59+
display: none !important;
60+
}
61+
62+
#back-home {
63+
background-color: rgba(151, 63, 255, 0.8);
64+
text-decoration: none;
65+
border-radius: 5px;
66+
font-weight: bold;
67+
font-size: large;
68+
color: white;
69+
padding: 10px;
70+
}
71+
4572
.fade-out {
4673
opacity: 0;
4774
transition: opacity 1s ease-out;

apps/web/src/window.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const changeWindowToLoader = async () => {
2222
};
2323

2424
export const setLoadingStatus = (filename: string, index?: number | null) => {
25-
const loaderFilename = document.getElementById(IDS.loaderStatus);
25+
const loaderFilename = document.getElementById(IDS.loadingStep);
2626
const loadingBarFill = document.getElementById(IDS.loadingBar);
2727

2828
if (loaderFilename) loaderFilename.innerText = filename;
@@ -36,3 +36,13 @@ export const setLoadingStatus = (filename: string, index?: number | null) => {
3636
export const setLoadingTotalFiles = (total: number) => {
3737
totalFiles = total;
3838
};
39+
40+
export const setError = (error: string) => {
41+
const loaderErrorMessage = document.getElementById(IDS.loaderErrorMessage);
42+
43+
if (!loaderErrorMessage) return;
44+
loaderErrorMessage.innerText = error;
45+
46+
setHiddenStatusOnId(IDS.loadingStatus, true);
47+
setHiddenStatusOnId(IDS.loaderError, false);
48+
};

0 commit comments

Comments
 (0)