/** * Process the request, get a response and return it. * * Exceptions thrown in most places will be handled here. * Currently there's no nice way to handle exceptions in Response::respond * * @return Response * @SuppressWarnings(PHPMD.ShortMethodName) */ public function go() { $response = $this->getResponse(); try { $request = $this->getRequest(); $response->setWriterFactory($this->getWriterFactory()); $response->setRequest($request); $response->setBodyData($this->getRouter()->processRequest($this->getRequest(), $this->controller)); $response->setStatus($this->controller->getStatus()); } catch (AyeAyeException $e) { $this->log(LogLevel::INFO, $e->getPublicMessage()); $this->log(LogLevel::ERROR, $e->getMessage(), ['exception' => $e]); $response->setBodyData($e->getPublicMessage()); $response->setStatus(new Status($e->getCode())); } catch (\Exception $e) { $status = new Status(500); $this->log(LogLevel::CRITICAL, $e->getMessage(), ['exception' => $e]); $response->setBodyData($status->getMessage()); $response->setStatus($status); } // Ultimate fail safe try { $response->prepareResponse(); } catch (\Exception $e) { $this->log(LogLevel::CRITICAL, $e->getMessage(), ['exception' => $e]); return $this->createFailSafeResponse(); } return $response; }
/** * @test * @covers ::getHttpHeader * @uses AyeAye\Api\Status::getCode * @uses AyeAye\Api\Status::__construct * @uses AyeAye\Api\Status::getMessageForCode */ public function testGetHttpHeader() { $status = new Status(); $this->assertSame('HTTP/1.1 200 OK', $status->getHttpHeader()); $status = new Status(500); $this->assertSame('HTTP/1.1 500 Internal Server Error', $status->getHttpHeader()); }
/** * @test * @covers ::createFailSafeResponse * @uses \AyeAye\Api\Api::__construct * @uses \AyeAye\Api\Status * @uses \AyeAye\Api\Response * @uses \AyeAye\Api\Request * @uses \AyeAye\Api\Injector\StatusInjector * @uses \AyeAye\Api\Injector\RequestInjector * @uses \AyeAye\Formatter\Writer\Json */ public function testCreateFailSafeResponse() { // Mocks $controller = $this->getMockController(); // Tests $api = new Api($controller); $createFailSafeResponse = $this->getObjectMethod($api, 'createFailSafeResponse'); /** @var Response $response */ $response = $createFailSafeResponse(); $this->assertSame(500, $response->getStatus()->getCode()); $this->assertSame(Status::getMessageForCode(500), $response->getBody()['data']); }