Better error pages

This commit is contained in:
Gnarwhal 2024-09-23 18:23:19 +00:00
parent fdae644eb2
commit aa12b94a01
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
6 changed files with 75 additions and 11 deletions

View file

@ -2,8 +2,9 @@
import { useState, useEffect } from 'react'
import NetworkError from './types/error/network';
import ContentTypeError from './types/error/content_type';
import NetworkError from './types/error/network';
import StatusError from './types/error/status';
import ImageContent from './types/image';
import MarkdownContent from './types/markdown';
import Terminal from './types/terminal';
@ -86,17 +87,21 @@ export default function Content({ src }: { src: string}) {
if (content == undefined) {
const result = fetch(src)
.then(response => {
for (const type of recognized_types) {
if (is_type(response, type)) {
const emitted = type.emit();
if (emitted != undefined) {
result.then(emitted.postprocess);
return emitted.process(response);
if (response.status < 400) {
for (const type of recognized_types) {
if (is_type(response, type)) {
const emitted = type.emit();
if (emitted != undefined) {
result.then(emitted.postprocess);
return emitted.process(response);
}
return;
}
return;
}
set_content(<ContentTypeError content_type={response.headers.get('Content-Type')!.split(';')[0]} />);
} else {
set_content(<StatusError status={response.status} />);
}
set_content(<ContentTypeError content_type={response.headers.get('Content-Type')!.split(';')[0]} />);
})
.catch(err => {
set_content(<NetworkError err={err} />);

View file

@ -1,3 +1,5 @@
import Error from './error'
export default function ContentTypeError({ content_type }: { content_type: string }) {
return <p>{`Unrecognised MIME type: ${content_type}`}</p>
return <Error>{`Unrecognised MIME type: ${content_type}`}</Error>
}

View file

@ -0,0 +1,18 @@
import { style } from '@vanilla-extract/css'
export const error = style({
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
width: '100%',
height: 'max-content',
});
export const message = style({
fontSize: '2em',
});
export const sad = style({
margin: '0em',
fontSize: '3em',
})

View file

@ -0,0 +1,13 @@
import * as style from './error.css'
const faces = [
"(•́︵•̀)",
"(╥﹏╥)",
]
export default function Error({ children }: { children }) {
return <div className={style.error}>
<p className={style.message}>{children}</p>
<p className={style.sad}>{faces[Math.floor(Math.random() * faces.length)]}</p>
</div>
}

View file

@ -1,3 +1,6 @@
import Error from './error'
export default function NetworkError({ err }: { err: string }) {
return <p>{`Error: ${err}`}</p>;
console.err(err);
return <Error>Network Error</Error>;
}

View file

@ -0,0 +1,23 @@
import Error from './error'
const statuses = {
[400]: "Bad Request",
[401]: "Unauthorized",
[402]: "Payment Required",
[403]: "Forbidden",
[404]: "Not Found",
[405]: "Method Not Allowed",
[500]: "Internal Server Error",
[501]: "Not Implemented",
[502]: "Bad Gateway",
[503]: "Service Unavailable",
[504]: "Gateway Timeout",
[505]: "HTTP Version Not Supported"
}
export default function StatusError({ status }: { status: number }) {
if (status in statuses) {
return <Error>{`${status} ${statuses[status as keyof typeof statuses]}`}</Error>
} else {
return <Error>{status}</Error>
}
}