aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-01-30 12:33:29 -0500
committerGitHub <noreply@github.com>2021-01-30 12:33:29 -0500
commitd0b8b605db93c51b5ce2501a482c57432b45bfa6 (patch)
treec42bf4f59d7945c8b9c42cd0677e3827d326a71a /ext/bg/js/backend.js
parentaf6e9a8153c24d0400592005b31d56fecff67068 (diff)
Add note errors (#1329)
* Update _addAnkiNote to track errors * Change comparison * Update anki note adding to show errors * Fix template * Show errors when Anki card creation behaves unexpectedly * Update some errors related to anki media injection * Update addAnkiNote error handling * Improve Anki errors * Simplify error messages related to template rendering
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js71
1 files changed, 46 insertions, 25 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 9a8844c5..b0648ac5 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -1539,13 +1539,14 @@ class Backend {
let clipboardImageFileName = null;
let clipboardText = null;
let audioFileName = null;
+ const errors = [];
try {
if (screenshotDetails !== null) {
screenshotFileName = await this._injectAnkNoteScreenshot(ankiConnect, timestamp, definitionDetails, screenshotDetails);
}
} catch (e) {
- // NOP
+ errors.push(serializeError(e));
}
try {
@@ -1553,7 +1554,7 @@ class Backend {
clipboardImageFileName = await this._injectAnkNoteClipboardImage(ankiConnect, timestamp, definitionDetails);
}
} catch (e) {
- // NOP
+ errors.push(serializeError(e));
}
try {
@@ -1561,7 +1562,7 @@ class Backend {
clipboardText = await this._clipboardReader.getText();
}
} catch (e) {
- // NOP
+ errors.push(serializeError(e));
}
try {
@@ -1569,34 +1570,50 @@ class Backend {
audioFileName = await this._injectAnkNoteAudio(ankiConnect, timestamp, definitionDetails, audioDetails);
}
} catch (e) {
- // NOP
+ errors.push(serializeError(e));
}
- return {screenshotFileName, clipboardImageFileName, clipboardText, audioFileName};
+ return {
+ result: {
+ screenshotFileName,
+ clipboardImageFileName,
+ clipboardText,
+ audioFileName
+ },
+ errors
+ };
}
async _injectAnkNoteAudio(ankiConnect, timestamp, definitionDetails, details) {
const {type, expression, reading} = definitionDetails;
- if (type === 'kanji') {
- throw new Error('Cannot inject audio for kanji');
- }
- if (!reading && !expression) {
- throw new Error('Invalid reading and expression');
+ if (
+ type === 'kanji' ||
+ typeof expression !== 'string' ||
+ typeof reading !== 'string' ||
+ (expression.length === 0 && reading.length === 0)
+ ) {
+ return null;
}
const {sources, customSourceUrl, customSourceType} = details;
- const data = await this._downloadDefinitionAudio(
- sources,
- expression,
- reading,
- {
- textToSpeechVoice: null,
- customSourceUrl,
- customSourceType,
- binary: true,
- disableCache: true
- }
- );
+ let data;
+ try {
+ data = await this._downloadDefinitionAudio(
+ sources,
+ expression,
+ reading,
+ {
+ textToSpeechVoice: null,
+ customSourceUrl,
+ customSourceType,
+ binary: true,
+ disableCache: true
+ }
+ );
+ } catch (e) {
+ // No audio
+ return null;
+ }
let fileName = this._generateAnkiNoteMediaFileName('yomichan_audio', '.mp3', timestamp, definitionDetails);
fileName = fileName.replace(/\]/g, '');
@@ -1611,7 +1628,9 @@ class Backend {
const {mediaType, data} = this._getDataUrlInfo(dataUrl);
const extension = this._mediaUtility.getFileExtensionFromImageMediaType(mediaType);
- if (extension === null) { throw new Error('Unknown image media type'); }
+ if (extension === null) {
+ throw new Error('Unknown media type for screenshot image');
+ }
const fileName = this._generateAnkiNoteMediaFileName('yomichan_browser_screenshot', extension, timestamp, definitionDetails);
await ankiConnect.storeMediaFile(fileName, data);
@@ -1622,12 +1641,14 @@ class Backend {
async _injectAnkNoteClipboardImage(ankiConnect, timestamp, definitionDetails) {
const dataUrl = await this._clipboardReader.getImage();
if (dataUrl === null) {
- throw new Error('No clipboard image');
+ return null;
}
const {mediaType, data} = this._getDataUrlInfo(dataUrl);
const extension = this._mediaUtility.getFileExtensionFromImageMediaType(mediaType);
- if (extension === null) { throw new Error('Unknown image media type'); }
+ if (extension === null) {
+ throw new Error('Unknown media type for clipboard image');
+ }
const fileName = this._generateAnkiNoteMediaFileName('yomichan_clipboard_image', extension, timestamp, definitionDetails);
await ankiConnect.storeMediaFile(fileName, data);