@useavalon/avalon 0.1.12 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (230) hide show
  1. package/mod.ts +302 -0
  2. package/package.json +9 -17
  3. package/src/build/integration-bundler-plugin.ts +116 -0
  4. package/src/build/integration-config.ts +168 -0
  5. package/src/build/integration-detection-plugin.ts +117 -0
  6. package/src/build/integration-resolver-plugin.ts +90 -0
  7. package/src/build/island-manifest.ts +269 -0
  8. package/src/build/island-types-generator.ts +476 -0
  9. package/src/build/mdx-island-transform.ts +464 -0
  10. package/src/build/mdx-plugin.ts +98 -0
  11. package/src/build/page-island-transform.ts +598 -0
  12. package/src/build/prop-extractors/index.ts +21 -0
  13. package/src/build/prop-extractors/lit.ts +140 -0
  14. package/src/build/prop-extractors/qwik.ts +16 -0
  15. package/src/build/prop-extractors/solid.ts +125 -0
  16. package/src/build/prop-extractors/svelte.ts +194 -0
  17. package/src/build/prop-extractors/vue.ts +111 -0
  18. package/src/build/sidecar-file-manager.ts +104 -0
  19. package/src/build/sidecar-renderer.ts +30 -0
  20. package/src/client/adapters/index.ts +21 -0
  21. package/src/client/components.ts +35 -0
  22. package/src/client/css-hmr-handler.ts +344 -0
  23. package/src/client/framework-adapter.ts +462 -0
  24. package/src/client/hmr-coordinator.ts +396 -0
  25. package/src/client/hmr-error-overlay.js +533 -0
  26. package/src/client/main.js +824 -0
  27. package/src/components/Image.tsx +123 -0
  28. package/src/components/IslandErrorBoundary.tsx +145 -0
  29. package/src/components/LayoutDataErrorBoundary.tsx +141 -0
  30. package/src/components/LayoutErrorBoundary.tsx +127 -0
  31. package/src/components/PersistentIsland.tsx +52 -0
  32. package/src/components/StreamingErrorBoundary.tsx +233 -0
  33. package/src/components/StreamingLayout.tsx +538 -0
  34. package/src/core/components/component-analyzer.ts +192 -0
  35. package/src/core/components/component-detection.ts +508 -0
  36. package/src/core/components/enhanced-framework-detector.ts +500 -0
  37. package/src/core/components/framework-registry.ts +563 -0
  38. package/src/core/content/mdx-processor.ts +46 -0
  39. package/src/core/integrations/index.ts +19 -0
  40. package/src/core/integrations/loader.ts +125 -0
  41. package/src/core/integrations/registry.ts +175 -0
  42. package/src/core/islands/island-persistence.ts +325 -0
  43. package/src/core/islands/island-state-serializer.ts +258 -0
  44. package/src/core/islands/persistent-island-context.tsx +80 -0
  45. package/src/core/islands/use-persistent-state.ts +68 -0
  46. package/src/core/layout/enhanced-layout-resolver.ts +322 -0
  47. package/src/core/layout/layout-cache-manager.ts +485 -0
  48. package/src/core/layout/layout-composer.ts +357 -0
  49. package/src/core/layout/layout-data-loader.ts +516 -0
  50. package/src/core/layout/layout-discovery.ts +243 -0
  51. package/src/core/layout/layout-matcher.ts +299 -0
  52. package/src/core/layout/layout-types.ts +110 -0
  53. package/src/core/modules/framework-module-resolver.ts +273 -0
  54. package/src/islands/component-analysis.ts +213 -0
  55. package/src/islands/css-utils.ts +565 -0
  56. package/src/islands/discovery/index.ts +80 -0
  57. package/src/islands/discovery/registry.ts +340 -0
  58. package/src/islands/discovery/resolver.ts +477 -0
  59. package/src/islands/discovery/scanner.ts +386 -0
  60. package/src/islands/discovery/types.ts +117 -0
  61. package/src/islands/discovery/validator.ts +544 -0
  62. package/src/islands/discovery/watcher.ts +368 -0
  63. package/src/islands/framework-detection.ts +428 -0
  64. package/src/islands/integration-loader.ts +490 -0
  65. package/src/islands/island.tsx +565 -0
  66. package/src/islands/render-cache.ts +550 -0
  67. package/src/islands/types.ts +80 -0
  68. package/src/islands/universal-css-collector.ts +157 -0
  69. package/src/islands/universal-head-collector.ts +137 -0
  70. package/src/layout-system.ts +218 -0
  71. package/src/middleware/discovery.ts +268 -0
  72. package/src/middleware/executor.ts +315 -0
  73. package/src/middleware/index.ts +76 -0
  74. package/src/middleware/types.ts +99 -0
  75. package/src/nitro/build-config.ts +576 -0
  76. package/src/nitro/config.ts +483 -0
  77. package/src/nitro/error-handler.ts +636 -0
  78. package/src/nitro/index.ts +173 -0
  79. package/src/nitro/island-manifest.ts +584 -0
  80. package/src/nitro/middleware-adapter.ts +260 -0
  81. package/src/nitro/renderer.ts +1471 -0
  82. package/src/nitro/route-discovery.ts +439 -0
  83. package/src/nitro/types.ts +321 -0
  84. package/src/render/collect-css.ts +198 -0
  85. package/src/render/error-pages.ts +79 -0
  86. package/src/render/isolated-ssr-renderer.ts +654 -0
  87. package/src/render/ssr.ts +1030 -0
  88. package/src/schemas/api.ts +30 -0
  89. package/src/schemas/core.ts +64 -0
  90. package/src/schemas/index.ts +212 -0
  91. package/src/schemas/layout.ts +279 -0
  92. package/src/schemas/routing/index.ts +38 -0
  93. package/src/schemas/routing.ts +376 -0
  94. package/src/types/as-island.ts +20 -0
  95. package/src/types/layout.ts +285 -0
  96. package/src/types/routing.ts +555 -0
  97. package/src/types/types.ts +5 -0
  98. package/src/utils/dev-logger.ts +299 -0
  99. package/src/utils/fs.ts +151 -0
  100. package/src/vite-plugin/auto-discover.ts +551 -0
  101. package/src/vite-plugin/config.ts +266 -0
  102. package/src/vite-plugin/errors.ts +127 -0
  103. package/src/vite-plugin/image-optimization.ts +156 -0
  104. package/src/vite-plugin/integration-activator.ts +126 -0
  105. package/src/vite-plugin/island-sidecar-plugin.ts +176 -0
  106. package/src/vite-plugin/module-discovery.ts +189 -0
  107. package/src/vite-plugin/nitro-integration.ts +1354 -0
  108. package/src/vite-plugin/plugin.ts +403 -0
  109. package/src/vite-plugin/types.ts +327 -0
  110. package/src/vite-plugin/validation.ts +228 -0
  111. package/dist/mod.js +0 -1
  112. package/dist/src/build/integration-bundler-plugin.js +0 -1
  113. package/dist/src/build/integration-config.js +0 -1
  114. package/dist/src/build/integration-detection-plugin.js +0 -1
  115. package/dist/src/build/integration-resolver-plugin.js +0 -1
  116. package/dist/src/build/island-manifest.js +0 -1
  117. package/dist/src/build/island-types-generator.js +0 -5
  118. package/dist/src/build/mdx-island-transform.js +0 -2
  119. package/dist/src/build/mdx-plugin.js +0 -1
  120. package/dist/src/build/page-island-transform.js +0 -3
  121. package/dist/src/build/prop-extractors/index.js +0 -1
  122. package/dist/src/build/prop-extractors/lit.js +0 -1
  123. package/dist/src/build/prop-extractors/qwik.js +0 -1
  124. package/dist/src/build/prop-extractors/solid.js +0 -1
  125. package/dist/src/build/prop-extractors/svelte.js +0 -1
  126. package/dist/src/build/prop-extractors/vue.js +0 -1
  127. package/dist/src/build/sidecar-file-manager.js +0 -1
  128. package/dist/src/build/sidecar-renderer.js +0 -6
  129. package/dist/src/client/adapters/index.js +0 -1
  130. package/dist/src/client/components.js +0 -1
  131. package/dist/src/client/css-hmr-handler.js +0 -1
  132. package/dist/src/client/framework-adapter.js +0 -13
  133. package/dist/src/client/hmr-coordinator.js +0 -1
  134. package/dist/src/client/hmr-error-overlay.js +0 -214
  135. package/dist/src/client/main.js +0 -39
  136. package/dist/src/components/Image.js +0 -1
  137. package/dist/src/components/IslandErrorBoundary.js +0 -1
  138. package/dist/src/components/LayoutDataErrorBoundary.js +0 -1
  139. package/dist/src/components/LayoutErrorBoundary.js +0 -1
  140. package/dist/src/components/PersistentIsland.js +0 -1
  141. package/dist/src/components/StreamingErrorBoundary.js +0 -1
  142. package/dist/src/components/StreamingLayout.js +0 -29
  143. package/dist/src/core/components/component-analyzer.js +0 -1
  144. package/dist/src/core/components/component-detection.js +0 -5
  145. package/dist/src/core/components/enhanced-framework-detector.js +0 -1
  146. package/dist/src/core/components/framework-registry.js +0 -1
  147. package/dist/src/core/content/mdx-processor.js +0 -1
  148. package/dist/src/core/integrations/index.js +0 -1
  149. package/dist/src/core/integrations/loader.js +0 -1
  150. package/dist/src/core/integrations/registry.js +0 -1
  151. package/dist/src/core/islands/island-persistence.js +0 -1
  152. package/dist/src/core/islands/island-state-serializer.js +0 -1
  153. package/dist/src/core/islands/persistent-island-context.js +0 -1
  154. package/dist/src/core/islands/use-persistent-state.js +0 -1
  155. package/dist/src/core/layout/enhanced-layout-resolver.js +0 -1
  156. package/dist/src/core/layout/layout-cache-manager.js +0 -1
  157. package/dist/src/core/layout/layout-composer.js +0 -1
  158. package/dist/src/core/layout/layout-data-loader.js +0 -1
  159. package/dist/src/core/layout/layout-discovery.js +0 -1
  160. package/dist/src/core/layout/layout-matcher.js +0 -1
  161. package/dist/src/core/layout/layout-types.js +0 -1
  162. package/dist/src/core/modules/framework-module-resolver.js +0 -1
  163. package/dist/src/islands/component-analysis.js +0 -1
  164. package/dist/src/islands/css-utils.js +0 -17
  165. package/dist/src/islands/discovery/index.js +0 -1
  166. package/dist/src/islands/discovery/registry.js +0 -1
  167. package/dist/src/islands/discovery/resolver.js +0 -2
  168. package/dist/src/islands/discovery/scanner.js +0 -1
  169. package/dist/src/islands/discovery/types.js +0 -1
  170. package/dist/src/islands/discovery/validator.js +0 -18
  171. package/dist/src/islands/discovery/watcher.js +0 -1
  172. package/dist/src/islands/framework-detection.js +0 -1
  173. package/dist/src/islands/integration-loader.js +0 -1
  174. package/dist/src/islands/island.js +0 -1
  175. package/dist/src/islands/render-cache.js +0 -1
  176. package/dist/src/islands/types.js +0 -1
  177. package/dist/src/islands/universal-css-collector.js +0 -5
  178. package/dist/src/islands/universal-head-collector.js +0 -2
  179. package/dist/src/layout-system.js +0 -1
  180. package/dist/src/middleware/discovery.js +0 -1
  181. package/dist/src/middleware/executor.js +0 -1
  182. package/dist/src/middleware/index.js +0 -1
  183. package/dist/src/middleware/types.js +0 -1
  184. package/dist/src/nitro/build-config.js +0 -1
  185. package/dist/src/nitro/config.js +0 -1
  186. package/dist/src/nitro/error-handler.js +0 -198
  187. package/dist/src/nitro/index.js +0 -1
  188. package/dist/src/nitro/island-manifest.js +0 -2
  189. package/dist/src/nitro/middleware-adapter.js +0 -1
  190. package/dist/src/nitro/renderer.js +0 -183
  191. package/dist/src/nitro/route-discovery.js +0 -1
  192. package/dist/src/nitro/types.js +0 -1
  193. package/dist/src/render/collect-css.js +0 -3
  194. package/dist/src/render/error-pages.js +0 -48
  195. package/dist/src/render/isolated-ssr-renderer.js +0 -1
  196. package/dist/src/render/ssr.js +0 -90
  197. package/dist/src/schemas/api.js +0 -1
  198. package/dist/src/schemas/core.js +0 -1
  199. package/dist/src/schemas/index.js +0 -1
  200. package/dist/src/schemas/layout.js +0 -1
  201. package/dist/src/schemas/routing/index.js +0 -1
  202. package/dist/src/schemas/routing.js +0 -1
  203. package/dist/src/types/as-island.js +0 -1
  204. package/dist/src/types/layout.js +0 -1
  205. package/dist/src/types/routing.js +0 -1
  206. package/dist/src/types/types.js +0 -1
  207. package/dist/src/utils/dev-logger.js +0 -12
  208. package/dist/src/utils/fs.js +0 -1
  209. package/dist/src/vite-plugin/auto-discover.js +0 -1
  210. package/dist/src/vite-plugin/config.js +0 -1
  211. package/dist/src/vite-plugin/errors.js +0 -1
  212. package/dist/src/vite-plugin/image-optimization.js +0 -45
  213. package/dist/src/vite-plugin/integration-activator.js +0 -1
  214. package/dist/src/vite-plugin/island-sidecar-plugin.js +0 -1
  215. package/dist/src/vite-plugin/module-discovery.js +0 -1
  216. package/dist/src/vite-plugin/nitro-integration.js +0 -42
  217. package/dist/src/vite-plugin/plugin.js +0 -1
  218. package/dist/src/vite-plugin/types.js +0 -1
  219. package/dist/src/vite-plugin/validation.js +0 -2
  220. /package/{dist/src → src}/client/types/framework-runtime.d.ts +0 -0
  221. /package/{dist/src → src}/client/types/vite-hmr.d.ts +0 -0
  222. /package/{dist/src → src}/client/types/vite-virtual-modules.d.ts +0 -0
  223. /package/{dist/src → src}/layout-system.d.ts +0 -0
  224. /package/{dist/src → src}/types/image.d.ts +0 -0
  225. /package/{dist/src → src}/types/index.d.ts +0 -0
  226. /package/{dist/src → src}/types/island-jsx.d.ts +0 -0
  227. /package/{dist/src → src}/types/island-prop.d.ts +0 -0
  228. /package/{dist/src → src}/types/mdx.d.ts +0 -0
  229. /package/{dist/src → src}/types/urlpattern.d.ts +0 -0
  230. /package/{dist/src → src}/types/vite-env.d.ts +0 -0
@@ -0,0 +1,636 @@
1
+ /**
2
+ * Nitro Error Handler for Avalon
3
+ *
4
+ * This module provides error handling utilities for the Nitro integration,
5
+ * including support for custom error pages (404, 500, _error).
6
+ *
7
+ * Custom error pages are discovered from the pages directory:
8
+ * - src/pages/404.tsx → Custom 404 page
9
+ * - src/pages/500.tsx → Custom 500 page
10
+ * - src/pages/_error.tsx → Generic error page (fallback)
11
+ *
12
+ * Requirements: 10.1, 10.2, 10.3, 10.4, 10.5
13
+ */
14
+
15
+ import type { PageModule, NitroRenderContext, AvalonRuntimeConfig } from "./types.ts";
16
+ import type { H3Event } from "h3";
17
+ import { HttpError, isHttpError, createNotFoundError, createInternalError } from "./types.ts";
18
+ import { createRenderContext, getRequestURL } from "./renderer.ts";
19
+
20
+ /**
21
+ * Error page props passed to custom error page components
22
+ */
23
+ export interface ErrorPageProps {
24
+ /** HTTP status code */
25
+ statusCode: number;
26
+ /** Error message */
27
+ message: string;
28
+ /** Error object (development only) */
29
+ error?: Error;
30
+ /** Stack trace (development only) */
31
+ stack?: string;
32
+ /** Request URL that caused the error */
33
+ url?: string;
34
+ }
35
+
36
+ /**
37
+ * Options for error handling
38
+ */
39
+ export interface ErrorHandlerOptions {
40
+ /** Whether running in development mode */
41
+ isDev?: boolean;
42
+ /** Avalon runtime configuration */
43
+ avalonConfig?: AvalonRuntimeConfig;
44
+ /** Custom page module loader */
45
+ loadPageModule?: (filePath: string) => Promise<PageModule>;
46
+ /** Pages directory path */
47
+ pagesDir?: string;
48
+ }
49
+
50
+ /**
51
+ * Cache for discovered error pages
52
+ */
53
+ interface ErrorPageCache {
54
+ /** Custom 404 page module */
55
+ notFound?: PageModule | null;
56
+ /** Custom 500 page module */
57
+ serverError?: PageModule | null;
58
+ /** Generic error page module */
59
+ genericError?: PageModule | null;
60
+ /** Whether cache has been initialized */
61
+ initialized: boolean;
62
+ }
63
+
64
+ // Module-level cache for error pages
65
+ let errorPageCache: ErrorPageCache = {
66
+ initialized: false,
67
+ };
68
+
69
+ /**
70
+ * Clears the error page cache
71
+ * Call this during development when error pages change
72
+ */
73
+ export function clearErrorPageCache(): void {
74
+ errorPageCache = {
75
+ initialized: false,
76
+ };
77
+ }
78
+
79
+ /**
80
+ * Discovers and caches custom error pages from the pages directory
81
+ *
82
+ * @param options - Error handler options
83
+ * @returns Object containing discovered error page modules
84
+ */
85
+ export async function discoverErrorPages(
86
+ options: ErrorHandlerOptions
87
+ ): Promise<ErrorPageCache> {
88
+ if (errorPageCache.initialized && !options.isDev) {
89
+ return errorPageCache;
90
+ }
91
+
92
+ const { loadPageModule, pagesDir = "src/pages" } = options;
93
+
94
+ if (!loadPageModule) {
95
+ errorPageCache.initialized = true;
96
+ return errorPageCache;
97
+ }
98
+
99
+ // Try to load custom 404 page
100
+ try {
101
+ errorPageCache.notFound = await loadPageModule(`${pagesDir}/404.tsx`);
102
+ } catch {
103
+ // Try .jsx extension
104
+ try {
105
+ errorPageCache.notFound = await loadPageModule(`${pagesDir}/404.jsx`);
106
+ } catch {
107
+ errorPageCache.notFound = null;
108
+ }
109
+ }
110
+
111
+ // Try to load custom 500 page
112
+ try {
113
+ errorPageCache.serverError = await loadPageModule(`${pagesDir}/500.tsx`);
114
+ } catch {
115
+ // Try .jsx extension
116
+ try {
117
+ errorPageCache.serverError = await loadPageModule(`${pagesDir}/500.jsx`);
118
+ } catch {
119
+ errorPageCache.serverError = null;
120
+ }
121
+ }
122
+
123
+ // Try to load generic error page
124
+ try {
125
+ errorPageCache.genericError = await loadPageModule(`${pagesDir}/_error.tsx`);
126
+ } catch {
127
+ // Try .jsx extension
128
+ try {
129
+ errorPageCache.genericError = await loadPageModule(`${pagesDir}/_error.jsx`);
130
+ } catch {
131
+ errorPageCache.genericError = null;
132
+ }
133
+ }
134
+
135
+ errorPageCache.initialized = true;
136
+ return errorPageCache;
137
+ }
138
+
139
+ /**
140
+ * Gets the appropriate error page module for a status code
141
+ *
142
+ * @param statusCode - HTTP status code
143
+ * @param cache - Error page cache
144
+ * @returns Error page module or null if no custom page exists
145
+ */
146
+ export function getErrorPageModule(
147
+ statusCode: number,
148
+ cache: ErrorPageCache
149
+ ): PageModule | null {
150
+ // Check for specific status code pages first
151
+ if (statusCode === 404 && cache.notFound) {
152
+ return cache.notFound;
153
+ }
154
+
155
+ if (statusCode === 500 && cache.serverError) {
156
+ return cache.serverError;
157
+ }
158
+
159
+ // Fall back to generic error page
160
+ if (cache.genericError) {
161
+ return cache.genericError;
162
+ }
163
+
164
+ return null;
165
+ }
166
+
167
+ /**
168
+ * Creates error page props from an error
169
+ *
170
+ * @param error - The error that occurred
171
+ * @param url - Request URL
172
+ * @param isDev - Whether running in development mode
173
+ * @returns Error page props
174
+ */
175
+ export function createErrorPageProps(
176
+ error: Error | HttpError,
177
+ url?: string,
178
+ isDev?: boolean
179
+ ): ErrorPageProps {
180
+ const statusCode = isHttpError(error) ? error.statusCode : 500;
181
+
182
+ const props: ErrorPageProps = {
183
+ statusCode,
184
+ message: error.message,
185
+ url,
186
+ };
187
+
188
+ // Include error details only in development
189
+ if (isDev) {
190
+ props.error = error;
191
+ props.stack = error.stack;
192
+ }
193
+
194
+ return props;
195
+ }
196
+
197
+ /**
198
+ * Renders a custom error page to HTML
199
+ *
200
+ * @param pageModule - The error page module
201
+ * @param props - Error page props
202
+ * @param context - Render context
203
+ * @param isDev - Whether running in development mode
204
+ * @returns Rendered HTML string
205
+ */
206
+ export async function renderErrorPage(
207
+ pageModule: PageModule,
208
+ props: ErrorPageProps,
209
+ context: NitroRenderContext,
210
+ isDev: boolean
211
+ ): Promise<string> {
212
+ // The page module's default export should be a component function
213
+ const Component = pageModule.default as (props: ErrorPageProps) => unknown;
214
+
215
+ if (typeof Component !== "function") {
216
+ // Fall back to default error page if component is invalid
217
+ return generateDefaultErrorPage(props.statusCode, props.message, isDev, props.stack);
218
+ }
219
+
220
+ try {
221
+ // Render the component
222
+ // In a real implementation, this would use the SSR pipeline
223
+ // For now, we generate a basic HTML structure
224
+ const metadata = pageModule.metadata || {};
225
+
226
+ return `<!DOCTYPE html>
227
+ <html lang="en">
228
+ <head>
229
+ <meta charset="utf-8">
230
+ <meta name="viewport" content="width=device-width, initial-scale=1">
231
+ <title>${escapeHtml(String(metadata.title || `Error ${props.statusCode}`))}</title>
232
+ ${metadata.description ? `<meta name="description" content="${escapeHtml(String(metadata.description))}">` : ""}
233
+ <style>
234
+ body {
235
+ font-family: system-ui, -apple-system, sans-serif;
236
+ margin: 0;
237
+ padding: 40px;
238
+ display: flex;
239
+ align-items: center;
240
+ justify-content: center;
241
+ min-height: 100vh;
242
+ box-sizing: border-box;
243
+ background: #f5f5f5;
244
+ }
245
+ .error-page {
246
+ text-align: center;
247
+ max-width: 600px;
248
+ }
249
+ h1 {
250
+ font-size: 48px;
251
+ margin: 0 0 20px 0;
252
+ color: #333;
253
+ }
254
+ p {
255
+ color: #666;
256
+ margin: 0 0 20px 0;
257
+ }
258
+ a {
259
+ color: #0066cc;
260
+ text-decoration: none;
261
+ }
262
+ a:hover {
263
+ text-decoration: underline;
264
+ }
265
+ details {
266
+ margin-top: 20px;
267
+ text-align: left;
268
+ }
269
+ pre {
270
+ background: #1a1a1a;
271
+ color: #e0e0e0;
272
+ padding: 15px;
273
+ border-radius: 4px;
274
+ overflow-x: auto;
275
+ font-size: 12px;
276
+ }
277
+ </style>
278
+ </head>
279
+ <body>
280
+ <div id="app" data-error-page="true" data-status-code="${props.statusCode}" data-props='${escapeHtml(JSON.stringify(props))}'>
281
+ <!-- Custom error page content rendered by Avalon SSR pipeline -->
282
+ <div class="error-page">
283
+ <h1>${props.statusCode}</h1>
284
+ <p>${escapeHtml(props.message)}</p>
285
+ ${isDev && props.stack ? `
286
+ <details>
287
+ <summary>Error details</summary>
288
+ <pre>${escapeHtml(props.stack)}</pre>
289
+ </details>
290
+ ` : ""}
291
+ <a href="/">Go back home</a>
292
+ </div>
293
+ </div>
294
+ </body>
295
+ </html>`;
296
+ } catch (renderError) {
297
+ console.error("[Error Page Render Error]", renderError);
298
+ // Fall back to default error page
299
+ return generateDefaultErrorPage(props.statusCode, props.message, isDev, props.stack);
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Generates a default error page when no custom page is available
305
+ *
306
+ * @param statusCode - HTTP status code
307
+ * @param message - Error message
308
+ * @param isDev - Whether running in development mode
309
+ * @param stack - Stack trace (development only)
310
+ * @returns HTML string
311
+ */
312
+ export function generateDefaultErrorPage(
313
+ statusCode: number,
314
+ message: string,
315
+ isDev: boolean,
316
+ stack?: string
317
+ ): string {
318
+ if (isDev) {
319
+ return generateDevErrorPage(statusCode, message, stack);
320
+ }
321
+ return generateProdErrorPage(statusCode, message);
322
+ }
323
+
324
+ /**
325
+ * Generates a development error page with full details
326
+ */
327
+ function generateDevErrorPage(
328
+ statusCode: number,
329
+ message: string,
330
+ stack?: string
331
+ ): string {
332
+ return `<!DOCTYPE html>
333
+ <html lang="en">
334
+ <head>
335
+ <meta charset="utf-8">
336
+ <meta name="viewport" content="width=device-width, initial-scale=1">
337
+ <title>Error ${statusCode}</title>
338
+ <style>
339
+ body {
340
+ font-family: system-ui, -apple-system, sans-serif;
341
+ margin: 0;
342
+ padding: 40px;
343
+ background: #1a1a1a;
344
+ color: #fff;
345
+ }
346
+ .error-container {
347
+ max-width: 800px;
348
+ margin: 0 auto;
349
+ background: #2d2d2d;
350
+ padding: 40px;
351
+ border-radius: 8px;
352
+ border-left: 4px solid #ff6b6b;
353
+ }
354
+ h1 {
355
+ color: #ff6b6b;
356
+ margin-top: 0;
357
+ font-size: 24px;
358
+ }
359
+ .status-code {
360
+ font-size: 48px;
361
+ font-weight: bold;
362
+ color: #ff6b6b;
363
+ margin-bottom: 10px;
364
+ }
365
+ .message {
366
+ font-size: 18px;
367
+ color: #ccc;
368
+ margin-bottom: 20px;
369
+ }
370
+ pre {
371
+ background: #1a1a1a;
372
+ padding: 20px;
373
+ border-radius: 4px;
374
+ overflow-x: auto;
375
+ font-size: 14px;
376
+ line-height: 1.5;
377
+ color: #e0e0e0;
378
+ }
379
+ .stack-title {
380
+ color: #888;
381
+ font-size: 12px;
382
+ text-transform: uppercase;
383
+ margin-bottom: 10px;
384
+ }
385
+ a {
386
+ color: #6b9fff;
387
+ text-decoration: none;
388
+ }
389
+ a:hover {
390
+ text-decoration: underline;
391
+ }
392
+ </style>
393
+ </head>
394
+ <body>
395
+ <div class="error-container">
396
+ <div class="status-code">${statusCode}</div>
397
+ <h1>${getStatusText(statusCode)}</h1>
398
+ <p class="message">${escapeHtml(message)}</p>
399
+ ${stack ? `
400
+ <div class="stack-title">Stack Trace</div>
401
+ <pre>${escapeHtml(stack)}</pre>
402
+ ` : ""}
403
+ <p><a href="/">← Return to home</a></p>
404
+ </div>
405
+ </body>
406
+ </html>`;
407
+ }
408
+
409
+ /**
410
+ * Generates a production error page without sensitive details
411
+ */
412
+ function generateProdErrorPage(statusCode: number, message: string): string {
413
+ // Use generic message for 500 errors in production
414
+ const displayMessage = statusCode >= 500
415
+ ? "An unexpected error occurred. Please try again later."
416
+ : message;
417
+
418
+ return `<!DOCTYPE html>
419
+ <html lang="en">
420
+ <head>
421
+ <meta charset="utf-8">
422
+ <meta name="viewport" content="width=device-width, initial-scale=1">
423
+ <title>Error ${statusCode}</title>
424
+ <style>
425
+ body {
426
+ font-family: system-ui, -apple-system, sans-serif;
427
+ margin: 0;
428
+ padding: 40px;
429
+ background: #f5f5f5;
430
+ display: flex;
431
+ align-items: center;
432
+ justify-content: center;
433
+ min-height: 100vh;
434
+ box-sizing: border-box;
435
+ }
436
+ .error-container {
437
+ text-align: center;
438
+ max-width: 400px;
439
+ }
440
+ .status-code {
441
+ font-size: 72px;
442
+ font-weight: bold;
443
+ color: #333;
444
+ margin-bottom: 10px;
445
+ }
446
+ h1 {
447
+ color: #666;
448
+ font-size: 24px;
449
+ margin: 0 0 20px 0;
450
+ }
451
+ p {
452
+ color: #888;
453
+ margin: 0 0 20px 0;
454
+ }
455
+ a {
456
+ color: #0066cc;
457
+ text-decoration: none;
458
+ }
459
+ a:hover {
460
+ text-decoration: underline;
461
+ }
462
+ </style>
463
+ </head>
464
+ <body>
465
+ <div class="error-container">
466
+ <div class="status-code">${statusCode}</div>
467
+ <h1>${getStatusText(statusCode)}</h1>
468
+ <p>${escapeHtml(displayMessage)}</p>
469
+ <p><a href="/">Return to home</a></p>
470
+ </div>
471
+ </body>
472
+ </html>`;
473
+ }
474
+
475
+ /**
476
+ * Gets the status text for an HTTP status code
477
+ */
478
+ function getStatusText(statusCode: number): string {
479
+ const statusTexts: Record<number, string> = {
480
+ 400: "Bad Request",
481
+ 401: "Unauthorized",
482
+ 403: "Forbidden",
483
+ 404: "Page Not Found",
484
+ 405: "Method Not Allowed",
485
+ 408: "Request Timeout",
486
+ 410: "Gone",
487
+ 429: "Too Many Requests",
488
+ 500: "Internal Server Error",
489
+ 502: "Bad Gateway",
490
+ 503: "Service Unavailable",
491
+ 504: "Gateway Timeout",
492
+ };
493
+ return statusTexts[statusCode] || "Error";
494
+ }
495
+
496
+ /**
497
+ * Escapes HTML special characters
498
+ */
499
+ function escapeHtml(str: string): string {
500
+ return str
501
+ .replaceAll('&', "&amp;")
502
+ .replaceAll('<', "&lt;")
503
+ .replaceAll('>', "&gt;")
504
+ .replaceAll('"', "&quot;")
505
+ .replaceAll('\'', "&#039;");
506
+ }
507
+
508
+ /**
509
+ * Handles a render error and returns an appropriate response
510
+ *
511
+ * This function:
512
+ * 1. Discovers custom error pages if available
513
+ * 2. Renders the appropriate error page (custom or default)
514
+ * 3. Returns a Response with the correct status code
515
+ *
516
+ * Requirements: 10.1, 10.2, 10.3, 10.4, 10.5
517
+ *
518
+ * @param error - The error that occurred
519
+ * @param event - H3 event
520
+ * @param options - Error handler options
521
+ * @returns Response with error page
522
+ */
523
+ export async function handleRenderError(
524
+ error: Error | HttpError,
525
+ event: H3Event,
526
+ options: ErrorHandlerOptions
527
+ ): Promise<Response> {
528
+ const { isDev = false } = options;
529
+ const statusCode = isHttpError(error) ? error.statusCode : 500;
530
+ const url = getRequestURL(event);
531
+
532
+ console.error(`[Render Error] ${statusCode} - ${error.message}`, {
533
+ url: url.pathname,
534
+ stack: isDev ? error.stack : undefined,
535
+ });
536
+
537
+ // Try to discover and use custom error pages
538
+ const errorPages = await discoverErrorPages(options);
539
+ const errorPageModule = getErrorPageModule(statusCode, errorPages);
540
+
541
+ let html: string;
542
+
543
+ if (errorPageModule) {
544
+ // Render custom error page
545
+ const props = createErrorPageProps(error, url.pathname, isDev);
546
+ const context = createRenderContext(event, {});
547
+ html = await renderErrorPage(errorPageModule, props, context, isDev);
548
+ } else {
549
+ // Use default error page
550
+ html = generateDefaultErrorPage(
551
+ statusCode,
552
+ error.message,
553
+ isDev,
554
+ isDev ? error.stack : undefined
555
+ );
556
+ }
557
+
558
+ return new Response(html, {
559
+ status: statusCode,
560
+ headers: { "Content-Type": "text/html; charset=utf-8" },
561
+ });
562
+ }
563
+
564
+ /**
565
+ * Handles an API error and returns an appropriate JSON response
566
+ *
567
+ * Requirements: 10.1, 10.2, 10.4
568
+ *
569
+ * @param error - The error that occurred
570
+ * @param options - Error handler options
571
+ * @returns Response with JSON error
572
+ */
573
+ export function handleApiError(
574
+ error: Error | HttpError,
575
+ options: ErrorHandlerOptions
576
+ ): Response {
577
+ const { isDev = false } = options;
578
+ const statusCode = isHttpError(error) ? error.statusCode : 500;
579
+
580
+ console.error(`[API Error] ${statusCode} - ${error.message}`, {
581
+ stack: isDev ? error.stack : undefined,
582
+ });
583
+
584
+ const body = isDev
585
+ ? {
586
+ error: error.message,
587
+ statusCode,
588
+ stack: error.stack,
589
+ }
590
+ : {
591
+ error: statusCode >= 500 ? "Internal Server Error" : error.message,
592
+ statusCode,
593
+ };
594
+
595
+ return new Response(JSON.stringify(body), {
596
+ status: statusCode,
597
+ headers: { "Content-Type": "application/json" },
598
+ });
599
+ }
600
+
601
+ /**
602
+ * Creates a 404 Not Found response
603
+ *
604
+ * @param pathname - The requested path
605
+ * @param event - H3 event
606
+ * @param options - Error handler options
607
+ * @returns Response with 404 error page
608
+ */
609
+ export async function handleNotFound(
610
+ pathname: string,
611
+ event: H3Event,
612
+ options: ErrorHandlerOptions
613
+ ): Promise<Response> {
614
+ const error = createNotFoundError(`Page not found: ${pathname}`);
615
+ return handleRenderError(error, event, options);
616
+ }
617
+
618
+ /**
619
+ * Creates a 500 Internal Server Error response
620
+ *
621
+ * @param error - The original error
622
+ * @param event - H3 event
623
+ * @param options - Error handler options
624
+ * @returns Response with 500 error page
625
+ */
626
+ export async function handleInternalError(
627
+ error: Error,
628
+ event: H3Event,
629
+ options: ErrorHandlerOptions
630
+ ): Promise<Response> {
631
+ const httpError = createInternalError(error.message);
632
+ // Preserve the original stack trace
633
+ httpError.stack = error.stack;
634
+ return handleRenderError(httpError, event, options);
635
+ }
636
+