Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: read error from custom element attributes in vite-error-overlay #5686

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts
Expand Up @@ -152,3 +152,12 @@ test('deep import built-in module', async () => {
await page.goto(url)
expect(await page.textContent('.file-message')).toMatch('fs/promises')
})

if (!isBuild) {
test('error overlay', async () => {
await page.goto(url + '/error')
expect(await page.textContent('vite-error-overlay .message-body')).toMatch(
'This should render vite-error-overlay'
)
})
}
18 changes: 16 additions & 2 deletions packages/playground/ssr-vue/server.js
Expand Up @@ -52,10 +52,11 @@ async function createServer(
}

app.use('*', async (req, res) => {
let template, render

try {
const url = req.originalUrl

let template, render
if (!isProd) {
// always read fresh template in dev
template = fs.readFileSync(resolve('index.html'), 'utf-8')
Expand All @@ -76,7 +77,20 @@ async function createServer(
} catch (e) {
vite && vite.ssrFixStacktrace(e)
console.log(e.stack)
res.status(500).end(e.stack)

if (template && !isProd) {
res
.status(500)
.set({ 'Content-Type': 'text/html' })
.end(
template.replace(
`</body>`,
`<vite-error-overlay message="${e.message}" stack="${e.stack}"></vite-error-overlay>\n</body>`
)
)
} else {
res.status(500).end(e.stack)
}
}
})

Expand Down
11 changes: 11 additions & 0 deletions packages/playground/ssr-vue/src/pages/Error.vue
@@ -0,0 +1,11 @@
<template>
<h1>Error</h1>
</template>

<script>
export default {
setup() {
throw new Error('This should render vite-error-overlay')
}
}
</script>
36 changes: 29 additions & 7 deletions packages/vite/src/client/overlay.ts
Expand Up @@ -123,6 +123,35 @@ export class ErrorOverlay extends HTMLElement {
this.root = this.attachShadow({ mode: 'open' })
this.root.innerHTML = template

if (!err) {
frandiox marked this conversation as resolved.
Show resolved Hide resolved
err = {
id: this.getAttribute('id') || undefined,
message: this.getAttribute('message') || '',
stack: this.getAttribute('stack') || '',
plugin: this.getAttribute('plugin') || undefined,
pluginCode: this.getAttribute('plugin-code') || undefined,
frame: this.getAttribute('frame') || undefined,
loc: this.hasAttribute('loc-line')
? {
file: this.getAttribute('loc-file') || undefined,
line: Number(this.getAttribute('loc-line')),
column: Number(this.getAttribute('loc-column'))
}
: undefined
}
}

this.showError(err)

this.root.querySelector('.window')!.addEventListener('click', (e) => {
e.stopPropagation()
})
this.addEventListener('click', () => {
this.close()
})
}

showError(err: ErrorPayload['err']) {
codeframeRE.lastIndex = 0
const hasFrame = err.frame && codeframeRE.test(err.frame)
const message = hasFrame
Expand All @@ -144,13 +173,6 @@ export class ErrorOverlay extends HTMLElement {
this.text('.frame', err.frame!.trim())
}
this.text('.stack', err.stack, true)

this.root.querySelector('.window')!.addEventListener('click', (e) => {
e.stopPropagation()
})
this.addEventListener('click', () => {
this.close()
})
}

text(selector: string, text: string, linkFiles = false): void {
Expand Down