@@ -335,7 +335,7 @@ private function processSession() {
335335 * @param string $method Request method
336336 * @param string $url Request url
337337 */
338- protected function dispatch ($ method , $ url ) {
338+ private function __dispatch ($ method , $ url ) {
339339 if (static ::$ useHttpsRedirect ) {
340340 $ this ->httpsRedirect ();
341341 }
@@ -393,41 +393,26 @@ protected function dispatch($method, $url) {
393393 // extract func params from path:
394394 // /1.1/call/{0}/{1}
395395 $ funcParams = explode ("/ " , ltrim ($ pathParts ["extra " ], "/ " ));
396- try {
397- if (count ($ funcParams ) == 1 ) {
398- // {1,1.1}/functions/{funcName}
399- $ this ->dispatchFunc ($ funcParams [0 ], $ json ,
400- $ pathParts ["endpoint " ] === "call " );
401- } else {
402- if ($ funcParams [0 ] == "onVerified " ) {
403- // {1,1.1}/functions/onVerified/sms
404- $ this ->dispatchOnVerified ($ funcParams [1 ], $ json );
405- } else if ($ funcParams [0 ] == "_User " &&
406- $ funcParams [1 ] == "onLogin " ) {
407- // {1,1.1}/functions/_User/onLogin
408- $ this ->dispatchOnLogin ($ json );
409- } else if ($ funcParams [0 ] == "BigQuery " ||
410- $ funcParams [0 ] == "Insight " ) {
411- // {1,1.1}/functions/Insight/onComplete
412- $ this ->dispatchOnInsight ($ json );
413- } else if (count ($ funcParams ) == 2 ) {
414- // {1,1.1}/functions/{className}/beforeSave
415- $ this ->dispatchHook ($ funcParams [0 ], $ funcParams [1 ], $ json );
416- }
396+ if (count ($ funcParams ) == 1 ) {
397+ // {1,1.1}/functions/{funcName}
398+ $ this ->dispatchFunc ($ funcParams [0 ], $ json ,
399+ $ pathParts ["endpoint " ] === "call " );
400+ } else {
401+ if ($ funcParams [0 ] == "onVerified " ) {
402+ // {1,1.1}/functions/onVerified/sms
403+ $ this ->dispatchOnVerified ($ funcParams [1 ], $ json );
404+ } else if ($ funcParams [0 ] == "_User " &&
405+ $ funcParams [1 ] == "onLogin " ) {
406+ // {1,1.1}/functions/_User/onLogin
407+ $ this ->dispatchOnLogin ($ json );
408+ } else if ($ funcParams [0 ] == "BigQuery " ||
409+ $ funcParams [0 ] == "Insight " ) {
410+ // {1,1.1}/functions/Insight/onComplete
411+ $ this ->dispatchOnInsight ($ json );
412+ } else if (count ($ funcParams ) == 2 ) {
413+ // {1,1.1}/functions/{className}/beforeSave
414+ $ this ->dispatchHook ($ funcParams [0 ], $ funcParams [1 ], $ json );
417415 }
418- } catch (FunctionError $ ex ) {
419- error_log ($ ex ->getMessage ());
420- error_log ($ ex ->getTraceAsString ());
421- $ this ->renderError ("Cloud function error: {$ ex ->getMessage ()}" , $ ex ->getCode ());
422- } catch (CloudException $ ex ) {
423- error_log ($ ex ->getMessage ());
424- error_log ($ ex ->getTraceAsString ());
425- $ this ->renderError ("Request to API failed: {$ ex ->getMessage ()}" , $ ex ->getCode ());
426- } catch (\Exception $ ex ) {
427- error_log ($ ex ->getMessage ());
428- error_log ($ ex ->getTraceAsString ());
429- $ this ->renderError ($ ex ->getMessage (),
430- $ ex ->getCode () ? $ ex ->getCode () : 1 );
431416 }
432417 }
433418 }
@@ -576,6 +561,36 @@ private function dispatchOnInsight($body) {
576561 $ this ->renderJSON (array ("result " => "ok " ));
577562 }
578563
564+ /**
565+ * Dispatch LeanEngine functions.
566+ *
567+ * @param string $method Request method
568+ * @param string $url Request url
569+ */
570+ protected function dispatch ($ method , $ url ) {
571+ try {
572+ $ this ->__dispatch ($ method , $ url );
573+ } catch (FunctionError $ ex ) {
574+ $ status = (int ) $ ex ->status ;
575+ if ( $ status >= 500 ) {
576+ error_log ($ ex );
577+ error_log ($ ex ->getTraceAsString ());
578+ }
579+ $ this ->renderError ("{$ ex ->getMessage ()}" , $ ex ->getCode (), $ ex ->status );
580+ } catch (CloudException $ ex ) {
581+ error_log ($ ex );
582+ error_log ($ ex ->getTraceAsString ());
583+ $ this ->renderError ("{$ ex ->getMessage ()}" , $ ex ->getCode (), $ ex ->status );
584+ } catch (\Exception $ ex ) {
585+ error_log ($ ex );
586+ error_log ($ ex ->getTraceAsString ());
587+ $ this ->renderError ($ ex ->getMessage (),
588+ $ ex ->getCode () ? $ ex ->getCode () : 1 ,
589+ // unhandled internal exception
590+ 500 );
591+ }
592+ }
593+
579594 /**
580595 * Start engine and process request
581596 */
0 commit comments