diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 443260ea90..18fa6b822b 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -1878,7 +1878,7 @@ App::post('/v1/functions/:functionId/executions') } /** Update execution status */ - $status = $executionResponse['statusCode'] >= 400 ? 'failed' : 'completed'; + $status = $executionResponse['statusCode'] >= 500 ? 'failed' : 'completed'; $execution->setAttribute('status', $status); $execution->setAttribute('responseStatusCode', $executionResponse['statusCode']); $execution->setAttribute('responseHeaders', $headersFiltered); diff --git a/app/controllers/general.php b/app/controllers/general.php index d532110df3..95e6dc18df 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -302,7 +302,7 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo } /** Update execution status */ - $status = $executionResponse['statusCode'] >= 400 ? 'failed' : 'completed'; + $status = $executionResponse['statusCode'] >= 500 ? 'failed' : 'completed'; $execution->setAttribute('status', $status); $execution->setAttribute('responseStatusCode', $executionResponse['statusCode']); $execution->setAttribute('responseHeaders', $headersFiltered); diff --git a/src/Appwrite/Platform/Workers/Functions.php b/src/Appwrite/Platform/Workers/Functions.php index 78671bfeb0..8ade08dc2d 100644 --- a/src/Appwrite/Platform/Workers/Functions.php +++ b/src/Appwrite/Platform/Workers/Functions.php @@ -494,7 +494,7 @@ class Functions extends Action logging: $function->getAttribute('logging', true), ); - $status = $executionResponse['statusCode'] >= 400 ? 'failed' : 'completed'; + $status = $executionResponse['statusCode'] >= 500 ? 'failed' : 'completed'; $headersFiltered = []; foreach ($executionResponse['headers'] as $key => $value) { diff --git a/src/Appwrite/Utopia/Response/Filters/V18.php b/src/Appwrite/Utopia/Response/Filters/V18.php index d0aa680e3b..6485b6f0ba 100644 --- a/src/Appwrite/Utopia/Response/Filters/V18.php +++ b/src/Appwrite/Utopia/Response/Filters/V18.php @@ -24,6 +24,12 @@ class V18 extends Filter protected function parseExecution(array $content) { + if(!empty($content['status']) && !empty($content['statusCode'])) { + if($content['status'] === 'completed' && $content['statusCode'] >= 400 && $content['statusCode'] < 500) { + $content['status'] = 'failed'; + } + } + unset($content['scheduledAt']); return $content; } diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index e3148752c8..0b6bf985c1 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -802,6 +802,23 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals('', $execution['body']['logs']); $this->assertLessThan(10, $execution['body']['duration']); + $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $data['functionId'] . '/executions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'async' => false, + 'path' => '/?code=400' + ]); + $this->assertEquals(201, $execution['headers']['status-code']); + $this->assertEquals('completed', $execution['body']['status']); + $this->assertEquals(400, $execution['body']['responseStatusCode']); + + $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $data['functionId'] . '/executions/' . $execution['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + $this->assertEquals(204, $execution['headers']['status-code']); + return array_merge($data, ['executionId' => $executionId]); } diff --git a/tests/resources/functions/php/index.php b/tests/resources/functions/php/index.php index d5328c40e1..e8e5eb67d5 100644 --- a/tests/resources/functions/php/index.php +++ b/tests/resources/functions/php/index.php @@ -1,6 +1,8 @@ req->query['code'] ?? '200'; + return $context->res->json([ 'APPWRITE_FUNCTION_ID' => \getenv('APPWRITE_FUNCTION_ID') ?: '', 'APPWRITE_FUNCTION_NAME' => \getenv('APPWRITE_FUNCTION_NAME') ?: '', @@ -11,5 +13,5 @@ return function ($context) { 'APPWRITE_REGION' => \getenv('APPWRITE_REGION') ?: '', 'UNICODE_TEST' => "êä", 'GLOBAL_VARIABLE' => \getenv('GLOBAL_VARIABLE') ?: '' - ]); + ], \intval($statusCode)); }; diff --git a/tests/unit/Utopia/Response/Filters/V18Test.php b/tests/unit/Utopia/Response/Filters/V18Test.php index c4011c08a1..5396779b77 100644 --- a/tests/unit/Utopia/Response/Filters/V18Test.php +++ b/tests/unit/Utopia/Response/Filters/V18Test.php @@ -34,7 +34,7 @@ class V18Test extends TestCase ], [ ] - ] + ], ]; } @@ -60,6 +60,46 @@ class V18Test extends TestCase ], [ ] + ], + 'update 404 status' => [ + [ + 'statusCode' => '404', + 'status' => 'completed' + ], + [ + 'statusCode' => '404', + 'status' => 'failed' + ] + ], + 'update 400 status' => [ + [ + 'statusCode' => '400', + 'status' => 'completed' + ], + [ + 'statusCode' => '400', + 'status' => 'failed' + ] + ], + 'dont update 200 status' => [ + [ + 'statusCode' => '200', + 'status' => 'completed' + ], + [ + 'statusCode' => '200', + 'status' => 'completed' + ] + ], + 'dont update 500 status' => [ + [ + 'statusCode' => '500', + 'status' => 'failed' + ], + [ + 'statusCode' => '500', + 'status' => 'failed' + ] ] ]; }