Home / Function/ enhanceViteSSRError() — astro Function Reference

enhanceViteSSRError() — astro Function Reference

Architecture documentation for the enhanceViteSSRError() function in vite.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  b8572813_5bc2_3049_256d_55c0ac719399["enhanceViteSSRError()"]
  35a0af3d_ab30_11cd_b3ee_bae2c089e53b["vite.ts"]
  b8572813_5bc2_3049_256d_55c0ac719399 -->|defined in| 35a0af3d_ab30_11cd_b3ee_bae2c089e53b
  style b8572813_5bc2_3049_256d_55c0ac719399 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/errors/dev/vite.ts lines 13–82

export function enhanceViteSSRError({
	error,
	filePath,
	loader,
	renderers,
}: {
	error: unknown;
	filePath?: URL;
	loader?: ModuleLoader;
	renderers?: SSRLoadedRenderer[];
}): Error {
	// NOTE: We don't know where the error that's coming here comes from, so we need to be defensive regarding what we do
	// to it to make sure we keep as much information as possible. It's very possible that we receive an error that does not
	// follow any kind of standard formats (ex: a number, a string etc)
	let safeError = createSafeError(error) as ErrorWithMetadata;

	// Vite will give you better stacktraces, using sourcemaps.
	if (loader) {
		try {
			loader.fixStacktrace(safeError as Error);
		} catch {}
	}

	if (filePath) {
		const path = fileURLToPath(filePath);
		const content = fs.readFileSync(path).toString();
		const lns = content.split('\n');

		// Vite has a fairly generic error message when it fails to load a module, let's try to enhance it a bit
		// https://github.com/vitejs/vite/blob/ee7c28a46a6563d54b828af42570c55f16b15d2c/packages/vite/src/node/ssr/ssrModuleLoader.ts#L91
		let importName: string | undefined;
		if ((importName = /Failed to load url (.*?) \(resolved id:/.exec(safeError.message)?.[1])) {
			safeError.title = FailedToLoadModuleSSR.title;
			safeError.name = 'FailedToLoadModuleSSR';
			safeError.message = FailedToLoadModuleSSR.message(importName);
			safeError.hint = FailedToLoadModuleSSR.hint;
			const line = lns.findIndex((ln) => ln.includes(importName!));

			if (line !== -1) {
				const column = lns[line]?.indexOf(importName);

				safeError.loc = {
					file: path,
					line: line + 1,
					column,
				};
			}
		}

		const fileId = safeError.id ?? safeError.loc?.file;

		// Vite throws a syntax error trying to parse MDX without a plugin.
		// Suggest installing the MDX integration if none is found.
		if (
			fileId &&
			!renderers?.find((r) => r.name === '@astrojs/mdx') &&
			safeError.message.includes('Syntax error') &&
			/.mdx$/.test(fileId)
		) {
			safeError = new AstroError({
				...MdxIntegrationMissingError,
				message: MdxIntegrationMissingError.message(JSON.stringify(fileId)),
				location: safeError.loc,
				stack: safeError.stack,
			}) as ErrorWithMetadata;
		}
	}

	return safeError;
}

Domain

Subdomains

Frequently Asked Questions

What does enhanceViteSSRError() do?
enhanceViteSSRError() is a function in the astro codebase, defined in packages/astro/src/core/errors/dev/vite.ts.
Where is enhanceViteSSRError() defined?
enhanceViteSSRError() is defined in packages/astro/src/core/errors/dev/vite.ts at line 13.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free