Skip to content

Commit be9b821

Browse files
committed
Support add status code to FunctionError
1 parent d29123d commit be9b821

File tree

2 files changed

+55
-36
lines changed

2 files changed

+55
-36
lines changed

src/LeanCloud/Engine/FunctionError.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@
55
* Error thrown when invoking function error
66
*/
77
class FunctionError extends \Exception {
8-
public function __construct($message, $code = 1) {
8+
9+
/**
10+
* Http status code
11+
*/
12+
public $status;
13+
14+
public function __construct($message, $code = 1, $status = 400) {
915
parent::__construct($message, $code);
16+
$this->status = $status;
1017
}
1118

1219
public function __toString() {

src/LeanCloud/Engine/LeanEngine.php

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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,33 @@ 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+
error_log($ex);
575+
error_log($ex->getTraceAsString());
576+
$this->renderError("{$ex->getMessage()}", $ex->getCode(), $ex->status);
577+
} catch (CloudException $ex) {
578+
error_log($ex);
579+
error_log($ex->getTraceAsString());
580+
$this->renderError("{$ex->getMessage()}", $ex->getCode(), $ex->status);
581+
} catch (\Exception $ex) {
582+
error_log($ex);
583+
error_log($ex->getTraceAsString());
584+
$this->renderError($ex->getMessage(),
585+
$ex->getCode() ? $ex->getCode() : 1,
586+
// unhandled internal exception
587+
500);
588+
}
589+
}
590+
579591
/**
580592
* Start engine and process request
581593
*/

0 commit comments

Comments
 (0)