28 lines
759 B
TypeScript
28 lines
759 B
TypeScript
// 全域 HTTP 錯誤事件(Playground 專用)
|
|
//
|
|
// 目的:
|
|
// - 避免 HTTP hooks 直接 import router 造成耦合
|
|
// - 由 router 層或 App 層決定要導到哪個錯誤頁與顯示哪些訊息
|
|
|
|
export const HTTP_ERROR_EVENT = 'sk-playground:http-error'
|
|
|
|
export type HttpErrorDetail = {
|
|
status?: number
|
|
message?: string
|
|
}
|
|
|
|
let httpErrorEmitted = false
|
|
|
|
export function emitHttpError(detail: HttpErrorDetail) {
|
|
// 避免同一波大量錯誤觸發多次導頁
|
|
if (httpErrorEmitted) return
|
|
httpErrorEmitted = true
|
|
|
|
window.dispatchEvent(new CustomEvent<HttpErrorDetail>(HTTP_ERROR_EVENT, { detail }))
|
|
|
|
// 下一個 event loop 再允許觸發(避免把 guard 永久鎖住)
|
|
setTimeout(() => {
|
|
httpErrorEmitted = false
|
|
}, 0)
|
|
}
|