Skip to content

Commit

Permalink
Add toError utility function (#523)
Browse files Browse the repository at this point in the history
* Add asError utility function

* Rename to toError
  • Loading branch information
toasted-nutbread authored Jan 15, 2024
1 parent 9cfa20c commit 48f1d01
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 27 deletions.
7 changes: 4 additions & 3 deletions dev/dictionary-validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {performance} from 'perf_hooks';
import {fileURLToPath} from 'url';
import {parseJson} from './json.js';
import {createJsonSchema} from './schema-validate.js';
import {toError} from './to-error.js';

const dirname = path.dirname(fileURLToPath(import.meta.url));

Expand All @@ -47,7 +48,7 @@ async function validateDictionaryBanks(mode, zip, fileNameFormat, schema) {
try {
jsonSchema = createJsonSchema(mode, schema);
} catch (e) {
const e2 = e instanceof Error ? e : new Error(`${e}`);
const e2 = toError(e);
e2.message += `\n(in file ${fileNameFormat})}`;
throw e2;
}
Expand All @@ -62,7 +63,7 @@ async function validateDictionaryBanks(mode, zip, fileNameFormat, schema) {
try {
jsonSchema.validate(data);
} catch (e) {
const e2 = e instanceof Error ? e : new Error(`${e}`);
const e2 = toError(e);
e2.message += `\n(in file ${fileName})}`;
throw e2;
}
Expand Down Expand Up @@ -92,7 +93,7 @@ export async function validateDictionary(mode, archive, schemas) {
const jsonSchema = createJsonSchema(mode, schemas.index);
jsonSchema.validate(index);
} catch (e) {
const e2 = e instanceof Error ? e : new Error(`${e}`);
const e2 = toError(e);
e2.message += `\n(in file ${indexFileName})}`;
throw e2;
}
Expand Down
18 changes: 18 additions & 0 deletions dev/to-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) 2023 Yomitan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

export {toError} from '../ext/js/core/to-error.js';
3 changes: 2 additions & 1 deletion ext/js/comm/mecab.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import {EventListenerCollection} from '../core.js';
import {toError} from '../core/to-error.js';

/**
* This class is used to connect Yomitan to a native component that is
Expand Down Expand Up @@ -235,7 +236,7 @@ export class Mecab {
try {
await this._setupPortPromise;
} catch (e) {
throw new Error(e instanceof Error ? e.message : `${e}`);
throw toError(e);
}
}

Expand Down
26 changes: 26 additions & 0 deletions ext/js/core/to-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2023 Yomitan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/**
* Utility function to convert an unknown value to an error.
* This is useful for try-catch situations where the catch parameter has the `unknown` type.
* @param {unknown} value
* @returns {Error}
*/
export function toError(value) {
return value instanceof Error ? value : new Error(`${value}`);
}
3 changes: 2 additions & 1 deletion ext/js/dictionary/dictionary-importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import {stringReverse} from '../core.js';
import {ExtensionError} from '../core/extension-error.js';
import {parseJson} from '../core/json.js';
import {toError} from '../core/to-error.js';
import {MediaUtil} from '../media/media-util.js';

const ajvSchemas = /** @type {import('dictionary-importer').CompiledSchemaValidators} */ (/** @type {unknown} */ (ajvSchemas0));
Expand Down Expand Up @@ -206,7 +207,7 @@ export class DictionaryImporter {
try {
await dictionaryDatabase.bulkAdd(objectStoreName, entries, i, count);
} catch (e) {
errors.push(e instanceof Error ? e : new Error(`${e}`));
errors.push(toError(e));
}

this._progressData.index += count;
Expand Down
17 changes: 9 additions & 8 deletions ext/js/display/display-anki.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import {EventListenerCollection, deferPromise} from '../core.js';
import {toError} from '../core/to-error.js';
import {AnkiNoteBuilder} from '../data/anki-note-builder.js';
import {AnkiUtil} from '../data/anki-util.js';
import {PopupMenu} from '../dom/popup-menu.js';
Expand Down Expand Up @@ -159,7 +160,7 @@ export class DisplayAnki {
try {
({note: note, errors, requirements} = await this._createNote(dictionaryEntry, mode, []));
} catch (e) {
errors = [e instanceof Error ? e : new Error(`${e}`)];
errors = [toError(e)];
}
/** @type {import('display-anki').AnkiNoteLogData} */
const entry = {mode, note};
Expand All @@ -174,7 +175,7 @@ export class DisplayAnki {

return {
ankiNoteData,
ankiNoteDataException: ankiNoteDataException instanceof Error ? ankiNoteDataException : new Error(`${ankiNoteDataException}`),
ankiNoteDataException: toError(ankiNoteDataException),
ankiNotes
};
}
Expand Down Expand Up @@ -490,7 +491,7 @@ export class DisplayAnki {
addNoteOkay = true;
} catch (e) {
allErrors.length = 0;
allErrors.push(e instanceof Error ? e : new Error(`${e}`));
allErrors.push(toError(e));
}

if (addNoteOkay) {
Expand All @@ -501,15 +502,15 @@ export class DisplayAnki {
try {
await yomitan.api.suspendAnkiCardsForNote(noteId);
} catch (e) {
allErrors.push(e instanceof Error ? e : new Error(`${e}`));
allErrors.push(toError(e));
}
}
button.disabled = true;
this._updateViewNoteButton(dictionaryEntryIndex, [noteId], true);
}
}
} catch (e) {
allErrors.push(e instanceof Error ? e : new Error(`${e}`));
allErrors.push(toError(e));
} finally {
progressIndicatorVisible.clearOverride(overrideToken);
}
Expand Down Expand Up @@ -647,7 +648,7 @@ export class DisplayAnki {
}
} catch (e) {
infos = this._getAnkiNoteInfoForceValue(notes, false);
ankiError = e instanceof Error ? e : new Error(`${e}`);
ankiError = toError(e);
}

/** @type {import('display-anki').DictionaryEntryDetails[]} */
Expand Down Expand Up @@ -855,11 +856,11 @@ export class DisplayAnki {
await yomitan.api.noteView(noteIds[0], this._noteGuiMode, false);
} catch (e) {
const displayErrors = (
e instanceof Error && e.message === 'Mode not supported' ?
toError(e).message === 'Mode not supported' ?
[this._display.displayGenerator.instantiateTemplateFragment('footer-notification-anki-view-note-error')] :
void 0
);
this._showErrorNotification([e instanceof Error ? e : new Error(`${e}`)], displayErrors);
this._showErrorNotification([toError(e)], displayErrors);
return;
}
}
Expand Down
5 changes: 3 additions & 2 deletions ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {FrameEndpoint} from '../comm/frame-endpoint.js';
import {DynamicProperty, EventDispatcher, EventListenerCollection, clone, deepEqual, log, promiseTimeout} from '../core.js';
import {extendApiMap, invokeApiMapHandler} from '../core/api-map.js';
import {ExtensionError} from '../core/extension-error.js';
import {toError} from '../core/to-error.js';
import {PopupMenu} from '../dom/popup-menu.js';
import {querySelectorNotNull} from '../dom/query-selector.js';
import {ScrollElement} from '../dom/scroll-element.js';
Expand Down Expand Up @@ -787,7 +788,7 @@ export class Display extends EventDispatcher {
break;
}
} catch (e) {
this.onError(e instanceof Error ? e : new Error(`${e}`));
this.onError(toError(e));
}
}

Expand Down Expand Up @@ -922,7 +923,7 @@ export class Display extends EventDispatcher {
};
this.setContent(details);
} catch (error) {
this.onError(error instanceof Error ? error : new Error(`${error}`));
this.onError(toError(error));
}
}

Expand Down
3 changes: 2 additions & 1 deletion ext/js/display/option-toggle-hotkey-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import {generateId} from '../core.js';
import {ExtensionError} from '../core/extension-error.js';
import {toError} from '../core/to-error.js';
import {yomitan} from '../yomitan.js';

export class OptionToggleHotkeyHandler {
Expand Down Expand Up @@ -132,7 +133,7 @@ export class OptionToggleHotkeyHandler {
* @returns {DocumentFragment}
*/
_createErrorMessage(path, error) {
const message = error instanceof Error ? error.message : `${error}`;
const message = toError(error).message;
const fragment = document.createDocumentFragment();
const n1 = document.createElement('em');
n1.textContent = path;
Expand Down
3 changes: 2 additions & 1 deletion ext/js/dom/text-source-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import {toError} from '../core/to-error.js';
import {DocumentUtil} from './document-util.js';
import {DOMTextScanner} from './dom-text-scanner.js';

Expand Down Expand Up @@ -228,7 +229,7 @@ export class TextSourceRange {
try {
return this._range.compareBoundaryPoints(Range.START_TO_START, other.range) === 0;
} catch (e) {
if (e instanceof Error && e.name === 'WrongDocumentError') {
if (toError(e).name === 'WrongDocumentError') {
// This can happen with shadow DOMs if the ranges are in different documents.
return false;
}
Expand Down
7 changes: 4 additions & 3 deletions ext/js/pages/settings/anki-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)];
}
}

Expand All @@ -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)];
}
}

Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion ext/js/pages/settings/anki-templates-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion ext/js/pages/settings/backup-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
}
}

Expand Down
5 changes: 3 additions & 2 deletions ext/js/pages/settings/dictionary-import-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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; }
Expand Down
3 changes: 2 additions & 1 deletion ext/js/pages/settings/mecab-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion ext/js/pages/settings/permissions-origin-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion ext/js/pages/settings/recommended-permissions-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import {EventListenerCollection} from '../../core.js';
import {toError} from '../../core/to-error.js';

export class RecommendedPermissionsController {
/**
Expand Down Expand Up @@ -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; }
Expand Down

0 comments on commit 48f1d01

Please sign in to comment.