Add support for logging Anki errors on the settings page (#2073)

This commit is contained in:
toasted-nutbread 2022-02-20 09:35:54 -05:00 committed by GitHub
parent 045eb63a52
commit 48b2807120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 2 deletions

View File

@ -183,7 +183,7 @@ class AnkiConnect {
}); });
} catch (e) { } catch (e) {
const error = new Error('Anki connection failure'); const error = new Error('Anki connection failure');
error.data = {action, params}; error.data = {action, params, originalError: e};
throw error; throw error;
} }
@ -200,7 +200,7 @@ class AnkiConnect {
result = JSON.parse(responseText); result = JSON.parse(responseText);
} catch (e) { } catch (e) {
const error = new Error('Invalid Anki response'); const error = new Error('Invalid Anki response');
error.data = {action, params, status: response.status, responseText}; error.data = {action, params, status: response.status, responseText, originalError: e};
throw error; throw error;
} }

View File

@ -43,6 +43,7 @@ class AnkiController {
this._ankiErrorMessageDetailsToggle = null; this._ankiErrorMessageDetailsToggle = null;
this._ankiErrorInvalidResponseInfo = null; this._ankiErrorInvalidResponseInfo = null;
this._ankiCardPrimary = null; this._ankiCardPrimary = null;
this._ankiError = null;
this._validateFieldsToken = null; this._validateFieldsToken = null;
} }
@ -70,6 +71,8 @@ class AnkiController {
input.addEventListener('change', this._onAnkiCardPrimaryTypeRadioChange.bind(this), false); input.addEventListener('change', this._onAnkiCardPrimaryTypeRadioChange.bind(this), false);
} }
document.querySelector('#anki-error-log').addEventListener('click', this._onAnkiErrorLogLinkClick.bind(this));
const options = await this._settingsController.getOptions(); const options = await this._settingsController.getOptions();
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this)); this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
this._onOptionsChanged({options}); this._onOptionsChanged({options});
@ -184,6 +187,11 @@ class AnkiController {
this._setAnkiCardPrimaryType(node.dataset.value, node.dataset.ankiCardMenu); this._setAnkiCardPrimaryType(node.dataset.value, node.dataset.ankiCardMenu);
} }
_onAnkiErrorLogLinkClick() {
if (this._ankiError === null) { return; }
console.log({error: this._ankiError});
}
_setAnkiCardPrimaryType(ankiCardType, ankiCardMenu) { _setAnkiCardPrimaryType(ankiCardType, ankiCardMenu) {
if (this._ankiCardPrimary === null) { return; } if (this._ankiCardPrimary === null) { return; }
this._ankiCardPrimary.dataset.ankiCardType = ankiCardType; this._ankiCardPrimary.dataset.ankiCardType = ankiCardType;
@ -296,9 +304,12 @@ class AnkiController {
this._ankiErrorMessageNode.textContent = (this._ankiConnect.enabled ? 'Connected' : 'Not enabled'); this._ankiErrorMessageNode.textContent = (this._ankiConnect.enabled ? 'Connected' : 'Not enabled');
this._ankiErrorMessageNode.classList.remove('danger-text'); this._ankiErrorMessageNode.classList.remove('danger-text');
this._ankiErrorMessageDetailsNode.textContent = ''; this._ankiErrorMessageDetailsNode.textContent = '';
this._ankiError = null;
} }
_showAnkiError(error) { _showAnkiError(error) {
this._ankiError = error;
let errorString = typeof error === 'object' && error !== null ? error.message : null; let errorString = typeof error === 'object' && error !== null ? error.message : null;
if (!errorString) { errorString = `${error}`; } if (!errorString) { errorString = `${error}`; }
if (!/[.!?]$/.test(errorString)) { errorString += '.'; } if (!/[.!?]$/.test(errorString)) { errorString += '.'; }

View File

@ -1569,6 +1569,9 @@
</div> </div>
</div> </div>
<div class="settings-item-children" id="anki-error-message-details-container" hidden> <div class="settings-item-children" id="anki-error-message-details-container" hidden>
<p>
<a tabindex="0" id="anki-error-log">Log error to console</a>
</p>
<p class="danger-text" id="anki-error-invalid-response-info" hidden> <p class="danger-text" id="anki-error-invalid-response-info" hidden>
Attempting to connect to Anki can sometimes return an error message which includes "Invalid response", Attempting to connect to Anki can sometimes return an error message which includes "Invalid response",
which may indicate that the value of the <em>AnkiConnect server address</em> option is incorrect. which may indicate that the value of the <em>AnkiConnect server address</em> option is incorrect.