-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGDT_Error.php
83 lines (70 loc) · 1.67 KB
/
GDT_Error.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
namespace GDO\UI;
use GDO\CLI\CLI;
use GDO\Core\Application;
use GDO\Core\Debug;
use GDO\Core\GDO_Exception;
use GDO\Util\Strings;
use Throwable;
/**
* An error is a message box with a special css class.
* It can be configured via an exception.
*
* @version 7.0.3
* @since 3.0.0
* @author gizmore
*/
final class GDT_Error extends GDT_Panel
{
public int $code = GDO_Exception::GDT_ERROR_CODE;
############
### Code ###
############
protected function __construct()
{
parent::__construct();
$this->icon = 'error';
$this->addClass('gdt-error');
$this->addClass('alert');
$this->addClass('alert-danger');
}
###########
### GDT ###
###########
public function code(int $code): self
{
$this->code = $code;
return $this;
}
public static function fromException(Throwable $t): self
{
$is_html = Application::$INSTANCE->isHTML();
$error = self::make()->title('exception', [$t->getMessage()]);
$error->textRaw(Debug::backtraceException($t, $is_html, $t->getMessage()));
if (!($t instanceof GDO_Exception))
{
Application::setResponseCode(500);
}
return $error;
}
public function renderHTML(): string
{
hdr("X-GDO-ERROR: {$this->renderHeaderText()}");
return parent::renderHTML();
}
public function renderHeaderText(): string
{
return self::displayHeaderText($this->renderTitle());
}
public static function displayHeaderText(string $text): string
{
$text = $text ? str_replace(["\r", "\n"], ' | ', $text) : t('no_information');
$text = CLI::removeColorCodes($text);
return Strings::dotted($text, 1024);
}
public function renderCLI(): string
{
return Color::red($this->renderText()) . "\n";
}
}