fix: read aggregate and error cause bubbles

This commit is contained in:
Aarnav Tale 2025-01-06 08:47:24 +05:30
parent 888c1a5fb6
commit 1316439786
No known key found for this signature in database

View File

@ -8,11 +8,34 @@ interface Props {
type?: 'full' | 'embedded';
}
function getMessage(error: Error | unknown) {
if (!(error instanceof Error)) {
return "An unknown error occurred";
}
let rootError = error;
// Traverse the error chain to find the root cause
if (error.cause) {
rootError = error.cause;
while (rootError.cause) {
rootError = rootError.cause;
}
}
// If we are aggregate, concat into a single message
if (rootError instanceof AggregateError) {
return rootError.errors.map((error) => error.message).join('\n');
}
return rootError.message;
}
export function ErrorPopup({ type = 'full' }: Props) {
const error = useRouteError();
const routing = isRouteErrorResponse(error);
const message =
error instanceof Error ? error.message : 'An unexpected error occurred';
const message = getMessage(error);
return (
<div