Home / File/ utils.ts — astro Source File

utils.ts — astro Source File

Architecture documentation for utils.ts, a typescript file in the astro codebase. 14 imports, 0 dependents.

File typescript CoreAstro CoreMiddleware 14 imports 7 functions

Entity Profile

Dependency Diagram

graph LR
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51["utils.ts"]
  10d4e39f_edb6_3e34_aa93_ae1211e7da05["../types/public/internal.js"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> 10d4e39f_edb6_3e34_aa93_ae1211e7da05
  7e4494c0_5563_4329_1bff_a84be66e1bc2["../core/path.js"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> 7e4494c0_5563_4329_1bff_a84be66e1bc2
  a370a45c_02f1_30de_445d_47ee08d095a2["../core/viteUtils.js"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> a370a45c_02f1_30de_445d_47ee08d095a2
  dd6187d6_53c4_ce90_a1d1_3a0b5e7e7d3f["../../core/errors/errors.js"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> dd6187d6_53c4_ce90_a1d1_3a0b5e7e7d3f
  ef8a1e3f_e350_75a6_b92d_62a8566d8db9["../core/errors/index.js"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> ef8a1e3f_e350_75a6_b92d_62a8566d8db9
  f87041c0_adb2_b5e5_4096_6b2c67c52a62["./printer.js"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> f87041c0_adb2_b5e5_4096_6b2c67c52a62
  91e2aae7_9271_3bbb_4d0e_63fd50e547d0["./utils.js"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> 91e2aae7_9271_3bbb_4d0e_63fd50e547d0
  e16a223b_37f3_6b25_1ee1_2b7bcb9d9415["node:fs"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> e16a223b_37f3_6b25_1ee1_2b7bcb9d9415
  c52a5f83_66e3_37d7_9ebb_767f7129bc62["node:path"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> c52a5f83_66e3_37d7_9ebb_767f7129bc62
  d9a92db9_c95e_9165_13ac_24b3d859d946["node:url"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> d9a92db9_c95e_9165_13ac_24b3d859d946
  b4a76fc8_3591_85b4_7b57_55ab21d1030d["node:util"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> b4a76fc8_3591_85b4_7b57_55ab21d1030d
  fc634da7_3e72_de83_7395_a46311dd837e["html-escaper"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> fc634da7_3e72_de83_7395_a46311dd837e
  10250468_0e83_bd69_43e9_3bcef2294a91["piccolore"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> 10250468_0e83_bd69_43e9_3bcef2294a91
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style 6382d0b9_8ae6_1f6a_25b6_41a3e8e6de51 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import * as fs from 'node:fs';
import { isAbsolute, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { stripVTControlCharacters } from 'node:util';
import { escape } from 'html-escaper';
import colors from 'piccolore';
import type { ESBuildTransformResult } from 'vite';
import type { SSRError } from '../../../types/public/internal.js';
import { removeLeadingForwardSlashWindows } from '../../path.js';
import { normalizePath } from '../../viteUtils.js';
import { AggregateError, type ErrorWithMetadata } from '../errors.js';
import { AstroErrorData } from '../index.js';
import { codeFrame } from '../printer.js';
import { normalizeLF } from '../utils.js';

type EsbuildMessage = ESBuildTransformResult['warnings'][number];

/**
 * Takes any error-like object and returns a standardized Error + metadata object.
 * Useful for consistent reporting regardless of where the error surfaced from.
 */
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) {
// ... (230 more lines)

Domain

Subdomains

Dependencies

  • ../../core/errors/errors.js
  • ../core/errors/index.js
  • ../core/path.js
  • ../core/viteUtils.js
  • ../types/public/internal.js
  • ./printer.js
  • ./utils.js
  • html-escaper
  • node:fs
  • node:path
  • node:url
  • node:util
  • piccolore
  • vite

Frequently Asked Questions

What does utils.ts do?
utils.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, CoreMiddleware subdomain.
What functions are defined in utils.ts?
utils.ts defines 7 function(s): cleanErrorStack, collectErrorMetadata, collectInfoFromStacktrace, generateHint, getDocsForError, isAllowedUrl, renderErrorMarkdown.
What does utils.ts depend on?
utils.ts imports 14 module(s): ../../core/errors/errors.js, ../core/errors/index.js, ../core/path.js, ../core/viteUtils.js, ../types/public/internal.js, ./printer.js, ./utils.js, html-escaper, and 6 more.
Where is utils.ts in the architecture?
utils.ts is located at packages/astro/src/core/errors/dev/utils.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/astro/src/core/errors/dev).

Analyze Your Own Codebase

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

Try Supermodel Free