Skip to content

Commit bfba7e7

Browse files
Merge pull request #187 from MarcinOrlowski/ex-handler-186
ExHandler now expects \Throwable instead of \Exception.
2 parents 27dc16f + 7964f0e commit bfba7e7

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

docs/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
99
## CHANGE LOG ##
1010

11+
* @dev
12+
* [RB-186] ExceptionHandler now expects `\Throwable` instead of `\Exception`.
13+
1114
* v9.2.0 (2020-12-27)
1215
* Updated Travis config to run tests on PHP 8 too.
1316
* Added Arabic translation (thanks to @mustafa-online)

docs/exceptions.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
* [Possible Exception Handler conflicts](#possible-exception-handler-conflicts)
1414
* [ApiCodes](#apicodes)
1515

16+
---
17+
18+
> ![WARNING](img/warning.png) Use of provided `ExceptionHandler` helper **requires** additional, **manual**
19+
> installation steps to be made, otherwise Laravel's built-in handler will be used instead. See the details
20+
> documented below.
21+
1622
---
1723

1824
# Exception Handling with Response Builder #
@@ -41,7 +47,7 @@ use MarcinOrlowski\ResponseBuilder\ExceptionHandlerHelper;
4147
Default handler as of Laravel 5.2 has been significantly simplified and by default it looks like this:
4248

4349
```php
44-
public function render($request, Exception $e)
50+
public function render($request, Throwable $e)
4551
{
4652
return parent::render($request, $e);
4753
}
@@ -50,7 +56,7 @@ public function render($request, Exception $e)
5056
After your edit it shall look like this:
5157

5258
```php
53-
public function render($request, Exception $e)
59+
public function render($request, Throwable $e)
5460
{
5561
return ExceptionHandlerHelper::render($request, $e);
5662
}

src/ExceptionHandlerHelper.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @link https://github.com/MarcinOrlowski/laravel-api-response-builder
1515
*/
1616

17-
use Exception;
17+
use Throwable;
1818
use Illuminate\Auth\AuthenticationException as AuthException;
1919
use Illuminate\Support\Facades\Config;
2020
use Illuminate\Support\Facades\Lang;
@@ -33,11 +33,11 @@ class ExceptionHandlerHelper
3333
* Render an exception into valid API response.
3434
*
3535
* @param \Illuminate\Http\Request $request Request object
36-
* @param \Exception $ex Exception
36+
* @param \Throwable $ex Throwable to handle
3737
*
3838
* @return HttpResponse
3939
*/
40-
public static function render(/** @scrutinizer ignore-unused */ $request, Exception $ex): HttpResponse
40+
public static function render(/** @scrutinizer ignore-unused */ $request, \Throwable $ex): HttpResponse
4141
{
4242
$result = null;
4343

@@ -62,17 +62,17 @@ public static function render(/** @scrutinizer ignore-unused */ $request, Except
6262
}
6363

6464
/**
65-
* Handles given exception and produces valid HTTP response object.
65+
* Handles given throwable and produces valid HTTP response object.
6666
*
67-
* @param \Exception $ex Exception to be handled.
67+
* @param \Throwable $ex Throwable to be handled.
6868
* @param array $ex_cfg ExceptionHandler's config excerpt related to $ex exception type.
6969
* @param int $fallback_http_code HTTP code to be assigned to produced $ex related response in
7070
* case configuration array lacks own `http_code` value. Default
7171
* HttpResponse::HTTP_INTERNAL_SERVER_ERROR
7272
*
7373
* @return \Symfony\Component\HttpFoundation\Response
7474
*/
75-
protected static function processException(\Exception $ex, array $ex_cfg,
75+
protected static function processException(\Throwable $ex, array $ex_cfg,
7676
int $fallback_http_code = HttpResponse::HTTP_INTERNAL_SERVER_ERROR)
7777
{
7878
$api_code = $ex_cfg['api_code'];
@@ -110,13 +110,13 @@ protected static function processException(\Exception $ex, array $ex_cfg,
110110
* `default` handler either for HttpException (if $ex is instance of it), or generic `default`
111111
* config.
112112
*
113-
* @param \Exception $ex
113+
* @param \Throwable $ex
114114
* @param int $http_code
115115
* @param array $placeholders
116116
*
117117
* @return string
118118
*/
119-
protected static function getErrorMessageForException(\Exception $ex, int $http_code, array $placeholders): string
119+
protected static function getErrorMessageForException(\Throwable $ex, int $http_code, array $placeholders): string
120120
{
121121
// exception message is uselss, lets go deeper
122122
if ($ex instanceof HttpException) {
@@ -153,14 +153,14 @@ protected function unauthenticated(/** @scrutinizer ignore-unused */ $request,
153153
/**
154154
* Process single error and produce valid API response.
155155
*
156-
* @param Exception $ex Exception to be handled.
156+
* @param \Throwable $ex Exception to be handled.
157157
* @param integer $api_code
158158
* @param integer $http_code
159159
* @param string $error_message
160160
*
161161
* @return HttpResponse
162162
*/
163-
protected static function error(Exception $ex,
163+
protected static function error(Throwable $ex,
164164
int $api_code, int $http_code = null, string $error_message = null): HttpResponse
165165
{
166166
$ex_http_code = ($ex instanceof HttpException) ? $ex->getStatusCode() : $ex->getCode();
@@ -265,7 +265,7 @@ protected static function getExceptionHandlerConfig(): array
265265
*
266266
* @return array|null
267267
*/
268-
protected static function getHandler(Exception $ex): ?array
268+
protected static function getHandler(\Throwable $ex): ?array
269269
{
270270
$result = null;
271271

0 commit comments

Comments
 (0)