Skip to content

Commit ef5d94c

Browse files
committed
fix(core): preserve error properties for Astro's error overlay enrichment
Pass through loc, hint, frame, fullCode, plugin, pluginCode, id, and cause from the original error to the WebSocket payload. Astro's module loader intercepts hot.send() and calls collectErrorMetadata() + getViteErrorPayload() to enrich the overlay — but only if these properties are present. Without them, the overlay was missing source code highlighting, error carets, and hints.
1 parent 8dbd6db commit ef5d94c

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

packages/core/src/dev/setup.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from "astro";
99
import { mergeConfig } from "astro/config";
1010
import type { Element } from "hast";
11-
import type { ViteDevServer } from "vite";
11+
import type { ErrorPayload, ViteDevServer } from "vite";
1212
import { getStylesForURL } from "@/dev/css";
1313
import type {
1414
MightyDevMiddleware,
@@ -217,19 +217,38 @@ export async function setupDev(
217217
throw error;
218218
}
219219

220+
const err = error as Error & Record<string, unknown>;
221+
222+
// Send error to Astro's error overlay via WebSocket.
223+
// Astro's module loader intercepts hot.send() and enriches the payload
224+
// with source code highlighting, hints, and docs links via
225+
// collectErrorMetadata() + getViteErrorPayload().
226+
// We preserve all error properties (loc, hint, frame, etc.) so the
227+
// enrichment has full context — especially for AstroError subclasses.
228+
const errPayload: ErrorPayload = {
229+
type: "error",
230+
err: {
231+
name: err.name,
232+
message: err.message,
233+
stack: err.stack ?? "",
234+
loc: err.loc,
235+
hint: err.hint,
236+
frame: err.frame,
237+
fullCode: err.fullCode,
238+
plugin: err.plugin,
239+
pluginCode: err.pluginCode,
240+
id: err.id,
241+
cause: err.cause,
242+
} as ErrorPayload["err"],
243+
};
244+
220245
setTimeout(() => {
221-
viteServer.environments.client.hot.send({
222-
type: "error",
223-
err: {
224-
message: (error as Error).message,
225-
stack: (error as Error).stack ?? "",
226-
},
227-
});
246+
viteServer.environments.client.hot.send(errPayload);
228247
}, 200);
229248

230249
return {
231250
status: 500,
232-
content: `<html><head><title>${(error as Error).name}</title><script type="module" src="${request.address}/@vite/client"></script></head><body></body></html>`,
251+
content: `<html><head><title>${err.name}</title><script type="module" src="${request.address}/@vite/client"></script></head><body></body></html>`,
233252
};
234253
}
235254
},

0 commit comments

Comments
 (0)