All files / web/src/lib/arcade/game-sdk GameErrorBoundary.tsx

0% Statements 0/124
0% Branches 0/1
0% Functions 0/1
0% Lines 0/124

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                                                                                                                                                                                                                                                         
/**
 * Error Boundary for Arcade Games
 *
 * Catches errors in game components and displays a friendly error message
 * instead of crashing the entire app.
 */

'use client'

import { Component, type ReactNode } from 'react'

interface Props {
  children: ReactNode
  gameName?: string
}

interface State {
  hasError: boolean
  error?: Error
}

export class GameErrorBoundary extends Component<Props, State> {
  constructor(props: Props) {
    super(props)
    this.state = { hasError: false }
  }

  static getDerivedStateFromError(error: Error): State {
    return {
      hasError: true,
      error,
    }
  }

  componentDidCatch(error: Error, errorInfo: unknown) {
    console.error('Game error:', error, errorInfo)
  }

  render() {
    if (this.state.hasError) {
      return (
        <div
          style={{
            display: 'flex',
            flexDirection: 'column',
            alignItems: 'center',
            justifyContent: 'center',
            padding: '40px',
            textAlign: 'center',
            minHeight: '400px',
            background: 'linear-gradient(135deg, #fef2f2, #fee2e2)',
          }}
        >
          <div
            style={{
              fontSize: '64px',
              marginBottom: '20px',
            }}
          >
            ⚠️
          </div>
          <h2
            style={{
              fontSize: '24px',
              fontWeight: 'bold',
              marginBottom: '12px',
              color: '#dc2626',
            }}
          >
            Game Error
          </h2>
          <p
            style={{
              fontSize: '16px',
              color: '#6b7280',
              marginBottom: '12px',
              maxWidth: '500px',
            }}
          >
            {this.props.gameName
              ? `There was an error loading the game "${this.props.gameName}".`
              : 'There was an error loading the game.'}
          </p>
          {this.state.error && (
            <pre
              style={{
                background: '#f9fafb',
                border: '1px solid #e5e7eb',
                borderRadius: '8px',
                padding: '16px',
                marginTop: '12px',
                maxWidth: '600px',
                overflow: 'auto',
                textAlign: 'left',
                fontSize: '12px',
                color: '#374151',
              }}
            >
              {this.state.error.message}
            </pre>
          )}
          <button
            onClick={() => window.location.reload()}
            style={{
              marginTop: '24px',
              padding: '12px 24px',
              background: '#3b82f6',
              color: 'white',
              border: 'none',
              borderRadius: '8px',
              fontSize: '14px',
              fontWeight: 600,
              cursor: 'pointer',
            }}
          >
            Reload Page
          </button>
        </div>
      )
    }

    return this.props.children
  }
}