2023-11-05 23:33:40 +00:00
|
|
|
import { ErrorInfo } from "react"
|
2023-10-26 14:14:24 +00:00
|
|
|
|
|
|
|
|
2023-10-27 10:57:34 +00:00
|
|
|
interface ComponentError {
|
|
|
|
timestamp: number;
|
|
|
|
message: string;
|
|
|
|
callStack: string | undefined;
|
2024-03-18 14:02:28 +00:00
|
|
|
componentStack: string | null | undefined;
|
2023-10-27 10:57:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 14:14:24 +00:00
|
|
|
export function handleComponentError(error: Error, info: ErrorInfo) {
|
2023-11-05 23:33:40 +00:00
|
|
|
const componentError: ComponentError = {
|
|
|
|
timestamp: Math.floor(Date.now() / 1000),
|
|
|
|
message: error.message,
|
|
|
|
callStack: error.stack,
|
|
|
|
componentStack: info.componentStack,
|
|
|
|
}
|
|
|
|
|
|
|
|
queueErrorRequest(componentError)
|
2023-10-27 10:57:34 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:33:40 +00:00
|
|
|
let errorsQueue: ComponentError[] = []
|
|
|
|
let timeoutId: ReturnType<typeof setTimeout>
|
2023-10-27 10:57:34 +00:00
|
|
|
|
|
|
|
function queueErrorRequest(error: ComponentError) {
|
2023-11-05 23:33:40 +00:00
|
|
|
errorsQueue.push(error)
|
2023-10-27 10:57:34 +00:00
|
|
|
|
2023-11-05 23:33:40 +00:00
|
|
|
clearTimeout(timeoutId)
|
|
|
|
timeoutId = setTimeout(() => {
|
|
|
|
sendErrorsToServer()
|
|
|
|
}, 1000)
|
2023-10-27 10:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function sendErrorsToServer() {
|
2023-11-05 23:33:40 +00:00
|
|
|
// makeRequest({
|
|
|
|
// url: "",
|
|
|
|
// method: "POST",
|
|
|
|
// body: errorsQueue,
|
|
|
|
// useToken: true,
|
|
|
|
// });
|
|
|
|
errorsQueue = []
|
2023-10-26 14:14:24 +00:00
|
|
|
}
|