prisma-php 0.0.1

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.
@@ -0,0 +1,359 @@
1
+ ---
2
+ title: Error Handling
3
+ description: Learn how to handle expected errors and uncaught exceptions in Prisma PHP.
4
+ related:
5
+ title: API Reference
6
+ description: Learn more about the features mentioned on this page by reading the Prisma PHP docs.
7
+ links:
8
+ - /docs/error-handler
9
+ - /docs/route-php
10
+ - /docs/index-php
11
+ - /docs/pages-and-layouts
12
+ ---
13
+
14
+ Errors in Prisma PHP can be thought of in two broad categories:
15
+
16
+ 1. **Expected errors** — validation failures, missing records, failed form submissions, or known request problems
17
+ 2. **Unexpected errors** — uncaught exceptions, fatal errors, parse errors, or framework-level failures
18
+
19
+ Prisma PHP’s documented error model is centered around the `ErrorHandler` class, which acts as the application safety net. It captures exceptions, fatal errors, and parse errors, then converts them into structured responses depending on the request context. For AJAX-style requests it can return JSON; for browser rendering it can show a richer diagnostic UI. citeturn0view0
20
+
21
+ ## Handling expected errors
22
+
23
+ ## Validation errors are expected errors
24
+
25
+ Most validation problems should not be treated as crashes.
26
+
27
+ If the user submits invalid data, that is usually an **expected error**, not an exceptional failure. In those cases:
28
+
29
+ - sanitize and cast values with `Validator`
30
+ - apply rules with `Validator::withRules(...)`
31
+ - return field-level errors or a structured message
32
+ - only let truly unexpected failures bubble into `ErrorHandler`
33
+
34
+ This distinction is especially important for:
35
+
36
+ - `pp.fetchFunction(...)` handlers
37
+ - form submissions
38
+ - `route.php` JSON endpoints
39
+ - inline validation flows in reactive UIs
40
+
41
+ ## Prefer returning validation results instead of throwing
42
+
43
+ For normal user input, a response like this is usually better than an exception:
44
+
45
+ ```php
46
+ [
47
+ 'success' => false,
48
+ 'errors' => [
49
+ 'email' => 'A valid email address is required.',
50
+ ],
51
+ ]
52
+ ```
53
+
54
+ That shape is easier for PulsePoint UIs, API consumers, and page handlers to render predictably.
55
+
56
+ Expected errors should usually be handled explicitly in your route logic, function handlers, or forms.
57
+
58
+ In Prisma PHP, the recommended pattern is generally:
59
+
60
+ - validate input
61
+ - prefer `PP\Validator` for sanitization, casting, and rule-based checks
62
+ - return a structured response or message
63
+ - avoid letting routine validation failures become fatal exceptions
64
+
65
+ ### Form or direct function example
66
+
67
+ ```php filename="src/app/contact/index.php"
68
+ <?php
69
+
70
+ use PP\Rule;
71
+ use PP\Validator;
72
+
73
+ function submitContact($data)
74
+ {
75
+ $name = Validator::string($data->name ?? '');
76
+ $email = Validator::email($data->email ?? '');
77
+
78
+ $nameResult = Validator::withRules($name, Rule::required()->min(2)->max(80));
79
+
80
+ if ($nameResult !== true) {
81
+ return [
82
+ 'success' => false,
83
+ 'errors' => [
84
+ 'name' => $nameResult,
85
+ ],
86
+ ];
87
+ }
88
+
89
+ if ($email === null) {
90
+ return [
91
+ 'success' => false,
92
+ 'errors' => [
93
+ 'email' => 'A valid email address is required.',
94
+ ],
95
+ ];
96
+ }
97
+
98
+ return [
99
+ 'success' => true,
100
+ 'errors' => [],
101
+ 'message' => 'Form submitted successfully.',
102
+ ];
103
+ }
104
+ ```
105
+
106
+ On the frontend, you can then show the returned message instead of throwing an exception.
107
+
108
+ ### Route handler example
109
+
110
+ For API-style routes in `route.php`, expected failures can be returned as structured JSON responses.
111
+
112
+ ```php filename="src/app/users/route.php"
113
+ <?php
114
+
115
+ use PP\Request;
116
+ use PP\Rule;
117
+ use PP\Validator;
118
+
119
+ $id = Validator::int(Request::$params['id'] ?? null);
120
+ $result = Validator::withRules($id, Rule::required());
121
+
122
+ if ($result !== true || $id === null) {
123
+ echo json_encode([
124
+ 'success' => false,
125
+ 'errors' => [
126
+ 'id' => 'A valid user ID is required.',
127
+ ],
128
+ ]);
129
+ exit;
130
+ }
131
+
132
+ echo json_encode([
133
+ 'success' => true,
134
+ 'id' => $id,
135
+ ]);
136
+ ```
137
+
138
+ This is the Prisma PHP equivalent of modeling expected failures as response values instead of uncaught exceptions.
139
+
140
+ ## Handling missing content
141
+
142
+ For missing content, Prisma PHP provides a dedicated route-level file convention:
143
+
144
+ - `not-found.php`
145
+
146
+ Use it when a route or resource should render a not-found UI instead of crashing the application.
147
+
148
+ Example route structure:
149
+
150
+ ```txt
151
+ src/app/blog/
152
+ ├── index.php
153
+ ├── not-found.php
154
+ └── [slug]/
155
+ └── index.php
156
+ ```
157
+
158
+ Example page logic:
159
+
160
+ ```php filename="src/app/blog/[slug]/index.php"
161
+ <?php
162
+
163
+ use PP\Request;
164
+ use Lib\Prisma\Classes\Prisma;
165
+
166
+ $slug = Request::$dynamicParams['slug'] ?? null;
167
+
168
+ $prisma = Prisma::getInstance();
169
+ $post = $prisma->post->findFirst([
170
+ 'where' => [
171
+ 'slug' => $slug,
172
+ ],
173
+ ]);
174
+
175
+ if (!$post) {
176
+ include APP_PATH . '/blog/not-found.php';
177
+ return;
178
+ }
179
+
180
+ echo "<h1>" . htmlspecialchars($post['title']) . "</h1>";
181
+ ```
182
+
183
+ And the not-found view:
184
+
185
+ ```php filename="src/app/blog/not-found.php"
186
+ <div>404 - Page Not Found</div>
187
+ ```
188
+
189
+ ## Handling uncaught exceptions
190
+
191
+ Unexpected errors should not be silently ignored. Prisma PHP’s `ErrorHandler` is designed to catch these failures and render the right kind of response.
192
+
193
+ The docs describe `ErrorHandler` as the safety net for the application. It captures:
194
+
195
+ - exceptions
196
+ - fatal errors
197
+ - parse errors citeturn0view0
198
+
199
+ This makes it the Prisma PHP equivalent of a central application error boundary, but implemented at the PHP framework level rather than with React client components.
200
+
201
+ ## Request-context aware output
202
+
203
+ Prisma PHP’s `ErrorHandler` automatically detects the request context and chooses the appropriate output format. The docs explicitly say it can return:
204
+
205
+ - a JSON payload for AJAX requests
206
+ - a rich diagnostic UI for browser requests citeturn0view0
207
+
208
+ This is one of the biggest differences from Next.js. Instead of defining client-side error boundaries with `error.js`, Prisma PHP handles failures centrally on the server and formats the response according to the request type.
209
+
210
+ ## Rich diagnostics
211
+
212
+ The Prisma PHP docs emphasize that `ErrorHandler` does more than show a generic PHP crash page. It can generate context-aware diagnostics for framework-specific failures. citeturn0view0
213
+
214
+ ### Component validation diagnostics
215
+
216
+ When a component receives invalid props, the docs say the error UI can show:
217
+
218
+ - the specific component name
219
+ - the invalid property
220
+ - quick fixes such as adding a missing public property
221
+ - a list of currently available props citeturn0view0
222
+
223
+ ### Template compilation diagnostics
224
+
225
+ When the template engine fails to parse a file, the docs say the error UI can:
226
+
227
+ - identify syntax errors in PulsePoint-specific tags
228
+ - highlight the specific line in the view file
229
+ - provide suggestions on how to correct the syntax citeturn0view0
230
+
231
+ This means Prisma PHP’s error page is intended to be an actionable developer tool, not only a fallback screen.
232
+
233
+ ## Customizing the error view
234
+
235
+ Prisma PHP allows you to override the default error output by creating:
236
+
237
+ ```php
238
+ APP_PATH . '/error.php'
239
+ ```
240
+
241
+ The docs state that if this file exists, `ErrorHandler` will render it inside your main layout wrapper instead of using the default output. citeturn0view0
242
+
243
+ That makes `error.php` the main file convention for customizing browser-visible application errors.
244
+
245
+ Example:
246
+
247
+ ```php filename="src/app/error.php"
248
+ <section class="container">
249
+ <h1>Something went wrong</h1>
250
+ <p>Please try again later.</p>
251
+ </section>
252
+ ```
253
+
254
+ ## Output buffering behavior
255
+
256
+ The docs note an important implementation detail: when a fatal error occurs, the handler calls `ob_end_clean()` to remove any partial HTML that was generated before the crash. This prevents broken layouts or incomplete output from being rendered. citeturn0view0
257
+
258
+ The docs also mention that if you are debugging with `echo` or `var_dump` immediately before a crash, you may need to temporarily disable this buffer cleaning in `modifyOutputLayoutForError` in order to see that debug output. citeturn0view0
259
+
260
+ ## Key methods
261
+
262
+ The Prisma PHP docs call out several important `ErrorHandler` methods.
263
+
264
+ ### `registerHandlers(): void`
265
+
266
+ Registers:
267
+
268
+ - `set_exception_handler`
269
+ - `register_shutdown_function`
270
+ - `set_error_handler`
271
+
272
+ The docs say this is usually called in `bootstrap.php`. citeturn0view0
273
+
274
+ ### `checkFatalError(): void`
275
+
276
+ Manually checks `error_get_last()`. The docs describe this as useful for catching shutdown-time errors that are not regular exceptions. citeturn0view0
277
+
278
+ ### `formatExceptionForDisplay(Throwable $e): string`
279
+
280
+ Formats an exception into the rich HTML diagnostic output. The docs say it determines whether the error is a standard PHP exception or a specific PulsePoint framework error. citeturn0view0
281
+
282
+ ## Bootstrap registration
283
+
284
+ Because `ErrorHandler` is typically framework-level, it is usually registered in the bootstrap process.
285
+
286
+ ```php filename="bootstrap.php"
287
+ <?php
288
+
289
+ use PP\ErrorHandler;
290
+
291
+ ErrorHandler::registerHandlers();
292
+ ```
293
+
294
+ This ensures the framework catches application failures consistently.
295
+
296
+ ## Manual usage in try/catch blocks
297
+
298
+ The Prisma PHP docs also provide a manual usage pattern for custom try/catch flows. You can format the exception and output it through the standard error layout.
299
+
300
+ ```php filename="src/app/admin/index.php"
301
+ <?php
302
+
303
+ use PP\ErrorHandler;
304
+ use PP\PHPX\Exceptions\ComponentValidationException;
305
+
306
+ try {
307
+ // Some complex logic
308
+ throw new ComponentValidationException("Missing required prop 'id'", 500);
309
+ } catch (Throwable $e) {
310
+ $html = ErrorHandler::formatExceptionForDisplay($e);
311
+ ErrorHandler::modifyOutputLayoutForError($html);
312
+ }
313
+ ```
314
+
315
+ This is useful when you want to catch an exception yourself but still use the framework’s standard rich diagnostic output. citeturn0view0
316
+
317
+ ## `index.php` vs `route.php` error handling
318
+
319
+ Error handling in Prisma PHP still follows the route model:
320
+
321
+ - in `index.php`, errors usually affect rendered page output
322
+ - in `route.php`, errors usually affect JSON or direct handler responses
323
+ - the `ErrorHandler` adapts the response format based on request context citeturn0view0
324
+
325
+ For full-stack projects, expected user-facing problems should usually be handled gracefully in `index.php` or in direct function responses. For API-style routes, structured JSON error responses are usually the right choice for expected failures. Uncaught exceptions should be left to the framework-level `ErrorHandler`.
326
+
327
+ ## Prisma PHP vs Next.js mental model
328
+
329
+ If you are coming from Next.js, the closest mapping is:
330
+
331
+ | Next.js idea | Prisma PHP equivalent |
332
+ | ---------------------------------------------- | --------------------------------------------- |
333
+ | `error.js` route boundary | central `ErrorHandler` + custom `error.php` |
334
+ | `not-found.js` | `not-found.php` |
335
+ | server-side returnable validation errors | structured PHP return values or JSON payloads |
336
+ | uncaught exceptions caught by React boundaries | uncaught exceptions trapped by `ErrorHandler` |
337
+ | custom fallback error UI | `APP_PATH . '/error.php'` |
338
+
339
+ The main difference is that Prisma PHP’s documented error handling is server-driven and centralized, rather than client-boundary driven.
340
+
341
+ ## Good to know
342
+
343
+ - Prisma PHP’s documented error system is `ErrorHandler`. citeturn0view0
344
+ - It captures exceptions, fatal errors, and parse errors. citeturn0view0
345
+ - It automatically switches output based on request context. citeturn0view0
346
+ - AJAX-style requests can receive JSON errors. citeturn0view0
347
+ - Browser requests can receive a rich diagnostic UI. citeturn0view0
348
+ - You can override the default error view with `APP_PATH . '/error.php'`. citeturn0view0
349
+ - Fatal-error handling clears partial output with `ob_end_clean()`. citeturn0view0
350
+ - `registerHandlers()` is usually called in `bootstrap.php`. citeturn0view0
351
+
352
+ ## Validation vs unexpected failures
353
+
354
+ A useful rule is:
355
+
356
+ - invalid user input → handle it explicitly with `Validator` and return a normal response
357
+ - broken database connection, parser failure, uncaught exception → let `ErrorHandler` handle it
358
+
359
+ This keeps your app predictable for users and keeps the rich framework diagnostics focused on actual failures rather than routine form mistakes.