/** * Render an API Problem representation * * Also sets the $apiProblem member to the passed object. * * @param ApiProblem $apiProblem * @return string */ protected function renderApiProblem(ApiProblem $apiProblem) { $this->apiProblem = $apiProblem; if ($this->displayExceptions) { $apiProblem->setDetailIncludesStackTrace(true); } return parent::render($apiProblem->toArray()); }
/** * Retrieve the HTTP status from an ApiProblem object * * Ensures that the status falls within the acceptable range (100 - 599). * * @param ApiProblem $problem * @return int */ protected function getStatusCodeFromApiProblem(ApiProblem $problem) { $problemData = $problem->toArray(); $status = array_key_exists('status', $problemData) ? $problemData['status'] : 0; if ($status < 100 || $status >= 600) { return 500; } return $status; }
/** * @throws ApiProblem */ public function bulkAction() { $data = $this->bodyParams(); if (!is_array($data)) { $exception = new ApiProblem(500, 'Invalid body'); return new JsonModel($exception->toArray()); } $result = $this->bulkService->bulk($data); $response = new JsonModel($result); return $response; }
public function deleteClientParamAction() { // get pass in params $data = $this->bodyParams(); if (isset($data['account']) && isset($data['key'])) { try { // service $service = $this->sm->get('Lib\\Openvpn\\Service\\ClientConfig'); $response = $service->deleteParam($data['account'], $data['key']); } catch (\Exception $e) { $this->getResponse()->setStatusCode(405); $response = new ApiProblem(405, 'Param key:[' . $data['key'] . '] has already deleted.'); } // view return new ViewModel($response->toArray()); } }
/** * Retrieve the content. * * Serializes the composed ApiProblem instance to JSON. * * @return string */ public function getContent() { return json_encode($this->apiProblem->toArray(), $this->jsonFlags); }
public function testUsesAdditionalDetailsFromExceptionWhenProvided() { $exception = new Exception\DomainException('exception message', 401); $exception->setAdditionalDetails(['foo' => 'bar']); $apiProblem = new ApiProblem('401', $exception); $payload = $apiProblem->toArray(); $this->assertArrayHasKey('foo', $payload); $this->assertEquals('bar', $payload['foo']); }
public function testApiProblemResponseBodyIsSerializedApiProblem() { $apiProblem = new ApiProblem(400, 'Random error'); $response = new ApiProblemResponse($apiProblem); $this->assertEquals($apiProblem->toArray(), json_decode($response->getContent(), true)); }