wip: better error handling

This commit is contained in:
syuilo 2021-03-03 00:25:13 +09:00
parent 878f09460c
commit 7e93319873
2 changed files with 59 additions and 25 deletions

View file

@ -67,6 +67,7 @@ console.info(`Misskey v${version}`);
// boot.jsのやつを解除 // boot.jsのやつを解除
window.onerror = null; window.onerror = null;
window.onunhandledrejection = null;
if (_DEV_) { if (_DEV_) {
console.warn('Development mode!!!'); console.warn('Development mode!!!');

View file

@ -11,12 +11,15 @@
'use strict'; 'use strict';
window.onerror = (e) => {
document.documentElement.innerHTML = '問題が発生しました。';
};
// ブロックの中に入れないと、定義した変数がブラウザのグローバルスコープに登録されてしまい邪魔なので // ブロックの中に入れないと、定義した変数がブラウザのグローバルスコープに登録されてしまい邪魔なので
(async () => { (async () => {
window.onerror = (e) => {
renderError('SOMETHING_HAPPENED', e.toString());
};
window.onunhandledrejection = (e) => {
renderError('SOMETHING_HAPPENED_IN_PROMISE', e.toString());
};
const v = localStorage.getItem('v') || VERSION; const v = localStorage.getItem('v') || VERSION;
//#region Detect language & fetch translations //#region Detect language & fetch translations
@ -38,9 +41,17 @@ window.onerror = (e) => {
} }
const res = await fetch(`/assets/locales/${lang}.${v}.json`); const res = await fetch(`/assets/locales/${lang}.${v}.json`);
localStorage.setItem('lang', lang); if (res.status === 200) {
localStorage.setItem('locale', await res.text()); localStorage.setItem('lang', lang);
localStorage.setItem('localeVersion', v); localStorage.setItem('locale', await res.text());
localStorage.setItem('localeVersion', v);
} else if (localeOutdated) {
// nop
} else {
renderError('LOCALE_FETCH_FAILED');
checkUpdate();
return;
}
} }
//#endregion //#endregion
@ -56,24 +67,8 @@ window.onerror = (e) => {
script.setAttribute('async', 'true'); script.setAttribute('async', 'true');
script.setAttribute('defer', 'true'); script.setAttribute('defer', 'true');
script.addEventListener('error', async () => { script.addEventListener('error', async () => {
document.documentElement.innerHTML = '読み込みに失敗しました。'; renderError('APP_FETCH_FAILED');
checkUpdate();
// TODO: サーバーが落ちている場合などのエラーハンドリング
const res = await fetch('/api/meta', {
method: 'POST',
cache: 'no-cache'
});
const meta = await res.json();
if (meta.version != v) {
localStorage.setItem('v', meta.version);
alert(
'Misskeyの新しいバージョンがあります。ページを再度読み込みします。' +
'\n\n' +
'New version of Misskey available. The page will be reloaded.');
refresh();
}
}); });
head.appendChild(script); head.appendChild(script);
//#endregion //#endregion
@ -112,6 +107,44 @@ window.onerror = (e) => {
document.documentElement.style.backgroundImage = `url(${wallpaper})`; document.documentElement.style.backgroundImage = `url(${wallpaper})`;
} }
// eslint-disable-next-line no-inner-declarations
function renderError(code, details) {
document.documentElement.innerHTML = `
<h1>エラーが発生しました</h1>
<p>問題が解決しない場合は管理者までお問い合わせください以下のオプションを試すこともできます:</p>
<ul>
<li><a href="/cli">簡易クライアント</a></li>
<li><a href="/bios">BIOS</a></li>
<li><a href="/flush">キャッシュをクリア</a></li>
</ul>
<hr>
<code>ERROR CODE: ${code}</code>
<details>
${details}
</details>
`;
}
// eslint-disable-next-line no-inner-declarations
async function checkUpdate() {
// TODO: サーバーが落ちている場合などのエラーハンドリング
const res = await fetch('/api/meta', {
method: 'POST',
cache: 'no-cache'
});
const meta = await res.json();
if (meta.version != v) {
localStorage.setItem('v', meta.version);
alert(
'Misskeyの新しいバージョンがあります。ページを再度読み込みします。' +
'\n\n' +
'New version of Misskey available. The page will be reloaded.');
refresh();
}
}
// eslint-disable-next-line no-inner-declarations // eslint-disable-next-line no-inner-declarations
function refresh() { function refresh() {
// Random // Random