0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix: error overlay message escape (#12305)

Co-authored-by: Princesseuh <3019731+Princesseuh@users.noreply.github.com>
This commit is contained in:
Florian Lefebvre 2024-11-08 16:07:40 +01:00 committed by GitHub
parent e10b03e88c
commit f5f71094ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes a case where the error overlay would not escape the message

View file

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

View file

@ -1,6 +1,9 @@
import { EventEmitter } from 'node:events'; import { EventEmitter } from 'node:events';
import path from 'node:path'; import path from 'node:path';
import { pathToFileURL } from 'node:url';
import type * as vite from 'vite'; 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'; import type { ModuleLoader, ModuleLoaderEventEmitter } from './loader.js';
export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader { export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader {
@ -43,6 +46,24 @@ export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader {
} }
const msg = args[0] as vite.HMRPayload; const msg = args[0] as vite.HMRPayload;
if (msg?.type === 'error') { 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); events.emit('hmr-error', msg);
} }
_wsSend.apply(this, args); _wsSend.apply(this, args);