-
Notifications
You must be signed in to change notification settings - Fork 0
/
Error.php
93 lines (85 loc) · 3.2 KB
/
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
84
85
86
87
88
89
90
91
92
93
<?php
/*
* This file is part of the OpenStates Framework (osf) package.
* (c) Guillaume Ponçon <guillaume.poncon@openstates.com>
* For the full copyright and license information, please read the LICENSE file distributed with the project.
*/
namespace Osf\Exception;
use Osf\Controller\Cli;
use Osf\Exception\PhpErrorException;
/**
* Common trigger errors
*
* @author Guillaume Ponçon <guillaume.poncon@openstates.com>
* @copyright OpenStates
* @version 1.0
* @since OSF-2.0 - 2017
* @package osf
* @subpackage error
*/
class Error
{
/**
* Transform errors to exceptions
* @return type
*/
public static function startErrorHandler()
{
return PhpErrorException::startHandler();
}
/**
* Non fatal error to display for testers and developers
* @param string $msg
* @task [ERROR] mettre dans un logger en production
*/
public static function notice(string $msg)
{
trigger_error($msg, E_USER_NOTICE);
}
/**
* Quick warning
* @param string $msg
* @task [ERROR] mettre dans un logger en production
*/
public static function warning(string $msg)
{
trigger_error($msg, E_USER_WARNING);
}
/**
* Display an exception if impossible to display it via EventController::error
* @param \Exception $e
* @task [ERROR] log d'erreur, remontée d'information propre
*/
public static function displayException(\Exception $e)
{
$cli = Cli::isCli();
if ($cli) {
$err = '-> Exception: ' . $e->getMessage() . "\n";
$err .= ' ' . $e->getFile() . ' (' . $e->getLine() . ")\n";
foreach ($e->getTrace() as $key => $item) {
if (isset($item['file']) && $item['file']) {
$err .= ' ' . trim($item['file']) . ' (' . $item['line'] . '): ' . $item['function'] . "()\n";
}
}
echo $err;
} else {
$html = '<div class="box box-danger"><div class="box-header with-border"><h3 class="box-title">Exception détectée via le error handler</h3>'
. '</div><div class="box-body"><div class="panel panel-danger"><div class="panel-body">'
. self::buildErrorLink($e->getFile(), $e->getLine()) . ': <strong>' . $e->getMessage() . '</strong></div>';
$html .= '<table class="table table-bordered"><tr><th style="width: 10px">#</th><th>Fichier</th><th>Fonction</th></tr>';
foreach ($e->getTrace() as $key => $item) {
$html .= '<tr><td>' . ($key + 1) . '</td><td>' . @self::buildErrorLink($item['file'], $item['line'])
. '</td><td>' . htmlspecialchars($item['function']) . '()</td></tr>';
}
$html .= '</table></div></div>';
//Container::getResponse()->appendBody($html);
echo $html;
}
}
public static function buildErrorLink($file, $line)
{
$link = '<a href="#" onclick="window.open(\'http://localhost/edit.php?file=' . $file . '&line=' . (int) $line . '\',\'edit\',\'menubar=no, status=no, scrollbars=no, menubar=no\');">';
$link .= $file . '(' . (int) $line . ')' . "</a>";
return $link;
}
}