generated from withstudiocms/project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error handling and refactor imports in markdown-remark integration
- Loading branch information
1 parent
9f5b574
commit ac60676
Showing
7 changed files
with
95 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { AstroError } from 'astro/errors'; | ||
|
||
export class MarkdownRemarkError extends AstroError { | ||
name = 'StudioCMS Markdown Remark Error'; | ||
} | ||
|
||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
export function prefixError(err: any, prefix: string): any { | ||
// If the error is an object with a `message` property, attempt to prefix the message | ||
if (err?.message) { | ||
try { | ||
err.message = `${prefix}:\n${err.message}`; | ||
return err; | ||
} catch { | ||
// Any errors here are ok, there's fallback code below | ||
} | ||
} | ||
|
||
// If that failed, create a new error with the desired message and attempt to keep the stack | ||
const wrappedError = new Error(`${prefix}${err ? `: ${err}` : ''}`); | ||
try { | ||
wrappedError.stack = err.stack; | ||
wrappedError.cause = err; | ||
} catch { | ||
// It's ok if we could not set the stack or cause - the message is the most important part | ||
} | ||
|
||
return wrappedError; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { MarkdownRemarkError, prefixError } from './errors.js'; | ||
|
||
/** | ||
* Imports components by their keys from the 'studiocms:markdown-remark/user-components' module. | ||
* | ||
* @param keys - An array of strings representing the keys of the components to import. | ||
* @returns A promise that resolves to an object containing the imported components. | ||
* @throws {MarkdownRemarkError} If any component fails to import, an error is thrown with a prefixed message. | ||
*/ | ||
export async function importComponentsKeys() { | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
const predefinedComponents: Record<string, any> = {}; | ||
|
||
const mod = import('studiocms:markdown-remark/user-components').catch((e) => { | ||
const newErr = prefixError(e, 'Failed to import user components'); | ||
console.error(newErr); | ||
throw new MarkdownRemarkError(newErr.message, newErr.stack); | ||
}); | ||
|
||
const componentKeys = (await mod).componentKeys; | ||
|
||
for (const key of componentKeys) { | ||
try { | ||
predefinedComponents[key.toLowerCase()] = (await mod)[key.toLowerCase()]; | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
const newErr = prefixError(e, `Failed to import component "${key}"`); | ||
console.error(newErr); | ||
throw new MarkdownRemarkError(newErr.message, newErr.stack); | ||
} | ||
const newErr = prefixError(new Error('Unknown error'), `Failed to import component "${key}"`); | ||
console.error(newErr); | ||
throw new MarkdownRemarkError(newErr.message, newErr.stack); | ||
} | ||
} | ||
|
||
return predefinedComponents; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters