Skip to content

Commit f5f7109

Browse files
fix: error overlay message escape (#12305)
Co-authored-by: Princesseuh <3019731+Princesseuh@users.noreply.github.com>
1 parent e10b03e commit f5f7109

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

.changeset/breezy-plums-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a case where the error overlay would not escape the message

packages/astro/src/core/errors/dev/vite.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export function enhanceViteSSRError({
105105
}
106106

107107
export interface AstroErrorPayload {
108+
__isEnhancedAstroErrorPayload: true;
108109
type: ErrorPayload['type'];
109110
err: Omit<ErrorPayload['err'], 'loc'> & {
110111
name?: string;
@@ -164,6 +165,7 @@ export async function getViteErrorPayload(err: ErrorWithMetadata): Promise<Astro
164165
: undefined;
165166

166167
return {
168+
__isEnhancedAstroErrorPayload: true,
167169
type: 'error',
168170
err: {
169171
...err,

packages/astro/src/core/module-loader/vite.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { EventEmitter } from 'node:events';
22
import path from 'node:path';
3+
import { pathToFileURL } from 'node:url';
34
import type * as vite from 'vite';
5+
import { collectErrorMetadata } from '../errors/dev/utils.js';
6+
import { getViteErrorPayload } from '../errors/dev/vite.js';
47
import type { ModuleLoader, ModuleLoaderEventEmitter } from './loader.js';
58

69
export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader {
@@ -43,6 +46,24 @@ export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader {
4346
}
4447
const msg = args[0] as vite.HMRPayload;
4548
if (msg?.type === 'error') {
49+
// If we have an error, but it didn't go through our error enhancement program, it means that it's a HMR error from
50+
// vite itself, which goes through a different path. We need to enhance it here.
51+
if (!(msg as any)['__isEnhancedAstroErrorPayload']) {
52+
const err = collectErrorMetadata(msg.err, pathToFileURL(viteServer.config.root));
53+
getViteErrorPayload(err).then((payload) => {
54+
events.emit('hmr-error', {
55+
type: 'error',
56+
err: {
57+
message: payload.err.message,
58+
stack: payload.err.stack,
59+
},
60+
});
61+
62+
args[0] = payload;
63+
_wsSend.apply(this, args);
64+
});
65+
return;
66+
}
4667
events.emit('hmr-error', msg);
4768
}
4869
_wsSend.apply(this, args);

0 commit comments

Comments
 (0)