* init * fix: electron Signed-off-by: Innei <i@innei.in> * update Signed-off-by: Innei <i@innei.in> * fix: build Signed-off-by: Innei <i@innei.in> * fix: lint Signed-off-by: Innei <i@innei.in> * fix: test Signed-off-by: Innei <i@innei.in> --------- Signed-off-by: Innei <i@innei.in>
38 lines
880 B
TypeScript
38 lines
880 B
TypeScript
import { useEffect, useRef } from "react"
|
|
import { useLocation } from "react-router-dom"
|
|
|
|
export const parseError = (error: unknown): { message?: string; stack?: string } => {
|
|
if (error instanceof Error) {
|
|
return {
|
|
message: error.message,
|
|
stack: error.stack,
|
|
}
|
|
} else {
|
|
return {
|
|
message: String(error),
|
|
stack: undefined,
|
|
}
|
|
}
|
|
}
|
|
|
|
export class CustomSafeError extends Error {
|
|
constructor(message: string) {
|
|
super(message)
|
|
}
|
|
}
|
|
|
|
export const useResetErrorWhenRouteChange = (resetError: () => void) => {
|
|
const location = useLocation()
|
|
const currentPathnameRef = useRef(location.pathname)
|
|
const onceRef = useRef(false)
|
|
useEffect(() => {
|
|
if (onceRef.current) {
|
|
return
|
|
}
|
|
if (currentPathnameRef.current !== location.pathname) {
|
|
resetError()
|
|
onceRef.current = true
|
|
}
|
|
}, [location.pathname])
|
|
}
|