PHP code example of sageit / phalcon-exception-handler

1. Go to this page and download the library: Download sageit/phalcon-exception-handler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sageit / phalcon-exception-handler example snippets


new Phalcon\Exception\Handler(true,'Error');

$this->error(function ($exception) {
  $handler = new \Phalcon\Exception\Handler(false);
  $this
    ->response
    ->setStatusCode($code)
    ->setJsonContent($handler->getJSON($exception))
    ->send();
  die;
});

class MyCustomException extends Phalcon\Exception\Exception {
    protected const ERROR_MESSAGE = 'I\'ve created a new custom Exceptions #reasons';
    protected const ERROR_CODE    = 401;
    protected const LOG_MESSAGE = 'My custom exception was activated';
    protected const LOG_LEVEL = 'alert';

$handler->setDisplay('Notification');

$handler->display($exception,'Notification');

$handler->log($exception,'Notification');

$handler->getJSON($exception);

[
  'code' => 500,  //$this->getCode()
  'status' => 'error',
  'message' => 'Exception Encountered' //$this->getMessage()
]

\Phalcon\Exception\Handler::convertException($exception);