diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2024-01-15 06:12:33 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-15 11:12:33 +0000 |
commit | 48f1d012ad5045319d4e492dfbefa39da92817b2 (patch) | |
tree | 51b5b53a7b1192f04a8292c3a9aa0f5063c64b46 /ext/js/pages | |
parent | 9cfa20cbd2289d82a5abf05a2e5ea8af5016222c (diff) |
Add toError utility function (#523)
* Add asError utility function
* Rename to toError
Diffstat (limited to 'ext/js/pages')
7 files changed, 17 insertions, 10 deletions
diff --git a/ext/js/pages/settings/anki-controller.js b/ext/js/pages/settings/anki-controller.js index d64034a5..c8daa85b 100644 --- a/ext/js/pages/settings/anki-controller.js +++ b/ext/js/pages/settings/anki-controller.js @@ -19,6 +19,7 @@ import {AnkiConnect} from '../../comm/anki-connect.js'; import {EventListenerCollection, log} from '../../core.js'; import {ExtensionError} from '../../core/extension-error.js'; +import {toError} from '../../core/to-error.js'; import {AnkiUtil} from '../../data/anki-util.js'; import {querySelectorNotNull} from '../../dom/query-selector.js'; import {SelectorObserver} from '../../dom/selector-observer.js'; @@ -409,7 +410,7 @@ export class AnkiController { this._sortStringArray(result); return [result, null]; } catch (e) { - return [[], e instanceof Error ? e : new Error(`${e}`)]; + return [[], toError(e)]; } } @@ -422,7 +423,7 @@ export class AnkiController { this._sortStringArray(result); return [result, null]; } catch (e) { - return [[], e instanceof Error ? e : new Error(`${e}`)]; + return [[], toError(e)]; } } @@ -487,7 +488,7 @@ export class AnkiController { try { await this._testAnkiNoteViewer(mode); } catch (e) { - this._setAnkiNoteViewerStatus(true, e instanceof Error ? e : new Error(`${e}`)); + this._setAnkiNoteViewerStatus(true, toError(e)); return; } this._setAnkiNoteViewerStatus(true, null); diff --git a/ext/js/pages/settings/anki-templates-controller.js b/ext/js/pages/settings/anki-templates-controller.js index 56e992b0..0c82cecc 100644 --- a/ext/js/pages/settings/anki-templates-controller.js +++ b/ext/js/pages/settings/anki-templates-controller.js @@ -17,6 +17,7 @@ */ import {ExtensionError} from '../../core/extension-error.js'; +import {toError} from '../../core/to-error.js'; import {AnkiNoteBuilder} from '../../data/anki-note-builder.js'; import {querySelectorNotNull} from '../../dom/query-selector.js'; import {JapaneseUtil} from '../../language/sandbox/japanese-util.js'; @@ -266,7 +267,7 @@ export class AnkiTemplatesController { allErrors.push(...errors); } } catch (e) { - allErrors.push(e instanceof Error ? e : new Error(`${e}`)); + allErrors.push(toError(e)); } /** diff --git a/ext/js/pages/settings/backup-controller.js b/ext/js/pages/settings/backup-controller.js index 2ae52925..457acc18 100644 --- a/ext/js/pages/settings/backup-controller.js +++ b/ext/js/pages/settings/backup-controller.js @@ -19,6 +19,7 @@ import {Dexie} from '../../../lib/dexie.js'; import {isObject, log} from '../../core.js'; import {parseJson} from '../../core/json.js'; +import {toError} from '../../core/to-error.js'; import {OptionsUtil} from '../../data/options-util.js'; import {ArrayBufferUtil} from '../../data/sandbox/array-buffer-util.js'; import {querySelectorNotNull} from '../../dom/query-selector.js'; @@ -498,7 +499,7 @@ export class BackupController { try { await this._importSettingsFile(file); } catch (error) { - this._showSettingsImportError(error instanceof Error ? error : new Error(`${error}`)); + this._showSettingsImportError(toError(error)); } } diff --git a/ext/js/pages/settings/dictionary-import-controller.js b/ext/js/pages/settings/dictionary-import-controller.js index eadfcb91..0c2fa7a5 100644 --- a/ext/js/pages/settings/dictionary-import-controller.js +++ b/ext/js/pages/settings/dictionary-import-controller.js @@ -18,6 +18,7 @@ import {log} from '../../core.js'; import {ExtensionError} from '../../core/extension-error.js'; +import {toError} from '../../core/to-error.js'; import {DictionaryWorker} from '../../dictionary/dictionary-worker.js'; import {querySelectorNotNull} from '../../dom/query-selector.js'; import {yomitan} from '../../yomitan.js'; @@ -126,7 +127,7 @@ export class DictionaryImportController { this._showErrors(errors); } } catch (error) { - this._showErrors([error instanceof Error ? error : new Error(`${error}`)]); + this._showErrors([toError(error)]); } finally { prevention.end(); this._setModifying(false); @@ -200,7 +201,7 @@ export class DictionaryImportController { await this._importDictionary(files[i], importDetails, onProgress); } } catch (err) { - this._showErrors([err instanceof Error ? err : new Error(`${err}`)]); + this._showErrors([toError(err)]); } finally { prevention.end(); for (const progress of progressContainers) { progress.hidden = true; } diff --git a/ext/js/pages/settings/mecab-controller.js b/ext/js/pages/settings/mecab-controller.js index 9c55c9a0..f57bc3c9 100644 --- a/ext/js/pages/settings/mecab-controller.js +++ b/ext/js/pages/settings/mecab-controller.js @@ -16,6 +16,7 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +import {toError} from '../../core/to-error.js'; import {querySelectorNotNull} from '../../dom/query-selector.js'; import {yomitan} from '../../yomitan.js'; @@ -57,7 +58,7 @@ export class MecabController { await yomitan.api.testMecab(); this._setStatus('Connection was successful', false); } catch (e) { - this._setStatus(e instanceof Error ? e.message : `${e}`, true); + this._setStatus(toError(e).message, true); } finally { this._testActive = false; /** @type {HTMLButtonElement} */ (this._testButton).disabled = false; diff --git a/ext/js/pages/settings/permissions-origin-controller.js b/ext/js/pages/settings/permissions-origin-controller.js index 3a9db602..73f06a56 100644 --- a/ext/js/pages/settings/permissions-origin-controller.js +++ b/ext/js/pages/settings/permissions-origin-controller.js @@ -17,6 +17,7 @@ */ import {EventListenerCollection} from '../../core.js'; +import {toError} from '../../core/to-error.js'; import {querySelectorNotNull} from '../../dom/query-selector.js'; export class PermissionsOriginController { @@ -155,7 +156,7 @@ export class PermissionsOriginController { } catch (e) { const errorContainer = /** @type {HTMLElement} */ (this._errorContainer); errorContainer.hidden = false; - errorContainer.textContent = e instanceof Error ? e.message : `${e}`; + errorContainer.textContent = toError(e).message; } if (!added) { return false; } await this._updatePermissions(); diff --git a/ext/js/pages/settings/recommended-permissions-controller.js b/ext/js/pages/settings/recommended-permissions-controller.js index a870de50..0500759e 100644 --- a/ext/js/pages/settings/recommended-permissions-controller.js +++ b/ext/js/pages/settings/recommended-permissions-controller.js @@ -17,6 +17,7 @@ */ import {EventListenerCollection} from '../../core.js'; +import {toError} from '../../core/to-error.js'; export class RecommendedPermissionsController { /** @@ -92,7 +93,7 @@ export class RecommendedPermissionsController { } catch (e) { if (this._errorContainer !== null) { this._errorContainer.hidden = false; - this._errorContainer.textContent = e instanceof Error ? e.message : `${e}`; + this._errorContainer.textContent = toError(e).message; } } if (!added) { return false; } |