From 95f35bd3261db4ce636076921c4af03292645bae Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Tue, 23 Jun 2026 19:53:37 -0700 Subject: [PATCH] refactor: Accept unknown in errorToConsole to drop `as Error` casts (#2541) --- src/camera-manager/frigate/camera.ts | 2 +- src/camera-manager/utils/go2rtc/audio.ts | 2 +- src/card-controller/actions/actions-manager.ts | 2 +- src/card-controller/issues/state-manager.ts | 2 +- src/card-controller/media-player-manager.ts | 2 +- src/card-controller/view/item-manager.ts | 2 +- src/components-lib/gallery/controller.ts | 2 +- src/components-lib/media-filter-controller.ts | 2 +- src/components-lib/signed-url-controller.ts | 4 ++-- src/components-lib/timeline/source.ts | 2 +- src/components/elements.ts | 4 ++-- src/components/live/providers/jsmpeg.ts | 2 +- src/ha/registry/device/index.ts | 2 +- src/ha/registry/entity/index.ts | 4 ++-- src/ha/resolved-media.ts | 2 +- src/utils/basic.ts | 10 +++++----- src/utils/media-actions.ts | 6 +++--- src/utils/task.ts | 2 +- tests/utils/basic.test.ts | 5 +++++ 19 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/camera-manager/frigate/camera.ts b/src/camera-manager/frigate/camera.ts index 8777b682..2275e962 100644 --- a/src/camera-manager/frigate/camera.ts +++ b/src/camera-manager/frigate/camera.ts @@ -366,7 +366,7 @@ export class FrigateCamera extends Camera { cameraConfig.frigate.camera_name, ); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); return null; } diff --git a/src/camera-manager/utils/go2rtc/audio.ts b/src/camera-manager/utils/go2rtc/audio.ts index 94e3c947..0089e354 100644 --- a/src/camera-manager/utils/go2rtc/audio.ts +++ b/src/camera-manager/utils/go2rtc/audio.ts @@ -16,7 +16,7 @@ const getGo2RTCStreamMetadata = async ( timeoutSeconds, }); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); return null; } }; diff --git a/src/card-controller/actions/actions-manager.ts b/src/card-controller/actions/actions-manager.ts index 8ec48540..22492f53 100644 --- a/src/card-controller/actions/actions-manager.ts +++ b/src/card-controller/actions/actions-manager.ts @@ -176,7 +176,7 @@ export class ActionsManager implements ActionsExecutor { await actionSet.execute(this._api); forwardHaptic('success'); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); forwardHaptic('warning'); } this._actionsInFlight = this._actionsInFlight.filter((a) => a !== actionSet); diff --git a/src/card-controller/issues/state-manager.ts b/src/card-controller/issues/state-manager.ts index 37a299b9..4e400da5 100644 --- a/src/card-controller/issues/state-manager.ts +++ b/src/card-controller/issues/state-manager.ts @@ -37,7 +37,7 @@ export class IssueStateManager implements IssueReadOnlyState { } catch (e) { // Isolate one issue's detection failure so it cannot abort detection // for the rest; log so the cause is visible. - errorToConsole(e as Error); + errorToConsole(e); } this._logIfNew(issue); } diff --git a/src/card-controller/media-player-manager.ts b/src/card-controller/media-player-manager.ts index b535546f..5e40a4ea 100644 --- a/src/card-controller/media-player-manager.ts +++ b/src/card-controller/media-player-manager.ts @@ -73,7 +73,7 @@ export class MediaPlayerManager { // Failing to fetch media player information is not considered // sufficiently serious to block card startup -- it is just logged and we // move on. - errorToConsole(e as Error); + errorToConsole(e); } // Filter out entities that are marked as hidden (this information is not diff --git a/src/card-controller/view/item-manager.ts b/src/card-controller/view/item-manager.ts index 8452e12f..586a3868 100644 --- a/src/card-controller/view/item-manager.ts +++ b/src/card-controller/view/item-manager.ts @@ -103,7 +103,7 @@ export class ViewItemManager { try { url = await homeAssistantGetSignedURLIfNecessary(hass, endpoint); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); url = null; } diff --git a/src/components-lib/gallery/controller.ts b/src/components-lib/gallery/controller.ts index ced110f3..5d8d185c 100644 --- a/src/components-lib/gallery/controller.ts +++ b/src/components-lib/gallery/controller.ts @@ -111,7 +111,7 @@ export class GalleryController { useCache, }); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); return; } diff --git a/src/components-lib/media-filter-controller.ts b/src/components-lib/media-filter-controller.ts index 12c06b89..844ff704 100644 --- a/src/components-lib/media-filter-controller.ts +++ b/src/components-lib/media-filter-controller.ts @@ -400,7 +400,7 @@ export class MediaFilterController { try { metadata = await cameraManager.getMediaMetadata(); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); } if (!metadata) { return; diff --git a/src/components-lib/signed-url-controller.ts b/src/components-lib/signed-url-controller.ts index 9c9558fc..c6b0c844 100644 --- a/src/components-lib/signed-url-controller.ts +++ b/src/components-lib/signed-url-controller.ts @@ -191,7 +191,7 @@ export class SignedURLController implements ReactiveController { }, ); } catch (e: unknown) { - errorToConsole(e as Error); + errorToConsole(e); return null; } } @@ -207,7 +207,7 @@ export class SignedURLController implements ReactiveController { PROXY_URL_SIGN_EXPIRY_SECONDS, ); } catch (e: unknown) { - errorToConsole(e as Error); + errorToConsole(e); return null; } } diff --git a/src/components-lib/timeline/source.ts b/src/components-lib/timeline/source.ts index a1a88e8b..bd342683 100644 --- a/src/components-lib/timeline/source.ts +++ b/src/components-lib/timeline/source.ts @@ -248,7 +248,7 @@ export class TimelineDataSource { ...(this._showRecordings ? [this._refreshRecordings(window)] : []), ]); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); } } diff --git a/src/components/elements.ts b/src/components/elements.ts index 14262e43..06820109 100644 --- a/src/components/elements.ts +++ b/src/components/elements.ts @@ -125,7 +125,7 @@ export class AdvancedCameraCardElementsCore extends LitElement { try { element.setConfig(config); } catch (e) { - errorToConsole(e as Error, console.error); + errorToConsole(e, console.error); throw new AdvancedCameraCardError(localize('error.invalid_elements_config')); } return element; @@ -155,7 +155,7 @@ export class AdvancedCameraCardElementsCore extends LitElement { this._renderedElements = elements; this._root = this._createRoot(); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); fireAdvancedCameraCardEvent(this, 'issue:trigger', { key: 'config_error', error: new ElementsCreationError(elements), diff --git a/src/components/live/providers/jsmpeg.ts b/src/components/live/providers/jsmpeg.ts index 74609b31..760f345c 100644 --- a/src/components/live/providers/jsmpeg.ts +++ b/src/components/live/providers/jsmpeg.ts @@ -199,7 +199,7 @@ export class AdvancedCameraCardLiveJSMPEG extends LitElement implements MediaPla JSMPEG_URL_SIGN_EXPIRY_SECONDS, ); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); } const address = response ? convertHTTPAdressToWebsocket(response) : null; diff --git a/src/ha/registry/device/index.ts b/src/ha/registry/device/index.ts index 5d9fe5b6..5ba1198b 100644 --- a/src/ha/registry/device/index.ts +++ b/src/ha/registry/device/index.ts @@ -40,7 +40,7 @@ export class DeviceRegistryManager { type: 'config/device_registry/list', }); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); return; } deviceList.forEach((device) => { diff --git a/src/ha/registry/entity/index.ts b/src/ha/registry/entity/index.ts index cef8e72e..9b869f7b 100644 --- a/src/ha/registry/entity/index.ts +++ b/src/ha/registry/entity/index.ts @@ -35,7 +35,7 @@ export class EntityRegistryManagerLive implements EntityRegistryManager { entity_id: entityID, }); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); return null; } this._cache.set(entity.entity_id, entity); @@ -79,7 +79,7 @@ export class EntityRegistryManagerLive implements EntityRegistryManager { type: 'config/entity_registry/list', }); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); return; } entityList.forEach((entity) => { diff --git a/src/ha/resolved-media.ts b/src/ha/resolved-media.ts index 6b0e93ef..7a5ebb80 100644 --- a/src/ha/resolved-media.ts +++ b/src/ha/resolved-media.ts @@ -40,7 +40,7 @@ export const resolveMedia = async ( try { resolvedMedia = await homeAssistantWSRequest(hass, resolvedMediaSchema, request); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); } if (cache && resolvedMedia) { cache.set(mediaContentID, resolvedMedia); diff --git a/src/utils/basic.ts b/src/utils/basic.ts index 290d6a84..24c10b77 100644 --- a/src/utils/basic.ts +++ b/src/utils/basic.ts @@ -76,13 +76,13 @@ export function contentsChanged( /** * Log an error as a warning to the console. - * @param e The Error-like object. + * @param e The caught error or error-like value. * @param func The Console func to call. */ -export function errorToConsole( - e: Error | { message: unknown } | string, - func: CallableFunction = console.warn, -): void { +export function errorToConsole(e: unknown, func: CallableFunction = console.warn): void { + if (!e) { + return; + } if (e instanceof AdvancedCameraCardError && e.context) { func(e, e.context); } else if (typeof e === 'object' && 'message' in e) { diff --git a/src/utils/media-actions.ts b/src/utils/media-actions.ts index f35f7254..d1f11a67 100644 --- a/src/utils/media-actions.ts +++ b/src/utils/media-actions.ts @@ -19,7 +19,7 @@ export async function toggleReviewed( try { await viewItemManager.reviewMedia(item, newState); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); return false; } item.setReviewed(newState); @@ -55,7 +55,7 @@ export async function toggleFavorite( try { await viewItemManager.favorite(item, newState); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); return false; } return true; @@ -72,7 +72,7 @@ export async function downloadMedia( try { await viewItemManager.download(item); } catch (e) { - errorToConsole(e as Error); + errorToConsole(e); return false; } return true; diff --git a/src/utils/task.ts b/src/utils/task.ts index b8cad154..b161a8dd 100644 --- a/src/utils/task.ts +++ b/src/utils/task.ts @@ -30,7 +30,7 @@ export const renderTask = ( pending: () => options?.inProgressFunc?.() ?? renderProgressIndicator(progressConfig), error: (e: unknown) => { - errorToConsole(e as Error); + errorToConsole(e); return options?.errorFunc?.(e as Error); }, complete: completeFunc, diff --git a/tests/utils/basic.test.ts b/tests/utils/basic.test.ts index 073fd011..6200ba9f 100644 --- a/tests/utils/basic.test.ts +++ b/tests/utils/basic.test.ts @@ -114,6 +114,11 @@ describe('errorToConsole', () => { errorToConsole('string message'); expect(spy).toHaveBeenCalledWith('string message'); }); + it('should do nothing given a falsy value', () => { + spy.mockClear(); + errorToConsole(null); + expect(spy).not.toHaveBeenCalled(); + }); }); // @vitest-environment jsdom