Skip to content

Commit

Permalink
feat: break down AstroErrorData in multiple objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Aug 3, 2023
1 parent 93ad8b9 commit ea426bd
Show file tree
Hide file tree
Showing 42 changed files with 1,230 additions and 1,158 deletions.
5 changes: 3 additions & 2 deletions packages/astro/components/Image.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
import { getImage, type LocalImageProps, type RemoteImageProps } from 'astro:assets';
import { AstroError, AstroErrorData } from '../dist/core/errors/index.js';
import { AstroError } from '../dist/core/errors/index.js';
import { ImageMissingAlt } from '../dist/core/errors/errors-data.js';
// The TypeScript diagnostic for JSX props uses the last member of the union to suggest props, so it would be better for
// LocalImageProps to be last. Unfortunately, when we do this the error messages that remote images get are complete nonsense
Expand All @@ -10,7 +11,7 @@ type Props = LocalImageProps | RemoteImageProps;
const props = Astro.props;
if (props.alt === undefined || props.alt === null) {
throw new AstroError(AstroErrorData.ImageMissingAlt);
throw new AstroError(ImageMissingAlt);
}
// As a convenience, allow width and height to be string with a number in them, to match HTML's native `img`.
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/assets/internal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AstroSettings } from '../@types/astro.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { AstroError } from '../core/errors/index.js';
import * as AstroErrorData from '../core/errors/errors-data.js';
import { isLocalService, type ImageService } from './services/service.js';
import type { GetImageResult, ImageMetadata, ImageTransform } from './types.js';

Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/assets/services/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AstroError, AstroErrorData } from '../../core/errors/index.js';
import { AstroError } from '../../core/errors/index.js';
import * as AstroErrorData from '../../core/errors/errors-data.js';
import { joinPaths } from '../../core/path.js';
import { VALID_SUPPORTED_FORMATS } from '../consts.js';
import { isESMImportedImage } from '../internal.js';
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { MarkdownHeading } from '@astrojs/markdown-remark';
import { ZodIssueCode, string as zodString } from 'zod';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { AstroError } from '../core/errors/index.js';
import * as AstroErrorData from '../core/errors/errors-data.js';
import { prependForwardSlash } from '../core/path.js';
import {
createComponent,
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { normalizePath, type ViteDevServer } from 'vite';
import type { AstroSettings, ContentEntryType } from '../@types/astro.js';
import { AstroErrorData } from '../core/errors/errors-data.js';
import * as AstroErrorData from '../core/errors/errors-data.js';
import { AstroError } from '../core/errors/errors.js';
import { info, warn, type LogOptions } from '../core/logger/core.js';
import { isRelativePath } from '../core/path.js';
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import type {
ImageInputFormat,
} from '../@types/astro.js';
import { VALID_INPUT_FORMATS } from '../assets/consts.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { AstroError } from '../core/errors/index.js';
import * as AstroErrorData from '../core/errors/errors-data.js';

import { formatYAMLException, isYAMLException } from '../core/errors/utils.js';
import { CONTENT_FLAGS, CONTENT_TYPES_FILE } from './consts.js';
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/content/vite-plugin-content-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
DataEntryModule,
DataEntryType,
} from '../@types/astro.js';
import { AstroErrorData } from '../core/errors/errors-data.js';
import * as AstroErrorData from '../core/errors/errors-data.js';
import { AstroError } from '../core/errors/errors.js';
import { escapeViteEnvReferences } from '../vite-plugin-utils/index.js';
import { CONTENT_FLAG, DATA_FLAG } from './consts.js';
Expand Down Expand Up @@ -371,6 +371,7 @@ function stringifyEntryData(data: Record<string, any>): string {
});
} else {
throw new AstroError({
name: 'ContentImportsPluginError',
message: 'Unexpected error processing content collection data.',
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
import pLimit from 'p-limit';
import type { Plugin } from 'vite';
import type { AstroSettings, ContentEntryType } from '../@types/astro.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { AstroError } from '../core/errors/index.js';
import * as AstroErrorData from '../core/errors/errors-data.js';
import { appendForwardSlash } from '../core/path.js';
import { rootRelativePath } from '../core/util.js';
import { VIRTUAL_MODULE_ID } from './consts.js';
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import {
import { runHookBuildGenerated } from '../../integrations/index.js';
import { isServerLikeOutput } from '../../prerender/utils.js';
import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { AstroError } from '../errors/index.js';
import * as AstroErrorData from '../errors/errors-data.js';
import { debug, info } from '../logger/core.js';
import { RedirectSinglePageBuiltModule, getRedirectLocationOrThrow } from '../redirects/index.js';
import { isEndpointResult } from '../render/core.js';
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import {
eachPageData,
type BuildInternals,
} from '../../core/build/internal.js';
import * as AstroErrorData from '../errors/errors-data.js';
import { emptyDir, removeEmptyDirs } from '../../core/fs/index.js';
import { appendForwardSlash, prependForwardSlash } from '../../core/path.js';
import { isModeServerWithNoAdapter } from '../../core/util.js';
import { runHookBuildSetup } from '../../integrations/index.js';
import { isServerLikeOutput } from '../../prerender/utils.js';
import { PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { AstroError } from '../errors/index.js';
import { info } from '../logger/core.js';
import { routeIsRedirect } from '../redirects/index.js';
import { getOutDirWithinCwd } from './common.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/compile/compile.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { TransformResult } from '@astrojs/compiler';
import type { ResolvedConfig } from 'vite';
import type { AstroConfig } from '../../@types/astro';

import * as AstroErrorData from '../errors/errors-data.js';
import { transform } from '@astrojs/compiler';
import { fileURLToPath } from 'node:url';
import { normalizePath } from 'vite';
import { AggregateError, AstroError, CompilerError } from '../errors/errors.js';
import { AstroErrorData } from '../errors/index.js';
import { resolvePath } from '../util.js';
import { createStylePreprocessor } from './style.js';

Expand Down Expand Up @@ -87,6 +86,7 @@ function handleCompileResultErrors(result: TransformResult, cssTransformErrors:

if (compilerError) {
throw new CompilerError({
name: 'CompilerError',
message: compilerError.text,
location: {
line: compilerError.location.line,
Expand Down
4 changes: 3 additions & 1 deletion packages/astro/src/core/compile/style.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { TransformOptions } from '@astrojs/compiler';
import fs from 'node:fs';
import { preprocessCSS, type ResolvedConfig } from 'vite';
import { AstroErrorData, CSSError, positionAt } from '../errors/index.js';
import { CSSError, positionAt } from '../errors/index.js';
import * as AstroErrorData from '../errors/errors-data.js';

export function createStylePreprocessor({
filename,
Expand Down Expand Up @@ -88,6 +89,7 @@ function enhanceCSSError(err: any, filename: string, cssContent: string) {
errorPosition.line += 1;

return new CSSError({
name: 'CSSError',
message: err.message,
location: {
file: filename,
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import type {
AstroUserConfig,
CLIFlags,
} from '../../@types/astro';

import * as AstroErrorData from '../errors/errors-data.js';
import * as colors from 'kleur/colors';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { ZodError } from 'zod';
import { eventConfigError, telemetry } from '../../events/index.js';
import { trackAstroConfigZodError } from '../errors/errors.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { AstroError } from '../errors/index.js';
import { formatConfigErrorMessage } from '../messages.js';
import { mergeConfig } from './merge.js';
import { createRelativeSchema } from './schema.js';
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/config/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { getContentPaths } from '../../content/index.js';
import jsxRenderer from '../../jsx/renderer.js';
import { markdownContentEntryType } from '../../vite-plugin-markdown/content-entry-type.js';
import { getDefaultClientDirectives } from '../client-directive/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { AstroError } from '../errors/index.js';
import * as AstroErrorData from '../errors/errors-data.js';
import { formatYAMLException, isYAMLException } from '../errors/utils.js';
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './../constants.js';
import { AstroTimer } from './timer.js';
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/cookies/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { CookieSerializeOptions } from 'cookie';
import { parse, serialize } from 'cookie';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { AstroError } from '../errors/index.js';
import * as AstroErrorData from '../errors/errors-data.js';

interface AstroCookieSetOptions {
domain?: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/endpoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import type { Environment, RenderContext } from '../render/index';
import { renderEndpoint } from '../../runtime/server/index.js';
import { ASTRO_VERSION } from '../constants.js';
import { AstroCookies, attachToResponse } from '../cookies/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { AstroError } from '../errors/index.js';
import { warn } from '../logger/core.js';
import { callMiddleware } from '../middleware/callMiddleware.js';
import * as AstroErrorData from '../errors/errors-data.js';

const clientAddressSymbol = Symbol.for('astro.clientAddress');
const clientLocalsSymbol = Symbol.for('astro.locals');
Expand Down
20 changes: 10 additions & 10 deletions packages/astro/src/core/errors/dev/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fileURLToPath } from 'node:url';
import { getHighlighter } from 'shiki';
import type { ErrorPayload } from 'vite';
import type { ModuleLoader } from '../../module-loader/index.js';
import { AstroErrorData } from '../errors-data.js';
import { FailedToLoadModuleSSR, InvalidGlob, MdxIntegrationMissingError } from '../errors-data.js';
import { AstroError, type ErrorWithMetadata } from '../errors.js';
import { createSafeError } from '../utils.js';
import type { SSRLoadedRenderer } from './../../../@types/astro.js';
Expand Down Expand Up @@ -41,10 +41,10 @@ export function enhanceViteSSRError({
// https://github.com/vitejs/vite/blob/ee7c28a46a6563d54b828af42570c55f16b15d2c/packages/vite/src/node/ssr/ssrModuleLoader.ts#L91
let importName: string | undefined;
if ((importName = safeError.message.match(/Failed to load url (.*?) \(resolved id:/)?.[1])) {
safeError.title = AstroErrorData.FailedToLoadModuleSSR.title;
safeError.title = FailedToLoadModuleSSR.title;
safeError.name = 'FailedToLoadModuleSSR';
safeError.message = AstroErrorData.FailedToLoadModuleSSR.message(importName);
safeError.hint = AstroErrorData.FailedToLoadModuleSSR.hint;
safeError.message = FailedToLoadModuleSSR.message(importName);
safeError.hint = FailedToLoadModuleSSR.hint;
const line = lns.findIndex((ln) => ln.includes(importName!));

if (line !== -1) {
Expand All @@ -68,8 +68,8 @@ export function enhanceViteSSRError({
fileId?.match(/\.mdx$/)
) {
safeError = new AstroError({
...AstroErrorData.MdxIntegrationMissingError,
message: AstroErrorData.MdxIntegrationMissingError.message(JSON.stringify(fileId)),
...MdxIntegrationMissingError,
message: MdxIntegrationMissingError.message(JSON.stringify(fileId)),
location: safeError.loc,
stack: safeError.stack,
}) as ErrorWithMetadata;
Expand All @@ -80,10 +80,10 @@ export function enhanceViteSSRError({
const globPattern = safeError.message.match(/glob: "(.+)" \(/)?.[1];

if (globPattern) {
safeError.message = AstroErrorData.InvalidGlob.message(globPattern);
safeError.message = InvalidGlob.message(globPattern);
safeError.name = 'InvalidGlob';
safeError.hint = AstroErrorData.InvalidGlob.hint;
safeError.title = AstroErrorData.InvalidGlob.title;
safeError.hint = InvalidGlob.hint;
safeError.title = InvalidGlob.title;

const line = lns.findIndex((ln) => ln.includes(globPattern));

Expand Down Expand Up @@ -137,7 +137,7 @@ export async function getViteErrorPayload(err: ErrorWithMetadata): Promise<Astro
const message = renderErrorMarkdown(err.message.trim(), 'html');
const hint = err.hint ? renderErrorMarkdown(err.hint.trim(), 'html') : undefined;

const hasDocs = err.name in AstroErrorData;
const hasDocs = !!err.name;
const docslink = hasDocs
? `https://docs.astro.build/en/reference/errors/${getKebabErrorName(err.name)}/`
: undefined;
Expand Down
Loading

0 comments on commit ea426bd

Please sign in to comment.