Skip to content

Commit

Permalink
fix: error overlay message escape (#12305)
Browse files Browse the repository at this point in the history
Co-authored-by: Princesseuh <[email protected]>
  • Loading branch information
florian-lefebvre and Princesseuh authored Nov 8, 2024
1 parent e10b03e commit f5f7109
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-plums-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a case where the error overlay would not escape the message
2 changes: 2 additions & 0 deletions packages/astro/src/core/errors/dev/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export function enhanceViteSSRError({
}

export interface AstroErrorPayload {
__isEnhancedAstroErrorPayload: true;
type: ErrorPayload['type'];
err: Omit<ErrorPayload['err'], 'loc'> & {
name?: string;
Expand Down Expand Up @@ -164,6 +165,7 @@ export async function getViteErrorPayload(err: ErrorWithMetadata): Promise<Astro
: undefined;

return {
__isEnhancedAstroErrorPayload: true,
type: 'error',
err: {
...err,
Expand Down
21 changes: 21 additions & 0 deletions packages/astro/src/core/module-loader/vite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { EventEmitter } from 'node:events';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import type * as vite from 'vite';
import { collectErrorMetadata } from '../errors/dev/utils.js';
import { getViteErrorPayload } from '../errors/dev/vite.js';
import type { ModuleLoader, ModuleLoaderEventEmitter } from './loader.js';

export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader {
Expand Down Expand Up @@ -43,6 +46,24 @@ export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader {
}
const msg = args[0] as vite.HMRPayload;
if (msg?.type === 'error') {
// If we have an error, but it didn't go through our error enhancement program, it means that it's a HMR error from
// vite itself, which goes through a different path. We need to enhance it here.
if (!(msg as any)['__isEnhancedAstroErrorPayload']) {
const err = collectErrorMetadata(msg.err, pathToFileURL(viteServer.config.root));
getViteErrorPayload(err).then((payload) => {
events.emit('hmr-error', {
type: 'error',
err: {
message: payload.err.message,
stack: payload.err.stack,
},
});

args[0] = payload;
_wsSend.apply(this, args);
});
return;
}
events.emit('hmr-error', msg);
}
_wsSend.apply(this, args);
Expand Down

0 comments on commit f5f7109

Please sign in to comment.