private function createBadRequestResponse(ApiProblem $problem) { $status = ApiProblemJsonResponse::HTTP_BAD_REQUEST; $problem->setStatus($status); $response = new ApiProblemJsonResponse($problem); return $response; }
/** * Creates and return new ApiProblem instance * * @param string $title title * @param int $status status code * @param string $detail description * @return ApiProblem */ private function apiProblem($title, $status, $detail = null) { $problem = new ApiProblem($title, 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html'); if ($detail) { $problem->setDetail($detail); } $problem->setStatus($status); return $problem; }
public function __construct(ApiProblem $problem, $status = 200, $headers = array()) { $headers += ['Content-Type' => 'application/problem+json']; if (null !== $problem->getStatus()) { $headers += ['X-Status-Code' => $problem->getStatus()]; } $data = $problem->asArray(); parent::__construct($data, $status, $headers); }
/** * @param \Exception $e * @return ApiProblem */ protected function createNewApiProblem(\Exception $e) { $problem = new ApiProblem($e->getMessage()); $problem->setStatus($e->getCode() ? $e->getCode() : ApiProblemJsonResponse::HTTP_BAD_REQUEST); return $problem; }
protected function registerErrorListeners(Application $app) { $app->error(function (\Exception $e, $code) { $problem = new ApiProblem('Unknown error'); $problem->setDetail($e->getMessage()); return new JsonResponse($problem->asArray(), $code); }); $app->error(function (ObjectNotFoundException $e) { $problem = new ApiProblem('Object not found', 'http://httpstatus.es/404'); $problem->setDetail($e->getMessage()); return new JsonResponse($problem->asArray(), Response::HTTP_NOT_FOUND); }, 10); $app->error(function (NotFoundHttpException $e, $code) { $problem = new ApiProblem('Resource not found', 'http://httpstatus.es/404'); $problem->setDetail($e->getMessage()); return new JsonResponse($problem->asArray(), $code); }, 10); $app->error(function (NotAcceptableHttpException $e, $code) { $problem = new ApiProblem('No acceptable format available', 'http://httpstatus.es/406'); $problem->setDetail($e->getMessage()); return new JsonResponse($problem->asArray(), $code); }, 10); }