Home / Function/ collectErrorMetadata() — astro Function Reference

collectErrorMetadata() — astro Function Reference

Architecture documentation for the collectErrorMetadata() function in utils.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  8d0d4acd_8384_a2c6_5b17_55868e5cf72d["collectErrorMetadata()"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51["utils.ts"]
  8d0d4acd_8384_a2c6_5b17_55868e5cf72d -->|defined in| 6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51
  e8f7378a_0237_7b9f_5d0b_f10ec6dbb971["collectInfoFromStacktrace()"]
  8d0d4acd_8384_a2c6_5b17_55868e5cf72d -->|calls| e8f7378a_0237_7b9f_5d0b_f10ec6dbb971
  bbf62356_656f_e004_5e19_86d1d5889e42["generateHint()"]
  8d0d4acd_8384_a2c6_5b17_55868e5cf72d -->|calls| bbf62356_656f_e004_5e19_86d1d5889e42
  style 8d0d4acd_8384_a2c6_5b17_55868e5cf72d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/errors/dev/utils.ts lines 22–138

export function collectErrorMetadata(e: any, rootFolder?: URL): ErrorWithMetadata {
	const err =
		AggregateError.is(e) || Array.isArray(e.errors) ? (e.errors as SSRError[]) : [e as SSRError];

	err.forEach((error) => {
		if (e.stack) {
			const stackInfo = collectInfoFromStacktrace(e);
			try {
				error.stack = stripVTControlCharacters(stackInfo.stack);
			} catch {}
			error.loc = stackInfo.loc;
			error.plugin = stackInfo.plugin;
			error.pluginCode = stackInfo.pluginCode;
		}

		// Make sure the file location is absolute, otherwise:
		// - It won't be clickable in the terminal
		// - We'll fail to show the file's content in the browser
		// - We'll fail to show the code frame in the terminal
		// - The "Open in Editor" button won't work

		// Normalize the paths so that we can correctly detect if it's absolute on any platform
		const normalizedFile = normalizePath(error.loc?.file || '');
		const normalizedRootFolder = removeLeadingForwardSlashWindows(rootFolder?.pathname || '');

		if (
			error.loc?.file &&
			rootFolder &&
			(!normalizedFile?.startsWith(normalizedRootFolder) || !isAbsolute(normalizedFile))
		) {
			error.loc.file = join(fileURLToPath(rootFolder), error.loc.file);
		}

		// If we don't have a frame, but we have a location let's try making up a frame for it
		if (error.loc && (!error.frame || !error.fullCode)) {
			try {
				const fileContents = fs.readFileSync(error.loc.file!, 'utf8');

				if (!error.frame) {
					const frame = codeFrame(fileContents, error.loc);
					error.frame = stripVTControlCharacters(frame);
				}

				if (!error.fullCode) {
					error.fullCode = fileContents;
				}
			} catch {}
		}

		// Generic error (probably from Vite, and already formatted)
		error.hint = generateHint(e);

		// Strip ANSI for `message` property. Note that ESBuild errors may not have the property,
		// but it will be handled and added below, which is already ANSI-free
		if (error.message) {
			try {
				error.message = stripVTControlCharacters(error.message);
			} catch {
				// Setting `error.message` can fail here if the message is read-only, which for the vast majority of cases will never happen, however some somewhat obscure cases can cause this to happen.
			}
		}
	});

	// If we received an array of errors and it's not from us, it's most likely from ESBuild, try to extract info for Vite to display
	// NOTE: We still need to be defensive here, because it might not necessarily be from ESBuild, it's just fairly likely.
	if (!AggregateError.is(e) && Array.isArray(e.errors)) {
		(e.errors as EsbuildMessage[]).forEach((buildError, i) => {
			const { location, pluginName, text } = buildError;

			// ESBuild can give us a slightly better error message than the one in the error, so let's use it
			if (text) {
				try {
					err[i].message = text;
				} catch {}
			}

			if (location) {
				err[i].loc = { file: location.file, line: location.line, column: location.column };
				err[i].id = err[0].id || location?.file;
			}

Domain

Subdomains

Frequently Asked Questions

What does collectErrorMetadata() do?
collectErrorMetadata() is a function in the astro codebase, defined in packages/astro/src/core/errors/dev/utils.ts.
Where is collectErrorMetadata() defined?
collectErrorMetadata() is defined in packages/astro/src/core/errors/dev/utils.ts at line 22.
What does collectErrorMetadata() call?
collectErrorMetadata() calls 2 function(s): collectInfoFromStacktrace, generateHint.

Analyze Your Own Codebase

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

Try Supermodel Free