1
1
// DO NOT IMPORT window.config HERE!
2
2
// to make sure the error handler always works, we should never import `window.config`, because
3
3
// some user's custom template breaks it.
4
+ import type { Intent } from './types.ts' ;
4
5
5
6
// This sets up the URL prefix used in webpack's chunk loading.
6
7
// This file must be imported before any lazy-loading is being attempted.
7
8
__webpack_public_path__ = `${ window . config ?. assetUrlPrefix ?? '/assets' } /` ;
8
9
9
- function shouldIgnoreError ( err ) {
10
+ function shouldIgnoreError ( err : Error ) {
10
11
const ignorePatterns = [
11
12
'/assets/js/monaco.' , // https://github.com/go-gitea/gitea/issues/30861 , https://github.com/microsoft/monaco-editor/issues/4496
12
13
] ;
@@ -16,14 +17,14 @@ function shouldIgnoreError(err) {
16
17
return false ;
17
18
}
18
19
19
- export function showGlobalErrorMessage ( msg , msgType = 'error' ) {
20
+ export function showGlobalErrorMessage ( msg : string , msgType : Intent = 'error' ) {
20
21
const msgContainer = document . querySelector ( '.page-content' ) ?? document . body ;
21
22
const msgCompact = msg . replace ( / \W / g, '' ) . trim ( ) ; // compact the message to a data attribute to avoid too many duplicated messages
22
- let msgDiv = msgContainer . querySelector ( `.js-global-error[data-global-error-msg-compact="${ msgCompact } "]` ) ;
23
+ let msgDiv = msgContainer . querySelector < HTMLDivElement > ( `.js-global-error[data-global-error-msg-compact="${ msgCompact } "]` ) ;
23
24
if ( ! msgDiv ) {
24
25
const el = document . createElement ( 'div' ) ;
25
26
el . innerHTML = `<div class="ui container js-global-error tw-my-[--page-spacing]"><div class="ui ${ msgType } message tw-text-center tw-whitespace-pre-line"></div></div>` ;
26
- msgDiv = el . childNodes [ 0 ] ;
27
+ msgDiv = el . childNodes [ 0 ] as HTMLDivElement ;
27
28
}
28
29
// merge duplicated messages into "the message (count)" format
29
30
const msgCount = Number ( msgDiv . getAttribute ( `data-global-error-msg-count` ) ) + 1 ;
@@ -33,18 +34,7 @@ export function showGlobalErrorMessage(msg, msgType = 'error') {
33
34
msgContainer . prepend ( msgDiv ) ;
34
35
}
35
36
36
- /**
37
- * @param {ErrorEvent|PromiseRejectionEvent } event - Event
38
- * @param {string } event.message - Only present on ErrorEvent
39
- * @param {string } event.error - Only present on ErrorEvent
40
- * @param {string } event.type - Only present on ErrorEvent
41
- * @param {string } event.filename - Only present on ErrorEvent
42
- * @param {number } event.lineno - Only present on ErrorEvent
43
- * @param {number } event.colno - Only present on ErrorEvent
44
- * @param {string } event.reason - Only present on PromiseRejectionEvent
45
- * @param {number } event.promise - Only present on PromiseRejectionEvent
46
- */
47
- function processWindowErrorEvent ( { error, reason, message, type, filename, lineno, colno} ) {
37
+ function processWindowErrorEvent ( { error, reason, message, type, filename, lineno, colno} : ErrorEvent & PromiseRejectionEvent ) {
48
38
const err = error ?? reason ;
49
39
const assetBaseUrl = String ( new URL ( __webpack_public_path__ , window . location . origin ) ) ;
50
40
const { runModeIsProd} = window . config ?? { } ;
@@ -90,7 +80,8 @@ function initGlobalErrorHandler() {
90
80
}
91
81
// then, change _globalHandlerErrors to an object with push method, to process further error
92
82
// events directly
93
- window . _globalHandlerErrors = { _inited : true , push : ( e ) => processWindowErrorEvent ( e ) } ;
83
+ // @ts -expect-error -- this should be refactored to not use a fake array
84
+ window . _globalHandlerErrors = { _inited : true , push : ( e : ErrorEvent & PromiseRejectionEvent ) => processWindowErrorEvent ( e ) } ;
94
85
}
95
86
96
87
initGlobalErrorHandler ( ) ;
0 commit comments