Error boundary

Catch an error thrown while rendering, show a fallback, and reset.

App.kn
<script lang="ts">
  import { ErrorBoundary } from '@inflector/keeno'
  import Fragile from './Fragile.kn'

  const caught = state(0)
</script>

<div class="demo">
  <ErrorBoundary
    fallback={(error, reset) => (
      <div class="fallback" role="alert">
        <span class="icon">!</span>
        <div class="body">
          <strong>Something broke</strong>
          <code>{String(error)}</code>
        </div>
        <button onClick={reset}>Try again</button>
      </div>
    )}
    onError={(error) => {
      caught.set(caught.peek() + 1)
      console.warn('Caught by the boundary:', error)
    }}
  >
    <Fragile />
  </ErrorBoundary>

  <p class="meta">
    <span>The child throws at 3; the boundary shows a fallback.</span>
    <span class="count">caught {caught}</span>
  </p>
</div>

<style>
  .demo {
    display: grid;
    gap: 0.75rem;
  }
  .fallback {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-height: 4.5rem;
    padding: 0.875rem 1rem;
    border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent);
    border-radius: 0.75rem;
    background: color-mix(in srgb, var(--danger) 6%, var(--bg));
  }
  .icon {
    display: grid;
    place-items: center;
    flex: none;
    width: 1.5rem;
    height: 1.5rem;
    border-radius: 50%;
    background: color-mix(in srgb, var(--danger) 15%, transparent);
    color: var(--danger);
    font-size: 0.8rem;
    font-weight: 700;
  }
  .body {
    display: grid;
    flex: 1;
    min-width: 0;
  }
  strong {
    font-weight: 500;
  }
  code {
    overflow: hidden;
    color: var(--muted);
    font-size: 0.75rem;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .meta {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    margin: 0;
    font-size: 0.75rem;
    color: var(--muted);
  }
  .count {
    flex: none;
    font-family: var(--font-mono);
  }
</style>