frontAnswerer/lib/utils/handleComponentError.ts

43 lines
1021 B
TypeScript
Raw Permalink Normal View History

2024-02-02 14:35:02 +00:00
import { ErrorInfo } from "react";
interface ComponentError {
2024-05-31 16:41:18 +00:00
timestamp: number;
message: string;
callStack: string | undefined;
componentStack: string | null | undefined;
2024-02-02 14:35:02 +00:00
}
export function handleComponentError(error: Error, info: ErrorInfo) {
2024-05-31 16:41:18 +00:00
const componentError: ComponentError = {
timestamp: Math.floor(Date.now() / 1000),
message: error.message,
callStack: error.stack,
componentStack: info.componentStack,
};
queueErrorRequest(componentError);
2024-02-02 14:35:02 +00:00
}
let errorsQueue: ComponentError[] = [];
let timeoutId: ReturnType<typeof setTimeout>;
function queueErrorRequest(error: ComponentError) {
2024-05-31 16:41:18 +00:00
errorsQueue.push(error);
2024-02-02 14:35:02 +00:00
2024-05-31 16:41:18 +00:00
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
sendErrorsToServer();
}, 1000);
2024-02-02 14:35:02 +00:00
}
async function sendErrorsToServer() {
2024-05-31 16:41:18 +00:00
// makeRequest({
// url: "",
// method: "POST",
// body: errorsQueue,
// useToken: true,
// });
console.log(`Fake-sending ${errorsQueue.length} errors to server`, errorsQueue);
errorsQueue = [];
2024-02-02 14:35:02 +00:00
}