|
| 1 | +# Error Handler |
| 2 | + |
| 3 | +Laravel DataTables allows you to configure how you want to handle server-side errors when processing your request. |
| 4 | +Below are the options available for error handling. |
| 5 | + |
| 6 | +## ERROR CONFIGURATIONS |
| 7 | +Configuration is located at `config/datatables.php` under `error` key. |
| 8 | + |
| 9 | + |
| 10 | +- [NULL](#null-error) : `'error' => null` |
| 11 | +- [THROW](#throw-error) : `'error' => 'throw'` |
| 12 | +- [CUSTOM MESSAGE](#custom-message) : `'error' => 'Any custom friendly message'` |
| 13 | +- [TRANSLATION](#custom-message) : `'error' => 'translation.key'` |
| 14 | + |
| 15 | +<a name="null-error"></a> |
| 16 | +## NULL Error |
| 17 | +If set to `null`, the actual exception message will be used on error response. |
| 18 | + |
| 19 | +```json |
| 20 | +{ |
| 21 | + "draw": 24, |
| 22 | + "recordsTotal": 200, |
| 23 | + "recordsFiltered": 0, |
| 24 | + "data": [], |
| 25 | + "error": "Exception Message:\n\nSQLSTATE[42S22]: Column not found: 1054 Unknown column 'xxx' in 'order clause' (SQL: select * from `users` where `users`.`deleted_at` is null order by `xxx` asc limit 10 offset 0)" |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +<a name="throw-error"></a> |
| 30 | +## THROW Error |
| 31 | +If set to `'throw'`, the package will throw a `\Yajra\Datatables\Exception`. |
| 32 | +You can then use your custom error handler if needed. |
| 33 | + |
| 34 | +**Example Error Handler** |
| 35 | + |
| 36 | +Update `app\Exceptions\Handler.php` and register dataTables error exception handler. |
| 37 | + |
| 38 | +```php |
| 39 | + /** |
| 40 | + * Render an exception into an HTTP response. |
| 41 | + * |
| 42 | + * @param \Illuminate\Http\Request $request |
| 43 | + * @param \Exception $exception |
| 44 | + * @return \Illuminate\Http\Response |
| 45 | + */ |
| 46 | + public function render($request, Exception $exception) |
| 47 | + { |
| 48 | + if ($exception instanceof \Yajra\Datatables\Exception) { |
| 49 | + return response([ |
| 50 | + 'draw' => 0, |
| 51 | + 'recordsTotal' => 0, |
| 52 | + 'recordsFiltered' => 0, |
| 53 | + 'data' => [], |
| 54 | + 'error' => 'Laravel Error Handler', |
| 55 | + ]); |
| 56 | + } |
| 57 | + |
| 58 | + return parent::render($request, $exception); |
| 59 | + } |
| 60 | +``` |
| 61 | + |
| 62 | +<a name="custom-message"></a> |
| 63 | +## Custom Message |
| 64 | +If set to `'any custom message'` or `'translation.key'`, this message will be used when an error occurs when processing the request. |
| 65 | + |
| 66 | +```json |
| 67 | +{ |
| 68 | + "draw": 24, |
| 69 | + "recordsTotal": 200, |
| 70 | + "recordsFiltered": 0, |
| 71 | + "data": [], |
| 72 | + "error": "any custom message" |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | + |
0 commit comments