1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Exception; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\App; |
6
|
|
|
use Ffcms\Core\Arch\Controller; |
7
|
|
|
use Ffcms\Core\Helper\Type\Str; |
8
|
|
|
|
9
|
|
|
abstract class TemplateException extends \Exception |
10
|
|
|
{ |
11
|
|
|
public $status = 404; |
12
|
|
|
public $title = '404 Not Found'; |
13
|
|
|
public $text = 'An unexpected error occurred: %e%'; |
14
|
|
|
public $tpl = 'default'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Display exception template |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public function display() |
21
|
|
|
{ |
22
|
|
|
// hide path root and stip tags from exception message |
23
|
|
|
$msg = Str::replace(root, '$DOCUMENT_ROOT', $this->getMessage()); |
|
|
|
|
24
|
|
|
$msg = App::$Security->strip_tags($msg); |
25
|
|
|
// get message translation |
26
|
|
|
$this->text = App::$Translate->get('Default', $this->text, ['e' => $msg]); |
27
|
|
|
|
28
|
|
|
// if exception is throwed not from html-based environment |
29
|
|
|
if (env_type !== 'html') { |
|
|
|
|
30
|
|
|
if (env_type === 'json') { |
31
|
|
|
return (new JsonException($msg))->display(); |
32
|
|
|
} else { |
33
|
|
|
return $this->text; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// add notification in debug bar if exist |
38
|
|
|
if (App::$Debug !== null) { |
39
|
|
|
App::$Debug->addException($this); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// return rendered result |
43
|
|
|
return $this->buildFakePage(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Build fake page with error based on fake controller initiation |
48
|
|
|
*/ |
49
|
|
|
protected function buildFakePage() |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
$rawResponse = App::$View->render('native/errors/' . $this->tpl, ['msg' => $this->text]); |
53
|
|
|
if (Str::likeEmpty($rawResponse)) { |
54
|
|
|
$rawResponse = $this->text; |
55
|
|
|
} |
56
|
|
|
} catch (\Exception $e) { |
57
|
|
|
$rawResponse = $this->text; |
58
|
|
|
} |
59
|
|
|
// set status code for header |
60
|
|
|
App::$Response->setStatusCode((int)$this->status); |
61
|
|
|
// return compiled html output |
62
|
|
|
return $rawResponse; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|