From ff4efb07d1af997e22cd5a4398156bd973ec7b55 Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Sun, 10 Oct 2021 13:44:48 -0700 Subject: [PATCH] Slightly improve error path readability. --- src/card.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/card.ts b/src/card.ts index 392a2c98..12237ba8 100644 --- a/src/card.ts +++ b/src/card.ts @@ -250,6 +250,21 @@ export class FrigateCard extends LitElement { return null; } + protected _getParseErrorPathString(path: (string|number)[]): string { + let out = ''; + for (let i = 0; i < path.length; i++) { + const item = path[i]; + if (typeof item == 'number') { + out += '[' + item + ']'; + } else if (out) { + out += " -> " + item; + } else { + out = item; + } + } + return out; + } + // Set the object configuration. public setConfig(inputConfig: FrigateCardConfig): void { if (!inputConfig) { @@ -258,8 +273,11 @@ export class FrigateCard extends LitElement { const parseResult = frigateCardConfigSchema.safeParse(inputConfig); if (!parseResult.success) { - const keys = getParseErrorKeys(parseResult.error); - throw new Error(localize('error.invalid_configuration') + ': ' + keys.join(', ')); + let hint = ''; + if (parseResult.error && parseResult.error.issues) { + hint = this._getParseErrorPathString(parseResult.error.issues[0].path); + } + throw new Error(localize('error.invalid_configuration') + (hint ? `: ${hint}` : '')); } const config = parseResult.data;