Home / Function/ errorMap() — astro Function Reference

errorMap() — astro Function Reference

Architecture documentation for the errorMap() function in error-map.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  f0e6d2c1_39ba_572e_b75c_8fa03ac80980["errorMap()"]
  f426a7f0_b6be_5991_9420_b6012c0b085e["error-map.ts"]
  f0e6d2c1_39ba_572e_b75c_8fa03ac80980 -->|defined in| f426a7f0_b6be_5991_9420_b6012c0b085e
  38d416f5_3f16_8020_d0c5_d03be6490763["flattenErrorPath()"]
  f0e6d2c1_39ba_572e_b75c_8fa03ac80980 -->|calls| 38d416f5_3f16_8020_d0c5_d03be6490763
  49d42275_9c14_d86e_71f3_d3b289195a3a["prefix()"]
  f0e6d2c1_39ba_572e_b75c_8fa03ac80980 -->|calls| 49d42275_9c14_d86e_71f3_d3b289195a3a
  b10081c1_9df6_e153_bd75_e6bbba0c2439["getTypeOrLiteralMsg()"]
  f0e6d2c1_39ba_572e_b75c_8fa03ac80980 -->|calls| b10081c1_9df6_e153_bd75_e6bbba0c2439
  31caee36_8f64_3f96_7288_6ddadd726f5d["stringify()"]
  f0e6d2c1_39ba_572e_b75c_8fa03ac80980 -->|calls| 31caee36_8f64_3f96_7288_6ddadd726f5d
  style f0e6d2c1_39ba_572e_b75c_8fa03ac80980 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/db/src/core/integration/error-map.ts lines 10–102

export const errorMap: $ZodErrorMap = (issue) => {
	const baseErrorPath = flattenErrorPath(issue.path ?? []);
	if (issue.code === 'invalid_union') {
		// Optimization: Combine type and literal errors for keys that are common across ALL union types
		// Ex. a union between `{ key: z.literal('tutorial') }` and `{ key: z.literal('blog') }` will
		// raise a single error when `key` does not match:
		// > Did not match union.
		// > key: Expected `'tutorial' | 'blog'`, received 'foo'
		let typeOrLiteralErrByPath = new Map<string, TypeOrLiteralErrByPathEntry>();
		for (const unionError of issue.errors.flat()) {
			if (unionError.code === 'invalid_type') {
				const flattenedErrorPath = flattenErrorPath(unionError.path);
				if (typeOrLiteralErrByPath.has(flattenedErrorPath)) {
					typeOrLiteralErrByPath.get(flattenedErrorPath)!.expected.push(unionError.expected);
				} else {
					typeOrLiteralErrByPath.set(flattenedErrorPath, {
						code: unionError.code,
						received: (unionError as any).received,
						expected: [unionError.expected],
						message: unionError.message,
					});
				}
			}
		}
		const messages: string[] = [prefix(baseErrorPath, 'Did not match union.')];
		const details: string[] = [...typeOrLiteralErrByPath.entries()]
			// If type or literal error isn't common to ALL union types,
			// filter it out. Can lead to confusing noise.
			.filter(([, error]) => error.expected.length === issue.errors.flat().length)
			.map(([key, error]) =>
				key === baseErrorPath
					? // Avoid printing the key again if it's a base error
						`> ${getTypeOrLiteralMsg(error)}`
					: `> ${prefix(key, getTypeOrLiteralMsg(error))}`,
			);

		if (details.length === 0) {
			const expectedShapes: string[] = [];
			for (const unionErrors of issue.errors) {
				const expectedShape: string[] = [];
				for (const _issue of unionErrors) {
					// If the issue is a nested union error, show the associated error message instead of the
					// base error message.
					if (_issue.code === 'invalid_union') {
						return errorMap(_issue as any);
					}
					const relativePath = flattenErrorPath(_issue.path)
						.replace(baseErrorPath, '')
						.replace(leadingPeriod, '');
					if ('expected' in _issue && typeof _issue.expected === 'string') {
						expectedShape.push(
							relativePath ? `${relativePath}: ${_issue.expected}` : _issue.expected,
						);
					} else if ('values' in _issue) {
						expectedShape.push(
							..._issue.values.filter((v) => typeof v === 'string').map((v) => `"${v}"`),
						);
					} else if (relativePath) {
						expectedShape.push(relativePath);
					}
				}
				if (expectedShape.length === 1 && !expectedShape[0]?.includes(':')) {
					// In this case the expected shape is not an object, but probably a literal type, e.g. `['string']`.
					expectedShapes.push(expectedShape.join(''));
				} else if (expectedShape.length > 0) {
					expectedShapes.push(`{ ${expectedShape.join('; ')} }`);
				}
			}
			if (expectedShapes.length) {
				details.push('> Expected type `' + expectedShapes.join(' | ') + '`');
				details.push('> Received `' + stringify(issue.input) + '`');
			}
		}

		return {
			message: messages.concat(details).join('\n'),
		};
	} else if (issue.code === 'invalid_type') {
		return {
			message: prefix(
				baseErrorPath,

Domain

Subdomains

Frequently Asked Questions

What does errorMap() do?
errorMap() is a function in the astro codebase, defined in packages/db/src/core/integration/error-map.ts.
Where is errorMap() defined?
errorMap() is defined in packages/db/src/core/integration/error-map.ts at line 10.
What does errorMap() call?
errorMap() calls 4 function(s): flattenErrorPath, getTypeOrLiteralMsg, prefix, stringify.

Analyze Your Own Codebase

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

Try Supermodel Free