Skip to content

Commit 668ad59

Browse files
committed
Print error stack for 5xx FunctionError only
1 parent a28fe47 commit 668ad59

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/LeanCloud/Engine/LeanEngine.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,11 @@ protected function dispatch($method, $url) {
571571
try {
572572
$this->__dispatch($method, $url);
573573
} catch (FunctionError $ex) {
574-
error_log($ex);
575-
error_log($ex->getTraceAsString());
574+
$status = (int) $ex->status;
575+
if ( $status >= 500) {
576+
error_log($ex);
577+
error_log($ex->getTraceAsString());
578+
}
576579
$this->renderError("{$ex->getMessage()}", $ex->getCode(), $ex->status);
577580
} catch (CloudException $ex) {
578581
error_log($ex);

test/engine/LeanEngineTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,15 @@ public function test_messageReceived() {
212212
$this->assertEquals(false, $resp["result"]["drop"]);
213213
}
214214

215+
public function testFunctionError() {
216+
try {
217+
$this->request("/1.1/functions/customError", "POST", array());
218+
} catch (CloudException $ex) {
219+
$this->assertEquals("My custom error.", $ex->getMessage());
220+
$this->assertEquals(1, $ex->getCode());
221+
$this->assertEquals(500, $ex->status);
222+
}
223+
}
224+
215225
}
216226

test/engine/index.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use LeanCloud\Client;
66
use LeanCloud\Engine\LeanEngine;
77
use LeanCloud\Engine\Cloud;
8+
use LeanCloud\Engine\FunctionError;
89
use LeanCloud\Storage\CookieStorage;
910

1011
Client::initialize(
@@ -25,6 +26,10 @@
2526
return "hello {$params['name']}";
2627
});
2728

29+
Cloud::define("customError", function($params, $user) {
30+
throw new FunctionError("My custom error.", 1, 500);
31+
});
32+
2833
Cloud::define("_messageReceived", function($params, $user){
2934
if ($params["convId"]) {
3035
return array("drop" => false);

0 commit comments

Comments
 (0)