2021-09-09 13:51:06 +00:00
|
|
|
import React from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-09-09 13:51:06 +00:00
|
|
|
import { HttpError } from "@lib/core/http/error";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
statusCode?: number | null;
|
|
|
|
error?: Error | HttpError | null;
|
|
|
|
message?: string;
|
|
|
|
/** Display debugging information */
|
|
|
|
displayDebug?: boolean;
|
|
|
|
children?: never;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
displayDebug: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const ErrorDebugPanel: React.FC<{ error: Props["error"]; children?: never }> = (props) => {
|
|
|
|
const { error: e } = props;
|
|
|
|
|
|
|
|
const debugMap = [
|
|
|
|
["error.message", e?.message],
|
|
|
|
["error.name", e?.name],
|
|
|
|
["error.class", e instanceof Error ? e.constructor.name : undefined],
|
|
|
|
["http.url", e instanceof HttpError ? e.url : undefined],
|
|
|
|
["http.status", e instanceof HttpError ? e.statusCode : undefined],
|
|
|
|
["http.cause", e instanceof HttpError ? e.cause?.message : undefined],
|
|
|
|
["error.stack", e instanceof Error ? e.stack : undefined],
|
|
|
|
];
|
|
|
|
|
|
|
|
return (
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="bg-default overflow-hidden shadow sm:rounded-lg">
|
|
|
|
<div className="border-subtle border-t px-4 py-5 sm:p-0">
|
|
|
|
<dl className="sm:divide-subtle sm:divide-y">
|
2021-09-09 13:51:06 +00:00
|
|
|
{debugMap.map(([key, value]) => {
|
|
|
|
if (value !== undefined) {
|
|
|
|
return (
|
2022-02-09 00:05:13 +00:00
|
|
|
<div key={key} className="py-4 sm:grid sm:grid-cols-3 sm:gap-4 sm:py-5 sm:px-6">
|
2023-04-05 18:14:46 +00:00
|
|
|
<dt className="text-emphasis text-sm font-bold">{key}</dt>
|
|
|
|
<dd className="text-emphasis mt-1 text-sm sm:col-span-2 sm:mt-0">{value}</dd>
|
2021-09-09 13:51:06 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})}
|
|
|
|
</dl>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ErrorPage: React.FC<Props> = (props) => {
|
|
|
|
const { message, statusCode, error, displayDebug } = { ...defaultProps, ...props };
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="bg-default min-h-screen px-4">
|
2022-02-09 00:05:13 +00:00
|
|
|
<main className="mx-auto max-w-xl pb-6 pt-16 sm:pt-24">
|
2021-09-09 13:51:06 +00:00
|
|
|
<div className="text-center">
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-emphasis text-sm font-semibold uppercase tracking-wide">{statusCode}</p>
|
|
|
|
<h1 className="text-emphasis mt-2 text-4xl font-extrabold tracking-tight sm:text-5xl">
|
2021-09-09 13:51:06 +00:00
|
|
|
{message}
|
|
|
|
</h1>
|
|
|
|
</div>
|
|
|
|
</main>
|
|
|
|
{displayDebug && (
|
|
|
|
<div className="flex-wrap">
|
|
|
|
<ErrorDebugPanel error={error} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|