-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy patherror.js
More file actions
114 lines (98 loc) · 3.15 KB
/
error.js
File metadata and controls
114 lines (98 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2025 naruya
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
window.addEventListener('error', (event) => {
const el = document.getElementById("error-display");
el.style.visibility = "visible";
if (event.target !== window) {
// Resource Error
const tagName = event.target.tagName;
let resourceUrl = "";
if (tagName === "IMG" || tagName === "SCRIPT") {
resourceUrl = event.target.src;
} else if (tagName === "LINK") {
resourceUrl = event.target.href;
} else {
resourceUrl = "(unknown resource URL)";
}
el.innerHTML += `
<p style="color:red;">
Resource load error: <${tagName}><br>
URL: ${resourceUrl}
</p>
`;
} else {
// Runtime Error
const filename = event.filename || "(no filename)";
const lineno = event.lineno || "(no line)";
const colno = event.colno || "(no column)";
const message = event.message || "(no message)";
el.innerHTML += `
<p style="color:red;">
<strong>Message:</strong> ${message}<br>
<strong>File:</strong> ${filename}<br>
<strong>Line:</strong> ${lineno}:${colno}
</p>
`;
}
}, true);
// Errors in Promise
window.addEventListener('unhandledrejection', (event) => {
const el = document.getElementById("error-display");
el.style.visibility = "visible";
el.innerHTML += `<p style="color:red;">Unhandled Promise Error: ${event.reason}</p>`;
});
// for WebGL Errors
(function() {
const originalError = console.error;
console.error = function(...args) {
originalError.apply(console, args);
const el = document.getElementById("error-display");
if (el) {
el.style.visibility = "visible";
const msg = args.map(a => String(a)).join(" ");
el.innerHTML += `<p style="color:red;">[console.error] ${msg}</p>`;
}
};
})();
// for Fetch Errors
(function() {
const originalFetch = window.fetch;
window.fetch = function(resource, init) {
const method = init?.method || 'GET';
let url = resource instanceof Request ? resource.url : resource;
// 相対URLを絶対URLに変換
if (!/^https?:\/\//i.test(url)) {
const a = document.createElement('a');
a.href = url;
url = a.href; // フルURL(プロトコル、ホスト、パスを含む)
}
return originalFetch.apply(this, arguments)
.then(response => {
if (!response.ok) {
const el = document.getElementById("error-display");
if (el) {
el.style.visibility = "visible";
el.innerHTML += `
<p style="color:red;">
${method} ${url} ${response.status} (${response.statusText})
</p>
`;
}
}
return response;
})
.catch(error => {
const el = document.getElementById("error-display");
if (el) {
el.style.visibility = "visible";
el.innerHTML += `
<p style="color:red;">
Fetch Error: ${method} ${url}<br>
${error.message}
</p>
`;
}
throw error;
});
};
})();