-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorHandler.php
More file actions
135 lines (119 loc) · 3.77 KB
/
ErrorHandler.php
File metadata and controls
135 lines (119 loc) · 3.77 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <info@arikaim.com>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Core\Framework;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Arikaim\Core\Http\Request;
use Arikaim\Core\App\Install;
use Arikaim\Core\Routes\RouteType;
use Arikaim\Core\System\Error\ApplicationError;
use Arikaim\Core\System\Error\ErrorHandlerInterface;
use Arikaim\Core\Validator\DataValidatorException;
use Arikaim\Core\Http\ApiResponse;
use ErrorException;
use Throwable;
/**
* Error handler
*/
class ErrorHandler
{
/**
* Error renderer
*
* @var ErrorHandlerInterface|null
*/
protected $renderer = null;
/**
* Constructor
*
* @param object|null $renderer
*/
public function __construct(?ErrorHandlerInterface $renderer = null)
{
$this->renderer = $renderer;
}
/**
* Handle php app errors
*
* @param mixed $num
* @param mixed $message
* @param mixed $file
* @param mixed $line
* @return void
*/
public function handleError($num, $message, $file, $line)
{
throw new ErrorException($message,0,$num,$file,$line);
}
/**
* Render exception
*
* @param Throwable $exception
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function renderExecption(Throwable $exception, ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$uri = $request->getUri()->getPath();
if (Install::isInstalled() == false) {
if (RouteType::isInstallPage() == false && RouteType::isApiInstallRequest($uri) == false) {
// redirect to install page
return $this->redirectToInstallPage($response);
}
}
// check for redirect url
if (empty($response->getHeaderLine('Location')) == false) {
return $response->withStatus(307);
};
$this->resolveRenderer();
// set status code
$statusCode = ($exception instanceof HttpException) ? $exception->getStatusCode() : 400;
$response = $response->withStatus($statusCode);
// validation exception
if ($exception instanceof DataValidatorException) {
$apiResponse = new ApiResponse($response);
$apiResponse->setErrors($exception->getErrors());
return $apiResponse->getResponse();
}
// render errror
$renderType = (Request::isJsonContentType($request) == true) ? 'json' : 'html';
$output = $this->renderer->renderError($exception,$renderType);
$response->getBody()->write($output);
return $response;
}
/**
* Redirect to install page
*
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function redirectToInstallPage(ResponseInterface $response): ResponseInterface
{
return $response
->withoutHeader('Cache-Control')
->withHeader('Cache-Control','no-cache, must-revalidate')
->withHeader('Expires','Sat, 26 Jul 1997 05:00:00 GMT')
->withHeader('Location',RouteType::getInstallPageUrl())
->withStatus(307);
}
/**
* Create renderer if not set
*
* @return void
*/
private function resolveRenderer(): void
{
global $arikaim;
if ($this->renderer == null) {
$this->renderer = new ApplicationError($arikaim->get('page'));
}
}
}