Просмотр исходного кода

feat: add custom error instances

Sv443 7 месяцев назад
Родитель
Сommit
0f694bb832
5 измененных файлов с 22 добавлено и 8 удалено
  1. 0 1
      src/dialogs/pluginList.ts
  2. 3 3
      src/features/lyrics.ts
  3. 4 4
      src/interface.ts
  4. 14 0
      src/utils/errors.ts
  5. 1 0
      src/utils/index.ts

+ 0 - 1
src/dialogs/pluginList.ts

@@ -70,7 +70,6 @@ async function renderBody() {
 
     const linksList = document.createElement("div");
     linksList.classList.add("bytm-plugin-list-row-links-list");
-    linksList.tabIndex = 0;
     leftEl.appendChild(linksList);
 
     let linkElCreated = false;

+ 3 - 3
src/features/lyrics.ts

@@ -1,5 +1,5 @@
 import { fetchAdvanced } from "@sv443-network/userutils";
-import { error, getResourceUrl, info, log, warn, t, tp, getCurrentMediaType, constructUrl, onInteraction, openInTab } from "../utils/index.js";
+import { error, getResourceUrl, info, log, warn, t, tp, getCurrentMediaType, constructUrl, onInteraction, openInTab, LyricsError } from "../utils/index.js";
 import { emitInterface } from "../interface.js";
 import { mode, scriptInfo } from "../constants.js";
 import { getFeature } from "../config.js";
@@ -243,14 +243,14 @@ export async function fetchLyricsUrls(artist: string, song: string): Promise<Omi
       return undefined;
     }
     else if(fetchRes.status < 200 || fetchRes.status >= 300) {
-      getFeature("errorOnLyricsNotFound") && error(`Couldn't fetch lyrics URLs from geniURL - status: ${fetchRes.status} - response: ${(await fetchRes.json()).message ?? await fetchRes.text() ?? "(none)"}`);
+      getFeature("errorOnLyricsNotFound") && error(new LyricsError(`Couldn't fetch lyrics URLs from geniURL - status: ${fetchRes.status} - response: ${(await fetchRes.json()).message ?? await fetchRes.text() ?? "(none)"}`));
       return undefined;
     }
 
     const result = await fetchRes.json();
 
     if(typeof result === "object" && result.error || !result || !result.all) {
-      getFeature("errorOnLyricsNotFound") && error("Couldn't fetch lyrics URL:", result.message);
+      getFeature("errorOnLyricsNotFound") && error(new LyricsError(`Couldn't fetch lyrics URLs from geniURL: ${result.message}`));
       return undefined;
     }
 

+ 4 - 4
src/interface.ts

@@ -1,7 +1,7 @@
 import * as UserUtils from "@sv443-network/userutils";
 import * as compareVersions from "compare-versions";
 import { mode, branch, host, buildNumber, compressionFormat, scriptInfo } from "./constants.js";
-import { getDomain, waitVideoElementReady, getResourceUrl, getSessionId, getVideoTime, log, setLocale, getLocale, hasKey, hasKeyFor, t, tp, type TrLocale, info, error, onInteraction, getThumbnailUrl, getBestThumbnailUrl, fetchVideoVotes, setInnerHtml, getCurrentMediaType, tl, tlp } from "./utils/index.js";
+import { getDomain, waitVideoElementReady, getResourceUrl, getSessionId, getVideoTime, log, setLocale, getLocale, hasKey, hasKeyFor, t, tp, type TrLocale, info, error, onInteraction, getThumbnailUrl, getBestThumbnailUrl, fetchVideoVotes, setInnerHtml, getCurrentMediaType, tl, tlp, PluginError } from "./utils/index.js";
 import { addSelectorListener } from "./observers.js";
 import { getFeatures, setFeatures } from "./config.js";
 import { autoLikeStore, featInfo, fetchLyricsUrlTop, getLyricsCacheEntry, sanitizeArtists, sanitizeSong } from "./features/index.js";
@@ -234,11 +234,11 @@ export function initPlugins() {
   const registerPlugin = (def: PluginDef): PluginRegisterResult => {
     try {
       if(registeredPlugins.has(getPluginKey(def)))
-        throw new Error(`Failed to register plugin '${getPluginKey(def)}': Plugin with the same name and namespace is already registered`);
+        throw new PluginError(`Failed to register plugin '${getPluginKey(def)}': Plugin with the same name and namespace is already registered`);
 
       const validationErrors = validatePluginDef(def);
       if(validationErrors)
-        throw new Error(`Failed to register plugin${def?.plugin?.name ? ` '${def?.plugin?.name}'` : ""} with invalid definition:\n- ${validationErrors.join("\n- ")}`);
+        throw new PluginError(`Failed to register plugin${def?.plugin?.name ? ` '${def?.plugin?.name}'` : ""} with invalid definition:\n- ${validationErrors.join("\n- ")}`);
 
       const events = new NanoEmitter<PluginEventMap>({ publicEmit: true });
       const token = randomId(32, 36, true);
@@ -259,7 +259,7 @@ export function initPlugins() {
       };
     }
     catch(err) {
-      error(`Failed to register plugin '${getPluginKey(def)}':`, err);
+      error(`Failed to register plugin '${getPluginKey(def)}':`, err instanceof PluginError ? err : new PluginError(String(err)));
       throw err;
     }
   };

+ 14 - 0
src/utils/errors.ts

@@ -0,0 +1,14 @@
+
+export class LyricsError extends Error {
+  constructor(message: string) {
+    super(message);
+    this.name = "LyricsError";
+  }
+}
+
+export class PluginError extends Error {
+  constructor(message: string) {
+    super(message);
+    this.name = "PluginError";
+  }
+}

+ 1 - 0
src/utils/index.ts

@@ -1,4 +1,5 @@
 export * from "./dom.js";
+export * from "./errors.js";
 export * from "./input.js";
 export * from "./logging.js";
 export * from "./misc.js";