From 76e18a2226cb71440aca57d364f6e469294313f9 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 30 Jun 2026 10:47:08 +0530 Subject: [PATCH] Fix build failure error classification --- .../Modules/Functions/Workers/Builds.php | 9 ++++ src/Executor/Exception.php | 17 +++++++ src/Executor/Executor.php | 18 +++++--- .../Services/Sites/SitesCustomServerTest.php | 44 ++++++++++++++++++- 4 files changed, 80 insertions(+), 8 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index fb4e691926..0f9a9dfdf8 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -17,6 +17,7 @@ use Appwrite\Usage\Context; use Appwrite\Utopia\Response\Model\Deployment; use Appwrite\Vcs\Comment; use Exception; +use Executor\Exception as ExecutorException; use Executor\Exception\Timeout as ExecutorTimeout; use Executor\Executor; use Swoole\Coroutine as Co; @@ -757,6 +758,14 @@ class Builds extends Action $span?->set('build.runtime.error_type', $error::class); $span?->set('build.runtime.error_message', $error->getMessage()); $err = new BuildException(type: AppwriteException::BUILD_TIMEOUT, previous: $error); + } catch (ExecutorException $error) { + $span?->set('build.runtime.error_type', $error::class); + $span?->set('build.runtime.error_message', $error->getMessage()); + $span?->set('build.runtime.executor_error_type', $error->getType()); + + $err = \in_array($error->getType(), [ExecutorException::BUILD_FAILED, ExecutorException::RUNTIME_FAILED], true) + ? new BuildException($error->getMessage(), previous: $error) + : $error; } catch (\Throwable $error) { $span?->set('build.runtime.error_type', $error::class); $span?->set('build.runtime.error_message', $error->getMessage()); diff --git a/src/Executor/Exception.php b/src/Executor/Exception.php index b799d22567..70499bd4c3 100644 --- a/src/Executor/Exception.php +++ b/src/Executor/Exception.php @@ -4,4 +4,21 @@ namespace Executor; class Exception extends \Exception { + public const string GENERAL_UNKNOWN = 'general_unknown'; + public const string BUILD_FAILED = 'build_failed'; + public const string RUNTIME_FAILED = 'runtime_failed'; + + public function __construct( + string $message = '', + int $code = 0, + ?\Throwable $previous = null, + private readonly string $type = self::GENERAL_UNKNOWN, + ) { + parent::__construct($message, $code, $previous); + } + + public function getType(): string + { + return $this->type; + } } diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 53c86f1287..1ca966416b 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -105,8 +105,9 @@ class Executor $status = $response['headers']['status-code']; if ($status >= 400) { - $message = \is_string($response['body']) ? $response['body'] : $response['body']['message']; - throw new ExecutorException($message, $status); + $message = \is_string($response['body']) ? $response['body'] : ($response['body']['message'] ?? ''); + $type = \is_array($response['body']) ? ($response['body']['type'] ?? ExecutorException::GENERAL_UNKNOWN) : ExecutorException::GENERAL_UNKNOWN; + throw new ExecutorException($message, $status, type: $type); } return $response['body']; @@ -165,7 +166,8 @@ class Executor } if ($status >= 400) { - throw new ExecutorException($message, $status); + $type = \is_array($response['body']) ? ($response['body']['type'] ?? ExecutorException::GENERAL_UNKNOWN) : ExecutorException::GENERAL_UNKNOWN; + throw new ExecutorException($message, $status, type: $type); } return $response['body']; @@ -248,8 +250,9 @@ class Executor $status = $response['headers']['status-code']; if ($status >= 400) { - $message = \is_string($response['body']) ? $response['body'] : $response['body']['message']; - throw new ExecutorException($message, $status); + $message = \is_string($response['body']) ? $response['body'] : ($response['body']['message'] ?? ''); + $type = \is_array($response['body']) ? ($response['body']['type'] ?? ExecutorException::GENERAL_UNKNOWN) : ExecutorException::GENERAL_UNKNOWN; + throw new ExecutorException($message, $status, type: $type); } $headers = $response['body']['headers'] ?? []; @@ -282,8 +285,9 @@ class Executor $status = $response['headers']['status-code']; if ($status >= 400) { - $message = \is_string($response['body']) ? $response['body'] : $response['body']['message']; - throw new ExecutorException($message, $status); + $message = \is_string($response['body']) ? $response['body'] : ($response['body']['message'] ?? ''); + $type = \is_array($response['body']) ? ($response['body']['type'] ?? ExecutorException::GENERAL_UNKNOWN) : ExecutorException::GENERAL_UNKNOWN; + throw new ExecutorException($message, $status, type: $type); } return $response['body']; diff --git a/tests/e2e/Services/Sites/SitesCustomServerTest.php b/tests/e2e/Services/Sites/SitesCustomServerTest.php index 7a5f578492..1732f1de24 100644 --- a/tests/e2e/Services/Sites/SitesCustomServerTest.php +++ b/tests/e2e/Services/Sites/SitesCustomServerTest.php @@ -2954,7 +2954,49 @@ final class SitesCustomServerTest extends Scope $this->assertEventually(function () use ($siteId, $deploymentId) { $deployment = $this->getDeployment($siteId, $deploymentId); $this->assertEquals('failed', $deployment['body']['status'], 'Deployment status does not match: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT)); - $this->assertStringContainsString('Error:', (string) $deployment['body']['buildLogs'], 'Deployment logs do not match: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT)); + + $buildLogs = \preg_replace('/\x1b\[[0-9;]*m/', '', (string) $deployment['body']['buildLogs']) ?? ''; + $errorLogs = \array_values(\array_filter(\array_map( + static fn (string $line): string => \trim(\preg_replace('/^\[\d{2}:\d{2}:\d{2}\]\s+\[open-runtimes\]\s*/', '', $line) ?? ''), + \explode("\n", $buildLogs), + ), static fn (string $line): bool => \str_starts_with($line, 'Error:'))); + + $this->assertSame([ + "Error: No source code found. Ensure your source isn't empty.", + ], $errorLogs, 'Deployment logs should expose the exact user build error: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT)); + }, 100000, 500); + + $this->cleanupSite($siteId); + } + + public function testExitOneBuildShowsBuildFailure(): void + { + $siteId = $this->setupSite([ + 'siteId' => ID::unique(), + 'name' => 'Exit One Site', + 'framework' => 'other', + 'buildRuntime' => 'node-22', + 'outputDirectory' => './', + 'buildCommand' => 'exit 1', + ]); + + $deployment = $this->createDeployment($siteId, [ + 'code' => $this->packageSite('static-single-file'), + 'activate' => true + ]); + $this->assertEquals(202, $deployment['headers']['status-code']); + + $deploymentId = $deployment['body']['$id']; + $this->assertNotEmpty($deploymentId); + + $this->assertEventually(function () use ($siteId, $deploymentId) { + $deployment = $this->getDeployment($siteId, $deploymentId); + $this->assertEquals('failed', $deployment['body']['status'], 'Deployment status does not match: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT)); + $this->assertStringContainsString( + 'Build command exited with code 1.', + (string) $deployment['body']['buildLogs'], + 'Deployment logs do not match: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT) + ); }, 100000, 500); $this->cleanupSite($siteId);