Files
pathfinder/app/main/exception/pathfinderexception.php
Mark Friedrich 703091949b - moved ajax endpoints (connections, systems) into new REST API endpoints, #274
- improved error handling in case an Exception is thrown
2018-11-16 19:09:12 +01:00

61 lines
1.6 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: exodus4d
* Date: 21.02.15
* Time: 00:41
*/
namespace Exception;
use 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
];
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()];
}
}