1313use Exception ;
1414use Psr \Http \Message \ResponseInterface ;
1515use Psr \Http \Message \ServerRequestInterface ;
16+ use Throwable ;
1617
1718/**
1819 * @package ActiveCollab\Id\Controller\Base
@@ -74,14 +75,29 @@ public function encode($action_result, ServerRequestInterface $request, Response
7475 return $ response ->write (json_encode ($ action_result ))->withStatus (200 );
7576
7677 // Exception
77- } elseif ($ action_result instanceof Exception) {
78+ } elseif ($ action_result instanceof Throwable || $ action_result instanceof Exception) {
7879 return $ this ->encodeException ($ action_result , $ response );
7980 } else {
80- return $ response ;
81+ return $ this -> onNoEncoderApplied ( $ action_result , $ request , $ response) ;
8182 }
8283 }
8384
8485 /**
86+ * Call this function when no response encoding is applied by the encode() method.
87+ *
88+ * @param mixed $action_result
89+ * @param ServerRequestInterface $request
90+ * @param ResponseInterface $response
91+ * @return ResponseInterface
92+ */
93+ protected function onNoEncoderApplied ($ action_result , ServerRequestInterface $ request , ResponseInterface $ response )
94+ {
95+ return $ response ;
96+ }
97+
98+ /**
99+ * Encode and return status response.
100+ *
85101 * @param StatusResponse $action_result
86102 * @param ResponseInterface $response
87103 * @return ResponseInterface
@@ -92,11 +108,13 @@ private function encodeStatus(StatusResponse $action_result, ResponseInterface $
92108 }
93109
94110 /**
95- * @param Exception $exception
96- * @param ResponseInterface $response
111+ * Encode and return exception.
112+ *
113+ * @param Throwable|Exception $exception
114+ * @param ResponseInterface $response
97115 * @return ResponseInterface
98116 */
99- private function encodeException (Exception $ exception , ResponseInterface $ response )
117+ private function encodeException ($ exception , ResponseInterface $ response )
100118 {
101119 $ error = ['message ' => $ exception ->getMessage (), 'type ' => get_class ($ exception )];
102120
0 commit comments