Exemplo n.º 1
0
 /**
  * If the application throws an HTTPException, respond correctly (json etc.)
  * todo this was not working as the try catch blocks in controllers
  * was catching the exception before it would be handled, need
  * to come back to this
  */
 public function setExceptionHandler(DiInterface $di)
 {
     //return $this;
     set_exception_handler(function ($exception) use($di) {
         /** @var $exception Exception */
         // Handled exceptions
         if (is_a($exception, 'Phrest\\API\\Exceptions\\HandledException')) {
             $response = new Response();
             $response->setStatusCode($exception->getCode(), $exception->getMessage());
             $response->addMessage($exception->getMessage(), ResponseMessage::TYPE_WARNING);
             return (new JSONResponse($response))->send();
         } else {
             $response = new Response();
             $response->setStatusCode(500, 'Internal Server Error');
             $response->addMessage('Internal Server Error', ResponseMessage::TYPE_WARNING);
             (new JSONResponse($response))->send();
         }
         // Log the exception
         error_log($exception);
         error_log($exception->getTraceAsString());
         return true;
     });
 }