Home / Class/ ErrorOverlay Class — vite Architecture

ErrorOverlay Class — vite Architecture

Architecture documentation for the ErrorOverlay class in overlay.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  033d9b63_5269_212a_e5d9_bd61baab6243["ErrorOverlay"]
  e11865a0_84fe_cc5a_9b23_3f69f2300ca4["overlay.ts"]
  033d9b63_5269_212a_e5d9_bd61baab6243 -->|defined in| e11865a0_84fe_cc5a_9b23_3f69f2300ca4
  fa7ed98c_b01d_f7ac_6336_e938d321dd76["constructor()"]
  033d9b63_5269_212a_e5d9_bd61baab6243 -->|method| fa7ed98c_b01d_f7ac_6336_e938d321dd76
  b2324c1b_ec31_980c_8408_543ca7920038["text()"]
  033d9b63_5269_212a_e5d9_bd61baab6243 -->|method| b2324c1b_ec31_980c_8408_543ca7920038
  2752be78_665c_da23_b678_4ae249d7fa39["close()"]
  033d9b63_5269_212a_e5d9_bd61baab6243 -->|method| 2752be78_665c_da23_b678_4ae249d7fa39

Relationship Graph

Source Code

packages/vite/src/client/overlay.ts lines 216–299

export class ErrorOverlay extends HTMLElement {
  root: ShadowRoot
  closeOnEsc: (e: KeyboardEvent) => void

  constructor(err: ErrorPayload['err'], links = true) {
    super()
    this.root = this.attachShadow({ mode: 'open' })
    this.root.appendChild(createTemplate())

    codeframeRE.lastIndex = 0
    const hasFrame = err.frame && codeframeRE.test(err.frame)
    const message = hasFrame
      ? err.message.replace(codeframeRE, '')
      : err.message
    if (err.plugin) {
      this.text('.plugin', `[plugin:${err.plugin}] `)
    }
    this.text('.message-body', message.trim())

    const [file] = (err.loc?.file || err.id || 'unknown file').split(`?`)
    if (err.loc) {
      this.text('.file', `${file}:${err.loc.line}:${err.loc.column}`, links)
    } else if (err.id) {
      this.text('.file', file)
    }

    if (hasFrame) {
      this.text('.frame', err.frame!.trim())
    }
    this.text('.stack', err.stack, links)

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

    this.addEventListener('click', () => {
      this.close()
    })

    this.closeOnEsc = (e: KeyboardEvent) => {
      if (e.key === 'Escape' || e.code === 'Escape') {
        this.close()
      }
    }

    document.addEventListener('keydown', this.closeOnEsc)
  }

  text(selector: string, text: string, linkFiles = false): void {
    const el = this.root.querySelector(selector)!
    if (!linkFiles) {
      el.textContent = text
    } else {
      let curIndex = 0
      let match: RegExpExecArray | null
      fileRE.lastIndex = 0
      while ((match = fileRE.exec(text))) {
        const { 0: file, index } = match
        const frag = text.slice(curIndex, index)
        el.appendChild(document.createTextNode(frag))
        const link = document.createElement('a')
        link.textContent = file
        link.className = 'file-link'
        link.onclick = () => {
          fetch(
            new URL(
              `${base}__open-in-editor?file=${encodeURIComponent(file)}`,
              import.meta.url,
            ),
          )
        }
        el.appendChild(link)
        curIndex += frag.length + file.length
      }
      if (curIndex < text.length) {
        el.appendChild(document.createTextNode(text.slice(curIndex)))
      }
    }
  }
  close(): void {
    this.parentNode?.removeChild(this)

Domain

Frequently Asked Questions

What is the ErrorOverlay class?
ErrorOverlay is a class in the vite codebase, defined in packages/vite/src/client/overlay.ts.
Where is ErrorOverlay defined?
ErrorOverlay is defined in packages/vite/src/client/overlay.ts at line 216.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free