|
1 | | -# Error Handling |
| 1 | +# Error Handling & Debugging |
2 | 2 |
|
3 | 3 | <!-- markdownlint-disable no-inline-html --> |
4 | 4 |
|
5 | | -It's super hard to get everything right the first time. Sometimes, you might encounter errors in your application. Leaf has a built-in error handler that displays a custom error page when an error occurs. It gives you some context about the error and what might have caused it. |
| 5 | +It's super hard to get everything right the first time, trust us, we know! This could be due to typos, wrong logic, or other unforeseen issues from external services. In such cases, it's important to handle errors gracefully and provide useful feedback to users. |
| 6 | + |
| 7 | +## Error Screens |
| 8 | + |
| 9 | +When an error occurs in your Leaf application, you want to make sure that you see a friendly error message with a trace instead of a raw error dump. This error screen gives you some context about the error and what might have caused it. |
6 | 10 |
|
7 | 11 | <img src="https://github.com/user-attachments/assets/52f044bb-bb9b-4fdd-a3d7-835ac7e1f085" alt="Error Page" width="100%" class="border border-gray-500 rounded-lg"> |
8 | 12 |
|
9 | | -In production, you might want to turn off error reporting to prevent users from seeing errors. You can do this by setting the `debug` config to `false`. |
| 13 | +It also adds information about your application's current context: |
10 | 14 |
|
11 | | -```php |
| 15 | +- Application environment (development, production, etc.) |
| 16 | +- Request information (server data, method, headers, etc.) |
| 17 | +- Files, cookies, session info, and more. |
| 18 | + |
| 19 | +Which is why we recommend that you always turn off error reporting in production, so you don't accidentally leak sensitive information about your application. |
| 20 | + |
| 21 | +## Disabling Error Reporting |
| 22 | + |
| 23 | +While Leaf's detailed error reporting is super useful during development, it's not something you want to use in production, as it can expose sensitive information about your application. You can disable error reporting by setting the `debug` config to `false` or by setting the `APP_DEBUG` environment variable to `false` in Leaf MVC. |
| 24 | + |
| 25 | +::: code-group |
| 26 | + |
| 27 | +```php:no-line-numbers [Leaf] |
12 | 28 | app()->config([ |
13 | 29 | 'debug' => false |
14 | 30 | ]); |
15 | 31 | ``` |
16 | 32 |
|
| 33 | +```env:no-line-numbers [Leaf MVC] |
| 34 | +APP_DEBUG=false |
| 35 | +``` |
| 36 | + |
| 37 | +::: |
| 38 | + |
17 | 39 | When you set `debug` to `false`, Leaf will automatically turn off error reporting and display a custom error page to users. You can customize this page using Leaf's `setErrorHandler()` method. |
18 | 40 |
|
19 | | -```php |
| 41 | +```php:no-line-numbers |
20 | 42 | app()->setErrorHandler(function () { |
21 | 43 | echo "<h1>My custom error page</h1>"; |
22 | 44 | }); |
23 | 45 | ``` |
24 | 46 |
|
25 | | -## Handling 404 |
| 47 | +We understand that you might want to enable debugging in production for some reason, however, doing that can expose sensitive information about your app, which can be used by attackers to exploit your app. If you truly need to debug in production, you should turn to logging instead. |
| 48 | + |
| 49 | +## Logging |
| 50 | + |
| 51 | +Logs are records of events in your application. They capture significant things like errors, requests, or user actions, helping you track your app's behavior. Log files are essential for debugging and understanding production issues. A typical log file looks like this: |
| 52 | + |
| 53 | +```log{4-5} |
| 54 | +[2021-03-31 22:44:53] |
| 55 | +ERROR - ErrorException: Trying to access array offset on value of type int in /home/mychi/Projects/leafphp/leaf/src/Experimental/Cache.php:83 |
| 56 | +Stack trace: |
| 57 | +#0 /home/mychi/Projects/leafphp/leaf/src/Experimental/Cache.php(83): Leaf\Exception\General::handleErrors() |
| 58 | +#1 /home/mychi/Projects/leafphp/leaf/test/index.php(45): Leaf\Experimental\Cache::get() |
| 59 | +#2 [internal function]: {closure}() |
| 60 | +#3 /home/mychi/Projects/leafphp/leaf/src/Router.php(337): call_user_func_array() |
| 61 | +#4 /home/mychi/Projects/leafphp/leaf/src/Router.php(392): Leaf\Router::invoke() |
| 62 | +#5 /home/mychi/Projects/leafphp/leaf/src/Router.php(443): Leaf\Router::handle() |
| 63 | +#6 /home/mychi/Projects/leafphp/leaf/src/App.php(863): Leaf\Router::run() |
| 64 | +#7 /home/mychi/Projects/leafphp/leaf/test/index.php(52): Leaf\App->run() |
| 65 | +#8 {main} |
| 66 | +``` |
| 67 | + |
| 68 | +Leaf offers a user-friendly logger for logging errors and other stuff in your app. It's integrated with Leaf's core, so no initialization is required, and you may not need to use the logger directly. |
| 69 | + |
| 70 | +::: details Manually Installing Logger |
| 71 | + |
| 72 | +Leaf's logger is included by default when you create a new Leaf MVC project, but if you're using Leaf without Leaf MVC, or previously uninstalled the logger module, you can manually install it by following the steps below. |
| 73 | + |
| 74 | +::: code-group |
| 75 | + |
| 76 | +```bash:no-line-numbers [Leaf CLI] |
| 77 | +leaf install logger |
| 78 | +``` |
| 79 | + |
| 80 | +```bash:no-line-numbers [Composer] |
| 81 | +composer require leafs/logger |
| 82 | +``` |
26 | 83 |
|
27 | | -Leaf displays a default 404 screen when it can't find a page that a user wants to access in your app, however this page may not match your app's design or you may want to return JSON instead of HTML. |
| 84 | +::: |
28 | 85 |
|
29 | | -<img alt="404 page" src="https://github.com/user-attachments/assets/97073d77-1298-4549-aca0-7b652dd2aa0f" width="100%" class="border border-gray-500 rounded-lg"> |
| 86 | +::: details Configuration without Leaf MVC |
30 | 87 |
|
31 | | -You can customize the 404 page using Leaf's `set404()` method. |
| 88 | +Once you have installed the logger module, you need to tell Leaf to log all exceptions/errors. You can do this simply by enabling the `log.enabled` configuration option. |
32 | 89 |
|
33 | 90 | ```php |
34 | | -app()->set404(function () { |
35 | | - response()->json([ |
36 | | - "error" => "Page not found" |
37 | | - ]); |
38 | | -}); |
| 91 | +app()->config([ |
| 92 | + 'log.enabled' => true |
| 93 | +]); |
| 94 | +``` |
| 95 | + |
| 96 | +You also need to tell Leaf which directory to save logs into. By default, Leaf saves logs in the `logs` directory in your app's root directory. You can change this by setting the `log.dir` configuration option: |
| 97 | + |
| 98 | +```php |
| 99 | +app()->config([ |
| 100 | + 'log.enabled' => true, |
| 101 | + 'log.dir' => __DIR__ . '/logs/' |
| 102 | +]); |
| 103 | +``` |
| 104 | + |
| 105 | +All logs will be saved in a `log.txt` file in the directory you specify. You can also specify a custom log file name by setting the `log.file` configuration option: |
| 106 | + |
| 107 | +```php{4} |
| 108 | +app()->config([ |
| 109 | + 'log.enabled' => true, |
| 110 | + 'log.dir' => __DIR__ . '/logs/', |
| 111 | + 'log.file' => 'app.log' |
| 112 | +]); |
| 113 | +``` |
| 114 | + |
| 115 | +::: |
| 116 | + |
| 117 | +::: details Usage with Leaf MVC |
| 118 | + |
| 119 | +If you are using Leaf MVC, the logger is already installed and configured for you, so you don't need to do anything. By default, Leaf MVC saves logs in the `storage/logs` directory in your app's root directory. |
| 120 | + |
| 121 | +Whenever you run into an error or exception, Leaf will automatically log it for you, even if error reporting is disabled. |
| 122 | + |
| 123 | +If you decide to disable logging for your app, you can simply get rid of the logger module by running: |
| 124 | + |
| 125 | +```bash:no-line-numbers |
| 126 | +leaf uninstall logger |
| 127 | +``` |
| 128 | + |
| 129 | +That's it! Leaf will no longer log errors or exceptions for your app. |
| 130 | + |
| 131 | +::: |
| 132 | + |
| 133 | +## Leaf DevTools <Badge type="warning" text="BETA" /> |
| 134 | + |
| 135 | +Leaf provides DevTools to give you more insight into your app than you can get from the error page. It has a beautiful and intuitive interface that give you information about your Leaf application, and a light-weight library that you can use to interact with the devtools frontend. |
| 136 | + |
| 137 | +<img src="https://user-images.githubusercontent.com/26604242/235434208-82ccdd87-6289-43fd-b93b-5fa09e6acd20.jpg" alt="Error Page" width="100%" class="border border-gray-500 rounded-lg"> |
| 138 | + |
| 139 | +To get started with the DevTools, you need to install the Leaf DevTools module: |
| 140 | + |
| 141 | +::: code-group |
| 142 | + |
| 143 | +```bash:no-line-numbers [Leaf CLI] |
| 144 | +leaf install devtools |
| 145 | +``` |
| 146 | + |
| 147 | +```bash:no-line-numbers [Composer] |
| 148 | +composer require leafs/devtools |
| 149 | +``` |
| 150 | + |
| 151 | +::: |
| 152 | + |
| 153 | +After installing the devtools module, you need to add the hook to your app. This will register the devtools routes and allow your Leaf app to communicate with the DevTools. You can do this by adding this line to your app root. |
| 154 | + |
| 155 | +```php{5} |
| 156 | +<?php |
| 157 | +
|
| 158 | +require __DIR__ . "/vendor/autoload.php"; |
| 159 | +
|
| 160 | +\Leaf\DevTools::install(); |
| 161 | +
|
| 162 | +... |
| 163 | +``` |
| 164 | + |
| 165 | +From there, you can access the DevTools by visiting `<your-app-url>/leafDevTools`. The DevTools will show you information about your app, like the routes, the request and response, and the environment variables. You can also use the DevTools to interact with your app, like making requests to your app and seeing the response. |
| 166 | + |
| 167 | +### Server Debug Logs |
| 168 | + |
| 169 | +When working with JavaScript, you can use `console.log` to log information to the console. In PHP, you can use `echo` or `var_dump` to log information to the browser. However, this can be a bit cumbersome, especially when you're working with APIs or other server-side code. Leaf provides a `log` function that you can use to log information to the server. This is useful for debugging your app in a non-invaisive way. |
| 170 | + |
| 171 | +```php |
| 172 | +\Leaf\DevTools::console('This data should be logged in the console'); |
39 | 173 | ``` |
40 | 174 |
|
41 | | -Once this is set, Leaf will automatically use your custom 404 page when a user tries to access a page that doesn't exist in your app. |
| 175 | +Adding this line to your code will log the data to the Leaf DevTools console without affecting the output of your app. This allows you to debug your app while going through the normal flow of your app. |
| 176 | + |
| 177 | +```php |
| 178 | +\Leaf\DevTools::console('This data should be logged in the console'); |
| 179 | +\Leaf\DevTools::console('This is a warning', 'warn'); |
| 180 | +\Leaf\DevTools::console('This is an error', 'error'); |
| 181 | +\Leaf\DevTools::console('This is an info message', 'info'); |
| 182 | +\Leaf\DevTools::console('This is a debug message', 'log'); |
| 183 | +``` |
| 184 | + |
| 185 | +These will output different colored messages in the console: |
| 186 | + |
| 187 | +<img src="https://github.com/leafsphp/devtools/assets/26604242/195e15b1-d063-4cf2-a817-5a60e8ba184d" alt="Console page" width="100%" class="border border-gray-500 rounded-lg"> |
| 188 | + |
| 189 | +***Leaf will only allow access to the DevTools when the app is in a development environment, but not every hosting provider sets the environment to `production` automatically. To be safe, we recommend uninstalling the DevTools module before deploying your app.*** |
42 | 190 |
|
43 | 191 | ## Maintenance Mode |
44 | 192 |
|
|
0 commit comments