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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,12 @@ test('import.meta.url', async () => {
await page.goto(url)
expect(await page.textContent('.protocol')).toEqual('file:')
})

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<h1>Error</h1>
</template>

<script>
export default {
setup() {
throw new Error('This should render vite-error-overlay')
}
}
</script>
38 changes: 31 additions & 7 deletions packages/vite/src/client/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,37 @@ 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
// In SSR, the overlay element can be directly injected in
// the HTML response with the error information as attributes.
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 +175,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