Add error logging util

This commit is contained in:
keanutaufan 2024-09-14 23:11:47 +07:00
parent 2adebd15a1
commit 006377b31a
No known key found for this signature in database
GPG Key ID: 0E2EF5BE1DA75288
4 changed files with 41 additions and 1 deletions

View File

@ -9,6 +9,7 @@
<canvas id="canvas" width="1200px" height="600px"> <canvas id="canvas" width="1200px" height="600px">
HTML5 Canvas is not supported. HTML5 Canvas is not supported.
</canvas> </canvas>
<div id="errorText"></div>
<script type="module" src="/src/main.ts"></script> <script type="module" src="/src/main.ts"></script>
</body> </body>
</html> </html>

10
src/debug.ts Normal file
View File

@ -0,0 +1,10 @@
export function showError(error: any) {
const p = document.querySelector<HTMLDivElement>("#errorText");
if (p) {
const newErrorText = document.createElement("p");
newErrorText.innerText = error;
p.appendChild(newErrorText);
}
console.error(error);
}

View File

@ -1 +1,20 @@
import { showError } from "./debug";
import "./style.css"; import "./style.css";
function render() {
const canvas = document.querySelector<HTMLCanvasElement>("#canvas");
if (!canvas) {
throw new Error("Canvas does not exist");
}
const gl = canvas.getContext("webgl");
if (!gl) {
throw new Error("This browser does not support WebGL");
}
}
try {
render();
} catch (err) {
showError(err);
}

View File

@ -5,6 +5,7 @@ html, head, body {
body { body {
display: flex; display: flex;
flex-direction: column;
height: 100vh; height: 100vh;
padding: 1rem; padding: 1rem;
box-sizing: border-box; box-sizing: border-box;
@ -18,3 +19,12 @@ body {
background-color: #da6052; background-color: #da6052;
image-rendering: crisp-edges; image-rendering: crisp-edges;
} }
#errorText {
color: #de5243;
text-align: center;
}
#errorText p {
font-size: 2rem;
}