Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit ffbce1c

Browse files
author
Andrey Helldar
committed
Improved support for Laravel and Lumen
1 parent 479b982 commit ffbce1c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/Support/Exception.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
namespace Helldar\ApiResponse\Support;
44

5-
use function basename;
65
use Exception as BaseException;
7-
use function get_class;
86
use Illuminate\Contracts\Support\Responsable;
97
use Illuminate\Http\Exceptions\HttpResponseException;
10-
8+
use Illuminate\Http\Response as LaravelResponse;
119
use Illuminate\Validation\ValidationException;
10+
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
11+
use Throwable;
12+
13+
use function basename;
14+
use function get_class;
1215
use function is_object;
1316
use function is_subclass_of;
1417
use function str_replace;
15-
use Throwable;
1618

1719
final class Exception
1820
{
@@ -40,8 +42,8 @@ public static function isThrowable($value = null): bool
4042
public static function getCode($value, int $status_code = 400): int
4143
{
4244
$code = $value instanceof ValidationException
43-
? ($value->status ?? $value->getCode())
44-
: ($value->getCode() ?? $status_code);
45+
? $value->status ?? $value->getCode() ?? $status_code
46+
: $value->getCode() ?? $value->getStatusCode() ?? $status_code;
4547

4648
return static::correctStatusCode($code, $status_code);
4749
}
@@ -55,6 +57,12 @@ public static function getType(Throwable $class): string
5557

5658
public static function getData($exception)
5759
{
60+
if ($exception instanceof SymfonyResponse || $exception instanceof LaravelResponse) {
61+
return method_exists($exception, 'getOriginalContent')
62+
? $exception->getOriginalContent()
63+
: $exception->getContent() ?? $exception->getMessage();
64+
}
65+
5866
if ($exception instanceof Responsable || $exception instanceof HttpResponseException) {
5967
return $exception->getResponse();
6068
}

0 commit comments

Comments
 (0)