Skip to content

Commit e3c8b31

Browse files
Merge pull request dingo#1692 from zhwei/exception-handler
Exception Handler: set exception to $response->exception
2 parents bcdd5e7 + bf3fbd9 commit e3c8b31

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

src/Exception/Handler.php

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

33
namespace Dingo\Api\Exception;
44

5-
use Exception;
6-
use ReflectionFunction;
7-
use Illuminate\Support\Str;
8-
use Illuminate\Http\Response;
95
use Dingo\Api\Contract\Debug\ExceptionHandler;
106
use Dingo\Api\Contract\Debug\MessageBagErrors;
7+
use Exception;
8+
use Illuminate\Contracts\Debug\ExceptionHandler as IlluminateExceptionHandler;
9+
use Illuminate\Http\Response;
10+
use Illuminate\Support\Str;
1111
use Illuminate\Validation\ValidationException;
12+
use ReflectionFunction;
1213
use Symfony\Component\HttpFoundation\Response as BaseResponse;
13-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1414
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
15-
use Illuminate\Contracts\Debug\ExceptionHandler as IlluminateExceptionHandler;
15+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1616

1717
class Handler implements ExceptionHandler, IlluminateExceptionHandler
1818
{
@@ -157,11 +157,11 @@ public function handle(Exception $exception)
157157
$response = new Response($response, $this->getExceptionStatusCode($exception));
158158
}
159159

160-
return $response;
160+
return $response->withException($exception);
161161
}
162162
}
163163

164-
return $this->genericResponse($exception);
164+
return $this->genericResponse($exception)->withException($exception);
165165
}
166166

167167
/**

tests/Exception/HandlerTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace Dingo\Api\Tests\Exception;
44

5-
use Mockery as m;
6-
use RuntimeException;
7-
use Illuminate\Http\Response;
8-
use PHPUnit\Framework\TestCase;
95
use Dingo\Api\Exception\Handler;
6+
use Dingo\Api\Exception\ResourceException;
7+
use Dingo\Api\Http\Request as ApiRequest;
108
use Illuminate\Http\JsonResponse;
119
use Illuminate\Http\RedirectResponse;
12-
use Dingo\Api\Http\Request as ApiRequest;
13-
use Dingo\Api\Exception\ResourceException;
10+
use Illuminate\Http\Response;
11+
use Mockery as m;
12+
use PHPUnit\Framework\TestCase;
13+
use RuntimeException;
1414
use Symfony\Component\HttpKernel\Exception\HttpException;
1515

1616
class HandlerTest extends TestCase
@@ -55,6 +55,7 @@ public function testExceptionHandlerHandlesException()
5555

5656
$this->assertSame('foo', $response->getContent());
5757
$this->assertSame(404, $response->getStatusCode());
58+
$this->assertSame($exception, $response->exception);
5859
}
5960

6061
public function testExceptionHandlerHandlesExceptionAndCreatesNewResponse()
@@ -142,6 +143,7 @@ public function testRegularExceptionsAreHandledByGenericHandler()
142143

143144
$this->assertSame('{"message":"Uh oh","status_code":500}', $response->getContent());
144145
$this->assertSame(500, $response->getStatusCode());
146+
$this->assertSame($exception, $response->exception);
145147
}
146148

147149
public function testResourceExceptionErrorsAreIncludedInResponse()

0 commit comments

Comments
 (0)