From 31442a144ba3aa4c3f87ef77846747555e6162b3 Mon Sep 17 00:00:00 2001 From: Lars Berger Date: Sat, 17 Feb 2024 15:49:07 +0800 Subject: [PATCH] feat: format errors during template parsing --- packages/client-api/src/init-element.ts | 13 ++++--- packages/client-api/src/init-window.ts | 2 +- .../user-config/get-parsed-element-config.ts | 22 ++++++------ .../src/user-config/get-user-config.ts | 5 ++- .../src/user-config/parse-with-schema.ts | 2 +- .../user-config/shared/format-config-error.ts | 34 ------------------- .../src/user-config/shared/index.ts | 2 -- .../shared/template-property-error.ts | 17 ---------- 8 files changed, 24 insertions(+), 73 deletions(-) delete mode 100644 packages/client-api/src/user-config/shared/format-config-error.ts delete mode 100644 packages/client-api/src/user-config/shared/template-property-error.ts diff --git a/packages/client-api/src/init-element.ts b/packages/client-api/src/init-element.ts index 4fdeac6b..14e5b575 100644 --- a/packages/client-api/src/init-element.ts +++ b/packages/client-api/src/init-element.ts @@ -106,12 +106,15 @@ export async function initElement( return elementContext as ElementContext; } catch (err) { - logger.error('Failed to initialize element:', err); + // Let error immediately bubble up if element is a window. + if (args.type !== ElementType.WINDOW) { + logger.error('Failed to initialize element:', err); - messageDialog((err as Error)?.message ?? 'Unknown reason.', { - title: 'Failed to initialize element!', - type: 'error', - }); + await messageDialog((err as Error)?.message ?? 'Unknown reason.', { + title: 'Failed to initialize element!', + type: 'error', + }); + } throw err; } diff --git a/packages/client-api/src/init-window.ts b/packages/client-api/src/init-window.ts index f472566c..56435505 100644 --- a/packages/client-api/src/init-window.ts +++ b/packages/client-api/src/init-window.ts @@ -112,7 +112,7 @@ export async function initWindowAsync(): Promise { } catch (err) { logger.error('Failed to initialize window:', err); - messageDialog((err as Error)?.message ?? 'Unknown reason.', { + await messageDialog((err as Error)?.message ?? 'Unknown reason.', { title: 'Failed to initialize window!', type: 'error', }); diff --git a/packages/client-api/src/user-config/get-parsed-element-config.ts b/packages/client-api/src/user-config/get-parsed-element-config.ts index 602b1de8..3da156bc 100644 --- a/packages/client-api/src/user-config/get-parsed-element-config.ts +++ b/packages/client-api/src/user-config/get-parsed-element-config.ts @@ -8,7 +8,6 @@ import { GroupConfigSchemaP1, TemplateConfigSchema, WindowConfigSchemaP1, - TemplatePropertyError, parseWithSchema, } from '~/user-config'; import type { PickPartial } from '~/utils'; @@ -52,15 +51,18 @@ export function getParsedElementConfig( return [key, rendered]; } catch (err) { - // Re-throw error as `TemplatePropertyError`. - throw err instanceof TemplateError - ? new TemplatePropertyError( - err.message, - key, - value, - err.templateIndex, - ) - : err; + if (!(err instanceof TemplateError)) { + throw err; + } + + const { message, templateIndex } = err; + + throw new Error( + `Property '${key}' in config isn't valid.\n\n` + + 'Syntax error at:\n' + + `...${value.slice(templateIndex - 30, templateIndex)} << \n\n` + + `⚠️ ${message}`, + ); } }); diff --git a/packages/client-api/src/user-config/get-user-config.ts b/packages/client-api/src/user-config/get-user-config.ts index 214dd53a..49cec245 100644 --- a/packages/client-api/src/user-config/get-user-config.ts +++ b/packages/client-api/src/user-config/get-user-config.ts @@ -1,7 +1,6 @@ import { createSignal } from 'solid-js'; -import { YAMLParseError, parse } from 'yaml'; +import { parse } from 'yaml'; -import { formatConfigError } from './shared'; import { createLogger } from '~/utils'; import { readConfigFile } from '~/desktop'; @@ -30,7 +29,7 @@ export async function getUserConfig() { return configObj; } catch (err) { throw new Error( - `Problem reading config file: ${(err as Error).message}`, + `Problem reading config file. ${(err as Error).message}`, ); } } diff --git a/packages/client-api/src/user-config/parse-with-schema.ts b/packages/client-api/src/user-config/parse-with-schema.ts index 368f1252..b9f5e99c 100644 --- a/packages/client-api/src/user-config/parse-with-schema.ts +++ b/packages/client-api/src/user-config/parse-with-schema.ts @@ -21,6 +21,6 @@ export function parseWithSchema( ); } - throw new Error('Failed to parse config.'); + throw new Error(`Failed to parse config. ${(err as Error).message}`); } } diff --git a/packages/client-api/src/user-config/shared/format-config-error.ts b/packages/client-api/src/user-config/shared/format-config-error.ts deleted file mode 100644 index c78f75c1..00000000 --- a/packages/client-api/src/user-config/shared/format-config-error.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { ZodError } from 'zod'; - -import { TemplatePropertyError } from './template-property-error'; - -export function formatConfigError(err: unknown) { - if (!(err instanceof Error)) { - return new Error('Problem reading config file.'); - } - - if (err instanceof ZodError && err.errors.length) { - const [firstError] = err.errors; - const { message, path } = firstError!; - const fullPath = path.join('.'); - - return new Error( - `Property '${fullPath}' in config isn't valid.\n` + `⚠️ ${message}`, - ); - } - - if (err instanceof TemplatePropertyError) { - const { message, path, template, templateIndex } = err; - - return new Error( - `Property '${path}' in config isn't valid.\n\n` + - 'Syntax error at:\n' + - `...${template.slice(templateIndex - 30, templateIndex)} << \n\n` + - `⚠️ ${message}`, - ); - } - - return new Error( - `Problem reading config file: ${(err as Error).message}.`, - ); -} diff --git a/packages/client-api/src/user-config/shared/index.ts b/packages/client-api/src/user-config/shared/index.ts index 6658db36..7bb6ea76 100644 --- a/packages/client-api/src/user-config/shared/index.ts +++ b/packages/client-api/src/user-config/shared/index.ts @@ -1,5 +1,3 @@ export * from './boolean-like.model'; -export * from './format-config-error'; export * from './get-child-configs'; -export * from './template-property-error'; export * from './with-dynamic-key'; diff --git a/packages/client-api/src/user-config/shared/template-property-error.ts b/packages/client-api/src/user-config/shared/template-property-error.ts deleted file mode 100644 index bc7e2a87..00000000 --- a/packages/client-api/src/user-config/shared/template-property-error.ts +++ /dev/null @@ -1,17 +0,0 @@ -export class TemplatePropertyError extends Error { - public path: string; - public template: string; - public templateIndex: number; - - constructor( - message: string, - path: string, - template: string, - templateIndex: number, - ) { - super(message); - this.path = path; - this.template = template; - this.templateIndex = templateIndex; - } -}