]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/api/exceptions/ApiException.php
3 namespace Shaarli\Api\Exceptions
;
5 use Slim\Http\Response
;
8 * Abstract class ApiException
10 * Parent Exception related to the API, able to generate a valid Response (ResponseInterface).
11 * Also can include various information in debug mode.
13 abstract class ApiException
extends \Exception
{
16 * @var Response instance from Slim.
21 * @var bool Debug mode enabled/disabled.
26 * Build the final response.
28 * @return Response Final response to give.
30 public abstract function getApiResponse();
33 * Creates ApiResponse body.
34 * In production mode, it will only return the exception message,
35 * but in dev mode, it includes additional information in an array.
37 * @return array|string response body
39 protected function getApiResponseBody() {
40 if ($this->debug
!== true) {
41 return $this->getMessage();
44 'message' => $this->getMessage(),
45 'stacktrace' => get_class($this) .': '. $this->getTraceAsString()
50 * Build the Response object to return.
52 * @param int $code HTTP status.
54 * @return Response with status + body.
56 protected function buildApiResponse($code)
58 $style = $this->debug
? JSON_PRETTY_PRINT
: null;
59 return $this->response
->withJson($this->getApiResponseBody(), $code, $style);
63 * @param Response $response
65 public function setResponse($response)
67 $this->response
= $response;
73 public function setDebug($debug)
75 $this->debug
= $debug;