Skip to content

Commit

Permalink
feat: format errors during template parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Feb 17, 2024
1 parent 7c2edd1 commit 31442a1
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 73 deletions.
13 changes: 8 additions & 5 deletions packages/client-api/src/init-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client-api/src/init-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export async function initWindowAsync(): Promise<WindowContext> {
} 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',
});
Expand Down
22 changes: 12 additions & 10 deletions packages/client-api/src/user-config/get-parsed-element-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
GroupConfigSchemaP1,
TemplateConfigSchema,
WindowConfigSchemaP1,
TemplatePropertyError,
parseWithSchema,
} from '~/user-config';
import type { PickPartial } from '~/utils';
Expand Down Expand Up @@ -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}`,
);
}
});

Expand Down
5 changes: 2 additions & 3 deletions packages/client-api/src/user-config/get-user-config.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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}`,
);
}
}
2 changes: 1 addition & 1 deletion packages/client-api/src/user-config/parse-with-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export function parseWithSchema<T extends z.ZodType>(
);
}

throw new Error('Failed to parse config.');
throw new Error(`Failed to parse config. ${(err as Error).message}`);
}
}
34 changes: 0 additions & 34 deletions packages/client-api/src/user-config/shared/format-config-error.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/client-api/src/user-config/shared/index.ts
Original file line number Diff line number Diff line change
@@ -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';

This file was deleted.

0 comments on commit 31442a1

Please sign in to comment.