/** * Try and execute the requested route. * If the requested route is an API route, * catch all HttpExceptions and make it a nice response. * * @param Request $request * @return \Illuminate\Http\Response */ public function dispatch(Request $request) { // If the route is not an API route, // dont modify the response if (!$this->isApiRequest($request)) { return parent::dispatch($request); } try { $response = Response::makeFromExisting(parent::dispatch($request)); } catch (HttpExceptionInterface $exception) { $response = Response::makeFromException($exception); } catch (ModelNotFoundException $exception) { $response = Response::makeFromException(new NotFoundResourceException($exception->getModel())); } return $response->finalize($this->getMutator($request), $this->getEncoder($request), $request); }
/** * When an exception is thrown, the Router will try and make a response from it. * It should also be attached to the response itself. * * @return void */ public function testCreatesInstanceFromExistingExceptionAlsoAddsExceptionToResponse() { $exception = new NotFoundResourceException('Test'); $response = Response::makeFromException($exception); $this->assertTrue($response->hasException()); }