Skip to content

Commit 2f2c1bf

Browse files
committed
Merge remote-tracking branch 'bearsunday/1.x' into 1.x
2 parents 8acb112 + 7aaae2d commit 2f2c1bf

File tree

3 files changed

+135
-14
lines changed

3 files changed

+135
-14
lines changed

src/Provide/Error/ErrorPage.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* This file is part of the BEAR.Package package.
4+
*
5+
* @license http://opensource.org/licenses/MIT MIT
6+
*/
7+
namespace BEAR\Package\Provide\Error;
8+
9+
use BEAR\Resource\ResourceObject;
10+
11+
class ErrorPage extends ResourceObject
12+
{
13+
/**
14+
* @var string
15+
*/
16+
private $postBody;
17+
18+
public function __construct($postBody)
19+
{
20+
$this->postBody = $postBody;
21+
}
22+
23+
public function __toString()
24+
{
25+
$string = parent::__toString();
26+
27+
return $string . $this->postBody;
28+
}
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* This file is part of the BEAR.Package package.
4+
*
5+
* @license http://opensource.org/licenses/MIT MIT
6+
*/
7+
namespace BEAR\Package\Provide\Error;
8+
9+
use BEAR\Sunday\Extension\Router\RouterMatch as Request;
10+
11+
class ExceptionAsString
12+
{
13+
public function summery(\Exception $e, $log)
14+
{
15+
return sprintf("\n\n[%s]\n%s\n %s", get_class($e), $e->getMessage(), $log);
16+
}
17+
18+
/**
19+
* @param Request $request
20+
* @param string $lastErrorLog
21+
*
22+
* @return string
23+
*/
24+
public function detail(\Exception $e, Request $request)
25+
{
26+
$eSummery = sprintf("[%s]\n%s\nin file %s on line %s\n\n%s",
27+
get_class($e),
28+
$e->getMessage(),
29+
$e->getFile(),
30+
$e->getLine(),
31+
$e->getTraceAsString()
32+
);
33+
34+
return sprintf("%s\n%s\n\n%s\n%s", date(DATE_RFC2822), $request, $eSummery, $this->getPhpVariables($_SERVER));
35+
}
36+
37+
/**
38+
* @param array $server
39+
*
40+
* @return string
41+
*/
42+
private function getPhpVariables(array $server)
43+
{
44+
if (PHP_SAPI === 'cli') {
45+
return '';
46+
}
47+
return sprintf("\nPHP Variables\n\n\$_SERVER => %s", print_r($server, true));
48+
}
49+
}

src/Provide/Error/VndError.php

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use BEAR\Sunday\Extension\Error\ErrorInterface;
1515
use BEAR\Sunday\Extension\Router\RouterMatch as Request;
1616
use BEAR\Sunday\Extension\Transfer\TransferInterface;
17+
use BEAR\Package\Provide\Error\ErrorPage as CliErrorPage;
1718
use BEAR\Sunday\Provide\Error\ErrorPage;
1819

1920
/**
@@ -24,46 +25,72 @@
2425
class VndError implements ErrorInterface
2526
{
2627
/**
27-
* @var AbstractAppMeta
28+
* @var int
2829
*/
29-
private $appMeta;
30+
private $code;
3031

3132
/**
32-
* @var int
33+
* @var array
3334
*/
34-
private $code;
35+
private $headers = [];
3536

3637
/**
3738
* @var array
3839
*/
3940
private $body = ['message' => '', 'logref' => ''];
4041

42+
/**
43+
* @var string
44+
*/
45+
private $logDir;
46+
47+
/**
48+
* @var ErrorPage
49+
*/
50+
private $errorPage;
51+
4152
/**
4253
* @var TransferInterface
4354
*/
4455
private $responder;
4556

57+
/**
58+
* @var string
59+
*/
60+
private $lastErrorFile;
61+
62+
/**
63+
* @var ExceptionAsString
64+
*/
65+
private $exceptionString;
66+
4667
public function __construct(AbstractAppMeta $appMeta, TransferInterface $responder)
4768
{
48-
$this->appMeta = $appMeta;
69+
$this->logDir = $appMeta->logDir;
70+
$this->lastErrorFile = $this->logDir . '/last_error.log';
4971
$this->responder = $responder;
72+
$this->exceptionString = new ExceptionAsString;
5073
}
5174

5275
/**
5376
* {@inheritdoc}
5477
*/
5578
public function handle(\Exception $e, Request $request)
5679
{
57-
unset($request);
80+
$this->errorPage = PHP_SAPI === 'cli' ? new CliErrorPage($this->exceptionString->summery($e, $this->lastErrorFile)) : new ErrorPage;
5881
$isCodeError = ($e instanceof NotFound || $e instanceof BadRequest || $e instanceof ServerErrorException);
5982
if ($isCodeError) {
6083
list($this->code, $this->body) = $this->codeError($e);
6184

6285
return $this;
6386
}
87+
// 500 exception
6488
$this->code = 500;
65-
$this->body = ['message' => '500 Server Error'];
66-
$this->logRef($e);
89+
$logRef = $this->log($e, $request);
90+
$this->body = [
91+
'message' => '500 Server Error',
92+
'logref' => $logRef
93+
];
6794

6895
return $this;
6996
}
@@ -73,7 +100,7 @@ public function handle(\Exception $e, Request $request)
73100
*/
74101
public function transfer()
75102
{
76-
$ro = new ErrorPage;
103+
$ro =$this->errorPage;
77104
$ro->code = $this->code;
78105
$ro->headers['content-type'] = 'application/vnd.error+json';
79106
$ro->body = $this->body;
@@ -96,14 +123,30 @@ private function codeError(\Exception $e)
96123

97124
/**
98125
* @param \Exception $e
126+
* @param Request $request
99127
*
100-
* @return string
128+
* @return int logRef
101129
*/
102-
private function logRef(\Exception $e)
130+
private function log(\Exception $e, Request $request)
103131
{
104-
$logRef = (string) CRC32($e);
105-
file_put_contents($this->appMeta->logDir . "/$logRef.log", $e);
132+
$logRefId = $this->getLogRefId($e);
133+
$logRefFile = sprintf('%s/e.%s.log', $this->logDir, $logRefId);
134+
$log = $this->exceptionString->detail($e, $request);
135+
file_put_contents($this->lastErrorFile, $log);
136+
file_put_contents($logRefFile, $log);
106137

107-
return $logRef;
138+
return $logRefId;
139+
}
140+
141+
/**
142+
* Return log ref id
143+
*
144+
* @param \Exception $e
145+
*
146+
* @return string
147+
*/
148+
private function getLogRefId(\Exception $e)
149+
{
150+
return (string) crc32(get_class($e) . $e->getMessage() . $e->getFile() . $e->getLine());
108151
}
109152
}

0 commit comments

Comments
 (0)