14
14
use BEAR \Sunday \Extension \Error \ErrorInterface ;
15
15
use BEAR \Sunday \Extension \Router \RouterMatch as Request ;
16
16
use BEAR \Sunday \Extension \Transfer \TransferInterface ;
17
+ use BEAR \Package \Provide \Error \ErrorPage as CliErrorPage ;
17
18
use BEAR \Sunday \Provide \Error \ErrorPage ;
18
19
19
20
/**
24
25
class VndError implements ErrorInterface
25
26
{
26
27
/**
27
- * @var AbstractAppMeta
28
+ * @var int
28
29
*/
29
- private $ appMeta ;
30
+ private $ code ;
30
31
31
32
/**
32
- * @var int
33
+ * @var array
33
34
*/
34
- private $ code ;
35
+ private $ headers = [] ;
35
36
36
37
/**
37
38
* @var array
38
39
*/
39
40
private $ body = ['message ' => '' , 'logref ' => '' ];
40
41
42
+ /**
43
+ * @var string
44
+ */
45
+ private $ logDir ;
46
+
47
+ /**
48
+ * @var ErrorPage
49
+ */
50
+ private $ errorPage ;
51
+
41
52
/**
42
53
* @var TransferInterface
43
54
*/
44
55
private $ responder ;
45
56
57
+ /**
58
+ * @var string
59
+ */
60
+ private $ lastErrorFile ;
61
+
62
+ /**
63
+ * @var ExceptionAsString
64
+ */
65
+ private $ exceptionString ;
66
+
46
67
public function __construct (AbstractAppMeta $ appMeta , TransferInterface $ responder )
47
68
{
48
- $ this ->appMeta = $ appMeta ;
69
+ $ this ->logDir = $ appMeta ->logDir ;
70
+ $ this ->lastErrorFile = $ this ->logDir . '/last_error.log ' ;
49
71
$ this ->responder = $ responder ;
72
+ $ this ->exceptionString = new ExceptionAsString ;
50
73
}
51
74
52
75
/**
53
76
* {@inheritdoc}
54
77
*/
55
78
public function handle (\Exception $ e , Request $ request )
56
79
{
57
- unset( $ request ) ;
80
+ $ this -> errorPage = PHP_SAPI === ' cli ' ? new CliErrorPage ( $ this -> exceptionString -> summery ( $ e , $ this -> lastErrorFile )) : new ErrorPage ;
58
81
$ isCodeError = ($ e instanceof NotFound || $ e instanceof BadRequest || $ e instanceof ServerErrorException);
59
82
if ($ isCodeError ) {
60
83
list ($ this ->code , $ this ->body ) = $ this ->codeError ($ e );
61
84
62
85
return $ this ;
63
86
}
87
+ // 500 exception
64
88
$ 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
+ ];
67
94
68
95
return $ this ;
69
96
}
@@ -73,7 +100,7 @@ public function handle(\Exception $e, Request $request)
73
100
*/
74
101
public function transfer ()
75
102
{
76
- $ ro = new ErrorPage ;
103
+ $ ro =$ this -> errorPage ;
77
104
$ ro ->code = $ this ->code ;
78
105
$ ro ->headers ['content-type ' ] = 'application/vnd.error+json ' ;
79
106
$ ro ->body = $ this ->body ;
@@ -96,14 +123,30 @@ private function codeError(\Exception $e)
96
123
97
124
/**
98
125
* @param \Exception $e
126
+ * @param Request $request
99
127
*
100
- * @return string
128
+ * @return int logRef
101
129
*/
102
- private function logRef (\Exception $ e )
130
+ private function log (\Exception $ e, Request $ request )
103
131
{
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 );
106
137
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 ());
108
151
}
109
152
}
0 commit comments