Files
pathfinder/app/Exception/PathfinderException.php
Mark Friedrich 647bd7db58 - BC Break: _PHP_ namespaces changed (PSR-4 standard). The _root_ namespace for all _PF_ related scripts is Exodus4D\Pathfinder
- BC Break: Project folder structure changed. Removed `app/main` dir.
- BC Break: Core _PHP_ framework + dependencies moved into `composer.json` and are no longer part of this repo
2019-12-15 22:27:17 +01:00

67 lines
1.7 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: exodus4d
* Date: 21.02.15
* Time: 00:41
*/
namespace Exodus4D\Pathfinder\Exception;
use Exodus4D\Pathfinder\Lib\Config;
class PathfinderException extends \Exception {
/**
* default HTTP response code for PathfinderExceptions
* -> can be specified by using custom Exception codes
*/
const DEFAULT_RESPONSECODE = 500;
/**
* lists all exception codes
* @var array
*/
protected $codes = [
0 => self::DEFAULT_RESPONSECODE
];
/**
* PathfinderException constructor.
* @param string $message
* @param int $code
*/
public function __construct(string $message, int $code = 0){
if( !array_key_exists($code, $this->codes) ){
// exception code not specified by child class
$code = 0;
}
parent::__construct($message, $code);
}
/**
* get error object
* @return \stdClass
*/
public function getError() : \stdClass {
$error = (object) [];
$error->type = 'error';
$error->code = $this->getResponseCode();
$error->status = Config::getHttpStatusByCode($this->getResponseCode());
$error->message = $this->getMessage();
if(\Base::instance()->get('DEBUG') >= 1){
$error->trace = preg_split('/\R/', $this->getTraceAsString()); // no $this->>getTrace() here -> to much data
}
return $error;
}
/**
* returns the HTTP response code for the client from exception
* -> if Exception is not handled/catched 'somewhere' this code is used by the final onError handler
* @return int
*/
public function getResponseCode() : int {
return $this->codes[$this->getCode()];
}
}