Veil UI
Components

Error Boundary

Catches render errors in a subtree and shows a recoverable fallback.

Installation

$ npx veil-ui@latest add error-boundary

Usage

import { ErrorBoundary } from "@/components/ui/error-boundary";

<ErrorBoundary onError={(error) => report(error)} fallback={({ reset }) => (
  <button type="button" onClick={reset}>
    Try again
  </button>
)}>
  <Dashboard />
</ErrorBoundary>;

Examples

Basic

Custom fallback

API reference

PropTypeDefaultDescription
childrenReact.ReactNode—Subtree protected by the boundary.
fallbackReactNode | ({ error, reset }) => ReactNode—Static or render-prop fallback; defaults to an alert panel.
onError(error, info) => void—Called from componentDidCatch for logging.
classNamestring—Applies to the default fallback.

Accessibility

  • The default fallback is a role="alert" region with the error message and a labelled “Try again” button.
  • Always provide a way to recover; resetting re-mounts the children.
  • Error boundaries do not catch async or event-handler errors; handle those locally.

Vue and plain HTML

This component also ships for Vue (Reka UI) and plain HTML. Install it with npx veil-ui add error-boundary --framework vue or switch the framework in components.json.

<script setup lang="ts">
import { AlertTriangle } from "lucide-vue-next";
import { onErrorCaptured, ref } from "vue";

import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";

import type { ErrorBoundaryProps, ErrorFallbackProps } from "./types";

defineOptions({ name: "ErrorBoundary" });

defineSlots<{
  default?: () => unknown;
  fallback?: (props: ErrorFallbackProps) => unknown;
}>();

const props = defineProps<ErrorBoundaryProps>();

const error = ref<Error | null>(null);

onErrorCaptured((captured, _instance, info) => {
  const normalized =
    captured instanceof Error ? captured : new Error(String(captured));
  error.value = normalized;
  props.onError?.(normalized, info);
  return false;
});

function reset() {
  error.value = null;
}
</script>

<template>
  <template v-if="!error">
    <slot />
  </template>
  <slot
    v-else
    name="fallback"
    :error="error ?? new Error('Unknown error')"
    :reset="reset"
  >
    <div
      data-slot="error-boundary"
      role="alert"
      :class="
        cn(
          'flex flex-col items-center justify-center gap-3 rounded-lg border border-destructive/30 bg-destructive/5 px-6 py-12 text-center',
          props.class,
        )
      "
    >
      <div
        aria-hidden
        class="flex size-10 items-center justify-center rounded-full bg-destructive/10 text-destructive"
      >
        <AlertTriangle class="size-5" />
      </div>
      <div class="grid gap-1">
        <p class="text-sm font-medium text-foreground">Something went wrong</p>
        <p class="text-sm text-muted-foreground">{{ error?.message }}</p>
      </div>
      <Button variant="outline" size="sm" @click="reset">Try again</Button>
    </div>
  </slot>
</template>

On this page