-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge-error-handler.html
More file actions
535 lines (518 loc) · 27.6 KB
/
Copy pathforge-error-handler.html
File metadata and controls
535 lines (518 loc) · 27.6 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<!doctype html>
<html lang="en" class="scroll-smooth">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Forge Kernel - ForgeErrorHandler</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
<link
href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css"
rel="stylesheet"
/>
<link
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
rel="stylesheet"
/>
<link rel="stylesheet" href="assets/css/docs.css" />
</head>
<body class="bg-gray-50 text-gray-900">
<!-- Navigation -->
<nav class="bg-white shadow-sm border-b">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between h-16">
<div class="flex items-center">
<div class="flex-shrink-0">
<h1 class="text-xl font-bold text-gray-900">
Forge Kernel
</h1>
</div>
<div class="hidden sm:ml-6 sm:flex sm:space-x-8">
<a
href="index.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600"
>
Home
</a>
<a
href="getting-started.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600"
>
Getting Started
</a>
<a
href="core-concepts.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600"
>
Core Concepts
</a>
<a
href="capabilities.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600"
>
Capabilities
</a>
<a
href="api-reference.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600"
>
API Reference
</a>
<a
href="tutorial.html"
class="text-gray-900 inline-flex items-center px-1 pt-1 text-sm font-medium hover:text-blue-600"
>
Tutorials
</a>
</div>
</div>
<div class="flex items-center sm:hidden">
<button
id="mobile-menu-button"
class="text-gray-700 hover:text-blue-600 p-2"
>
<i class="fas fa-bars text-xl"></i>
</button>
</div>
</div>
</div>
<div
id="mobile-menu"
class="sm:hidden hidden bg-white border-t border-gray-200"
>
<div class="px-2 pt-2 pb-3 space-y-1">
<a
href="index.html"
class="block px-3 py-2 text-gray-900 hover:text-blue-600"
>Home</a
>
<a
href="getting-started.html"
class="block px-3 py-2 text-gray-900 hover:text-blue-600"
>Getting Started</a
>
<a
href="core-concepts.html"
class="block px-3 py-2 text-gray-900 hover:text-blue-600"
>Core Concepts</a
>
<a
href="capabilities.html"
class="block px-3 py-2 text-gray-900 hover:text-blue-600"
>Capabilities</a
>
<a
href="api-reference.html"
class="block px-3 py-2 text-gray-900 hover:text-blue-600"
>API Reference</a
>
<a
href="tutorial.html"
class="block px-3 py-2 text-gray-900 hover:text-blue-600"
>Tutorials</a
>
</div>
</div>
</nav>
<!-- Main Content -->
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<div class="flex flex-col lg:flex-row gap-8">
<!-- Sidebar Navigation -->
<div id="sidebar-nav" class="lg:w-1/4">
<div class="bg-white rounded-lg shadow-sm p-6 sticky top-8">
<h3 class="text-lg font-semibold text-gray-900 mb-4">
ForgeErrorHandler
</h3>
<nav class="space-y-2">
<a
href="#overview"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>Overview</a
>
<a
href="#when-to-use"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>When to Reach for It</a
>
<a
href="#installation"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>Installation</a
>
<a
href="#how-it-works"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>How It Works</a
>
<a
href="#debug-vs-production"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>Debug vs Production</a
>
<a
href="#api-requests"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>API Requests</a
>
<a
href="#logging"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>Error Logging</a
>
<a
href="#writing-your-own"
class="nav-link block px-3 py-2 text-sm text-gray-700 rounded-md hover:text-blue-600"
>Writing Your Own</a
>
</nav>
<div class="mt-6 pt-6 border-t border-gray-200">
<h4 class="font-semibold text-sm mb-3 text-blue-600">
Related
</h4>
<nav class="space-y-2">
<a
href="capabilities.html"
class="block text-gray-600 hover:text-blue-600 py-1 text-sm"
>All capabilities →</a
>
<a
href="core-concepts.html"
class="block text-gray-600 hover:text-blue-600 py-1 text-sm"
>Core Concepts →</a
>
</nav>
</div>
</div>
</div>
<!-- Main Content -->
<div class="lg:w-3/4">
<div class="bg-white rounded-lg shadow-sm p-8">
<h1 class="text-4xl font-bold text-gray-900 mb-6">
ForgeErrorHandler
</h1>
<p class="text-xl text-gray-600 mb-4">
A component that handles errors in <strong>your
app</strong>: it turns uncaught exceptions and PHP
errors into clean responses — a rich debug page while
you develop, a friendly page (or JSON) in production —
and logs what went wrong along the way.
</p>
<!-- Overview -->
<section id="overview" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">
Overview
</h2>
<p class="text-gray-600 mb-4">
This component plugs into the error-handling
contract used by
<em>your web app</em>. It implements
<code>ErrorHandlerInterface</code> — the contract
that turns a <code>Throwable</code> and the current
request into a <code>Response</code> — so you don't
have to write that plumbing yourself. You still
decide how your app behaves on the other side of an
error.
</p>
<div
class="bg-blue-50 border border-blue-200 rounded-lg p-6 mb-6"
>
<h3 class="font-semibold text-lg mb-4">
What it gives you
</h3>
<div class="grid md:grid-cols-2 gap-4 text-sm text-gray-700">
<ul class="space-y-2 list-disc list-inside">
<li>Catches exceptions and PHP errors</li>
<li>Debug page with code snippets and trace</li>
<li>Production page that hides internals</li>
<li>JSON responses for API routes</li>
</ul>
<ul class="space-y-2 list-disc list-inside">
<li>Logs to a PSR-3 logger or a file</li>
<li>Masks passwords, tokens, and secrets</li>
<li>CLI error handling for commands</li>
</ul>
</div>
</div>
<div
class="bg-green-50 border-l-4 border-green-400 p-4"
>
<p class="text-sm text-green-700">
<strong>Plugs into ForgeRouter:</strong>
ForgeErrorHandler implements
<code>Modules\ForgeRouter\Contracts\ErrorHandlerInterface</code>,
so it works with the web layer rather than the
Kernel alone. It ships core
(<code>core: true</code>, loaded at
<code>order: 2</code>).
</p>
</div>
</section>
<!-- When to Reach for It -->
<section id="when-to-use" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">
When to Reach for It
</h2>
<p class="text-gray-600 mb-4">
Use it for any app that serves web requests and
wants sensible, safe error responses:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-4">
<li>
You want detailed errors in development but a
clean, leak-free page in production.
</li>
<li>
Your API should return JSON errors instead of an
HTML page.
</li>
<li>
You want every failure logged with context
(request, trace, timing) without wiring it by hand.
</li>
</ul>
<p class="text-gray-600">
If you're building a pure CLI/worker tool with no web
layer, this component has less to offer — the Kernel's
own CLI error handling may be enough.
</p>
</section>
<!-- Installation -->
<section id="installation" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">
Installation
</h2>
<p class="text-gray-600 mb-4">
A core capability, so usually present. To add it to a
project:
</p>
<div
class="code-block p-6 text-white rounded-lg mb-6"
>
<pre><code class="language-bash">php forge.php package:install-module --module=ForgeErrorHandler</code></pre>
</div>
<p class="text-gray-600">
It depends on the router's error contract, so it's
expected to be used alongside the web layer.
</p>
</section>
<!-- How It Works -->
<section id="how-it-works" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">
How It Works
</h2>
<p class="text-gray-600 mb-4">
On setup the component registers PHP's standard
handlers, then routes everything through its
<code>handle()</code> entry point:
</p>
<ul class="list-disc list-inside space-y-2 text-gray-600 mb-6">
<li>
<strong>Error handler</strong> — PHP warnings and
notices are converted to an
<code>ErrorException</code>.
</li>
<li>
<strong>Exception handler</strong> — uncaught
exceptions produce a response (or CLI error) and
exit.
</li>
<li>
<strong>Shutdown handler</strong> — fatal errors
(memory, parse, compile) are caught on shutdown.
</li>
</ul>
<p class="text-gray-600">
Every handled error is logged first, then turned into
a response whose shape depends on your
<code>APP_DEBUG</code> setting and whether the request
is an API call.
</p>
</section>
<!-- Debug vs Production -->
<section
id="debug-vs-production"
class="section-anchor mb-12"
>
<h2 class="text-2xl font-bold text-gray-900 mb-4">
Debug vs Production
</h2>
<p class="text-gray-600 mb-4">
The behavior flips on your
<code>APP_DEBUG</code> environment setting:
</p>
<div class="overflow-x-auto mb-6">
<table class="min-w-full divide-y divide-gray-200 border border-gray-200 rounded-lg overflow-hidden">
<thead class="bg-gray-50"><tr>
<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Mode</th>
<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">HTML (browser)</th>
<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">API (JSON)</th>
</tr></thead>
<tbody class="bg-white divide-y divide-gray-200">
<tr>
<td class="px-4 py-3 text-sm font-medium text-gray-900">Debug (<code>APP_DEBUG=true</code>)</td>
<td class="px-4 py-3 text-sm text-gray-600">Rich page: type, message, code snippets, filtered trace, request/session data.</td>
<td class="px-4 py-3 text-sm text-gray-600">Pretty JSON with the full error and trace.</td>
</tr>
<tr>
<td class="px-4 py-3 text-sm font-medium text-gray-900">Production (<code>APP_DEBUG=false</code>)</td>
<td class="px-4 py-3 text-sm text-gray-600">Friendly 500 page; internals never exposed.</td>
<td class="px-4 py-3 text-sm text-gray-600">Minimal JSON: a 500 "An error occurred" message.</td>
</tr>
</tbody>
</table>
</div>
<p class="text-gray-600">
PII and secrets are masked in both modes — keys like
<code>password</code>, <code>token</code>,
<code>secret</code>, <code>authorization</code>, and
<code>cookie</code> are shown as masked values.
</p>
</section>
<!-- API Requests -->
<section id="api-requests" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">
API Requests
</h2>
<p class="text-gray-600 mb-4">
A request is treated as an API call when it signals
JSON — the <code>Accept</code> or
<code>Content-Type</code> header contains
<code>application/json</code>, or the URI starts with
<code>/api</code>. API calls always get JSON errors
instead of an HTML page.
</p>
<div
class="code-block p-6 text-white rounded-lg mb-6"
>
<pre><code class="language-json">{
"error": {
"message": "An error occurred. Please try again later.",
"type": "InternalServerError",
"code": 500
}
}</code></pre>
</div>
</section>
<!-- Error Logging -->
<section id="logging" class="section-anchor mb-12">
<h2 class="text-2xl font-bold text-gray-900 mb-4">
Error Logging
</h2>
<p class="text-gray-600 mb-4">
Every handled error is logged with rich context. If a
PSR-3-compatible logger is available it's used;
otherwise the component writes to
<code>storage/logs/errors.log</code>.
</p>
<p class="text-gray-600 mb-4">
Each entry captures the fingerprint, a request id,
the exception class and message, file and line, the
trace, peak memory, duration, source (client IP,
method, URI — or the CLI command), user agent, and
session/query/post data:
</p>
<div
class="code-block p-6 text-white rounded-lg mb-4"
>
<pre><code class="language-text">[2026-08-27 12:00:00] 9f1c2a3b4d5e6f7 [ab12cd34] RuntimeException – src/App/Controller.php:42 | #0 ... | Something broke</code></pre>
</div>
<p class="text-gray-600">
Logging is rate-limited per error fingerprint, so a
repeating failure doesn't flood the log.
</p>
</section>
<!-- Writing Your Own -->
<section
id="writing-your-own"
class="section-anchor mb-12"
>
<h2 class="text-2xl font-bold text-gray-900 mb-4">
Writing Your Own
</h2>
<p class="text-gray-600 mb-4">
This is a component, not a cage. If your app wants its
own behavior — different pages, custom logging, a
different response format — implement the same
contract yourself:
</p>
<div
class="code-block p-6 text-white rounded-lg mb-6"
>
<pre><code class="language-php">use Modules\ForgeRouter\Contracts\ErrorHandlerInterface;
use Modules\ForgeRouter\Http\Request;
use Modules\ForgeRouter\Http\Response;
use Throwable;
class MyErrorHandler implements ErrorHandlerInterface
{
public function handle(Throwable $e, Request $request): Response
{
// Log, format, and return whatever your app needs
return new Response('Something went wrong', 500);
}
}
</code></pre>
</div>
<p class="text-gray-600">
Anything implementing the interface can be bound in
its place — your handler, or another capability.
</p>
</section>
</div>
</div>
</div>
</div>
<!-- Footer -->
<footer class="bg-gray-900 text-white py-8 mt-16">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 text-center">
<div class="mb-4">
<span class="text-lg font-bold">Forge Kernel</span>
</div>
<p class="text-gray-400">
A toolbox for builders who like to do things their way.
</p>
</div>
</footer>
<script>
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
anchor.addEventListener("click", function (e) {
e.preventDefault();
const target = document.querySelector(
this.getAttribute("href"),
);
if (target) {
target.scrollIntoView({
behavior: "smooth",
block: "start",
});
}
});
});
const mobileMenuButton =
document.getElementById("mobile-menu-button");
const mobileMenu = document.getElementById("mobile-menu");
if (mobileMenuButton && mobileMenu) {
mobileMenuButton.addEventListener("click", function () {
mobileMenu.classList.toggle("hidden");
});
}
window.addEventListener("scroll", function () {
const sections = document.querySelectorAll(".section-anchor");
const navLinks = document.querySelectorAll(".nav-link");
let current = "";
sections.forEach((section) => {
const sectionTop = section.offsetTop - 100;
if (window.pageYOffset >= sectionTop) {
current = section.getAttribute("id");
}
});
navLinks.forEach((link) => {
link.classList.remove("active");
if (link.getAttribute("href") === "#" + current) {
link.classList.add("active");
}
});
});
</script>
</body>
</html>