16
16
use ActiveCollab \Controller \ResultEncoder \ResultEncoderInterface ;
17
17
use Exception ;
18
18
use Interop \Container \ContainerInterface ;
19
+ use InvalidArgumentException ;
19
20
use Psr \Http \Message \ResponseInterface ;
20
21
use Psr \Http \Message \ServerRequestInterface ;
21
22
use Psr \Log \LoggerInterface ;
23
+ use RuntimeException ;
22
24
23
25
/**
24
26
* @package ActiveCollab\Controller
@@ -32,14 +34,31 @@ abstract class Controller implements ContainerAccessInterface, ControllerInterfa
32
34
*/
33
35
private $ result_encoder ;
34
36
37
+ /**
38
+ * @var LoggerInterface
39
+ */
40
+ private $ logger ;
41
+
42
+ /**
43
+ * @var string
44
+ */
45
+ private $ action_exception_message = 'Whoops, something went wrong... ' ;
46
+
47
+ /**
48
+ * @var string
49
+ */
50
+ private $ log_exception_message = 'Controller action aborted with an exception ' ;
51
+
35
52
/**
36
53
* @param ContainerInterface $container
37
54
* @param ResultEncoderInterface $result_encoder
55
+ * @param LoggerInterface|null $logger
38
56
*/
39
- public function __construct (ContainerInterface &$ container , ResultEncoderInterface &$ result_encoder )
57
+ public function __construct (ContainerInterface &$ container , ResultEncoderInterface &$ result_encoder, LoggerInterface $ logger = null )
40
58
{
41
59
$ this ->setContainer ($ container );
42
60
$ this ->setResultEncoder ($ result_encoder );
61
+ $ this ->setLogger ($ logger );
43
62
}
44
63
45
64
/**
@@ -60,6 +79,68 @@ public function &setResultEncoder(ResultEncoderInterface $result_encoder)
60
79
return $ this ;
61
80
}
62
81
82
+ /**
83
+ * {@inheritdoc}
84
+ */
85
+ public function getLogger ()
86
+ {
87
+ return $ this ->logger ;
88
+ }
89
+
90
+ /**
91
+ * {@inheritdoc}
92
+ */
93
+ public function &setLogger (LoggerInterface $ logger = null )
94
+ {
95
+ $ this ->logger = $ logger ;
96
+
97
+ return $ this ;
98
+ }
99
+
100
+ /**
101
+ * {@inheritdoc}
102
+ */
103
+ public function getClientFacingExceptionMessage ()
104
+ {
105
+ return $ this ->action_exception_message ;
106
+ }
107
+
108
+ /**
109
+ * {@inheritdoc}
110
+ */
111
+ public function &setClientFacingExceptionMessage ($ message )
112
+ {
113
+ if (empty ($ message )) {
114
+ throw new InvalidArgumentException ("Client facing exception message can't be empty " );
115
+ }
116
+
117
+ $ this ->action_exception_message = $ message ;
118
+
119
+ return $ this ;
120
+ }
121
+
122
+ /**
123
+ * {@inheritdoc}
124
+ */
125
+ public function getLogExceptionMessage ()
126
+ {
127
+ return $ this ->log_exception_message ;
128
+ }
129
+
130
+ /**
131
+ * {@inheritdoc}
132
+ */
133
+ public function &setLogExceptionMessage ($ message )
134
+ {
135
+ if (empty ($ message )) {
136
+ throw new InvalidArgumentException ("Log exception message can't be empty " );
137
+ }
138
+
139
+ $ this ->log_exception_message = $ message ;
140
+
141
+ return $ this ;
142
+ }
143
+
63
144
/**
64
145
* Run before every action.
65
146
*
@@ -114,13 +195,17 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
114
195
try {
115
196
return $ this ->getResultEncoder ()->encode (call_user_func ([&$ this , $ action ], $ request , $ arguments ), $ request , $ response );
116
197
} catch (Exception $ e ) {
117
- if ($ this ->logger instanceof LoggerInterface) {
118
- $ this ->logger ->warning ("Exception is caught with message {$ e ->getMessage ()}" , [
119
- 'exception ' => $ e ,
120
- ]);
198
+ if ($ this ->logger ) {
199
+ $ this ->logger ->error ($ this ->getLogExceptionMessage (), ['exception ' => $ e ]);
200
+ }
201
+
202
+ $ exception_message = $ this ->action_exception_message ;
203
+
204
+ if (strpos ($ exception_message , '{message} ' ) !== false ) {
205
+ $ exception_message = str_replace ('{message} ' , $ e ->getMessage (), $ exception_message );
121
206
}
122
207
123
- return $ this ->getResultEncoder ()->encode ($ e , $ request , $ response );
208
+ return $ this ->getResultEncoder ()->encode (new RuntimeException ( $ exception_message , 0 , $ e ) , $ request , $ response );
124
209
}
125
210
}
126
211
} else {
0 commit comments