mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #12613 from appwrite/fix/builds-worker-mask-internal-errors
fix(builds): mask internal errors from user-visible build logs
This commit is contained in:
2 files changed
+59
-14
No files matched your search
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Platform\Modules\Functions\Workers;
|
||||
|
||||
use Appwrite\Extend\Exception;
|
||||
|
||||
/**
|
||||
* A build failure whose message is safe to surface in user-visible build logs.
|
||||
*
|
||||
* Every build error the worker raises intentionally is a BuildException, so the
|
||||
* failure handler can show its message to users while masking any other
|
||||
* throwable (e.g. database or executor failures) behind a generic message.
|
||||
*/
|
||||
class BuildException extends Exception
|
||||
{
|
||||
public function __construct(
|
||||
?string $message = null,
|
||||
string $type = Exception::BUILD_FAILED,
|
||||
?\Throwable $previous = null,
|
||||
) {
|
||||
parent::__construct($type, $message, previous: $previous);
|
||||
}
|
||||
}
|
||||
@@ -195,6 +195,7 @@ class Builds extends Action
|
||||
|
||||
$startTime = DateTime::now();
|
||||
$durationStart = \microtime(true);
|
||||
$phaseStart = $durationStart;
|
||||
|
||||
$resourceKey = match ($resource->getCollection()) {
|
||||
'functions' => 'functionId',
|
||||
@@ -215,7 +216,7 @@ class Builds extends Action
|
||||
}
|
||||
|
||||
if ($isResourceBlocked($project, $resource->getCollection() === 'functions' ? RESOURCE_TYPE_FUNCTIONS : RESOURCE_TYPE_SITES, $resource->getId())) {
|
||||
throw new \Exception('Resource is blocked');
|
||||
throw new BuildException('Resource is blocked');
|
||||
}
|
||||
|
||||
$log->addTag('deploymentId', $deployment->getId());
|
||||
@@ -226,7 +227,7 @@ class Builds extends Action
|
||||
}
|
||||
|
||||
if ($resource->getCollection() === 'functions' && empty($deployment->getAttribute('entrypoint', ''))) {
|
||||
throw new \Exception('Entrypoint for your Appwrite Function is missing. Please specify it when making deployment or update the entrypoint under your function\'s "Settings" > "Configuration" > "Entrypoint".');
|
||||
throw new BuildException('Entrypoint for your Appwrite Function is missing. Please specify it when making deployment or update the entrypoint under your function\'s "Settings" > "Configuration" > "Entrypoint".');
|
||||
}
|
||||
|
||||
$version = $this->getVersion($resource);
|
||||
@@ -297,6 +298,9 @@ class Builds extends Action
|
||||
$github->initializeVariables($providerInstallationId, $privateKey, $githubAppId);
|
||||
}
|
||||
|
||||
Span::add('timings.setup', \round(\microtime(true) - $phaseStart, 3));
|
||||
$phaseStart = \microtime(true);
|
||||
|
||||
try {
|
||||
if (! $isVcsEnabled) {
|
||||
// Non-VCS + Template
|
||||
@@ -406,7 +410,7 @@ class Builds extends Action
|
||||
$exit = Console::execute($gitCloneCommand, '', $stdout, $stderr);
|
||||
|
||||
if ($exit !== 0) {
|
||||
throw new \Exception('Unable to clone code repository: ' . $stderr);
|
||||
throw new BuildException('Unable to clone code repository: ' . $stderr);
|
||||
}
|
||||
|
||||
// Local refactoring for function folder with spaces
|
||||
@@ -501,7 +505,7 @@ class Builds extends Action
|
||||
}
|
||||
|
||||
if ($directorySize > $sizeLimit && $sizeLimit !== 0) {
|
||||
throw new \Exception('Repository directory size should be less than ' . number_format($sizeLimit / (1000 * 1000), 2) . ' MBs.');
|
||||
throw new BuildException('Repository directory size should be less than ' . number_format($sizeLimit / (1000 * 1000), 2) . ' MBs.');
|
||||
}
|
||||
|
||||
Console::execute('find ' . \escapeshellarg($tmpDirectory) . ' -type d -name ".git" -exec rm -rf {} +', '', $stdout, $stderr);
|
||||
@@ -539,6 +543,9 @@ class Builds extends Action
|
||||
$this->runGitAction('processing', $github, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform);
|
||||
}
|
||||
|
||||
Span::add('timings.source', \round(\microtime(true) - $phaseStart, 3));
|
||||
$phaseStart = \microtime(true);
|
||||
|
||||
/** Request the executor to build the code... */
|
||||
$updated = $dbForProject->updateDocuments('deployments', new Document([
|
||||
'status' => 'building',
|
||||
@@ -739,7 +746,7 @@ class Builds extends Action
|
||||
$span?->set('build.runtime.timed_out', true);
|
||||
$span?->set('build.runtime.error_type', $error::class);
|
||||
$span?->set('build.runtime.error_message', $error->getMessage());
|
||||
$err = new AppwriteException(AppwriteException::BUILD_TIMEOUT, previous: $error);
|
||||
$err = new BuildException(type: AppwriteException::BUILD_TIMEOUT, previous: $error);
|
||||
} catch (\Throwable $error) {
|
||||
$span?->set('build.runtime.error_type', $error::class);
|
||||
$span?->set('build.runtime.error_message', $error->getMessage());
|
||||
@@ -849,6 +856,9 @@ class Builds extends Action
|
||||
}),
|
||||
]);
|
||||
|
||||
Span::add('timings.build', \round(\microtime(true) - $phaseStart, 3));
|
||||
$phaseStart = \microtime(true);
|
||||
|
||||
$latestDeployment = $dbForProject->getDocument('deployments', $deploymentId);
|
||||
if ($latestDeployment->getAttribute('status') === 'canceled') {
|
||||
$this->finalizeCanceledDeployment($deployment->getId(), $dbForProject, $queueForRealtime);
|
||||
@@ -865,7 +875,7 @@ class Builds extends Action
|
||||
$buildSizeLimit = $plan['buildSize'] * 1000 * 1000;
|
||||
}
|
||||
if ($response['size'] > $buildSizeLimit && $buildSizeLimit !== 0) {
|
||||
throw new \Exception('Build size should be less than ' . number_format($buildSizeLimit / (1000 * 1000), 2) . ' MBs.');
|
||||
throw new BuildException('Build size should be less than ' . number_format($buildSizeLimit / (1000 * 1000), 2) . ' MBs.');
|
||||
}
|
||||
|
||||
$deployment->setAttribute('buildPath', $response['path']);
|
||||
@@ -896,7 +906,7 @@ class Builds extends Action
|
||||
Span::add('build.adapter', $deployment->getAttribute('adapter'));
|
||||
Span::add('build.fallback_file', $deployment->getAttribute('fallbackFile'));
|
||||
} elseif ($adapter === 'ssr' && $detection->getName() === 'static') {
|
||||
throw new \Exception('Adapter mismatch. Detected: ' . $detection->getName() . ' does not match with the set adapter: ' . $adapter);
|
||||
throw new BuildException('Adapter mismatch. Detected: ' . $detection->getName() . ' does not match with the set adapter: ' . $adapter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1127,6 +1137,8 @@ class Builds extends Action
|
||||
|
||||
Span::add('build.screenshot_queued', true);
|
||||
}
|
||||
|
||||
Span::add('timings.finalize', \round(\microtime(true) - $phaseStart, 3));
|
||||
} catch (\Throwable $th) {
|
||||
if ($dbForProject->getDocument('deployments', $deploymentId)->getAttribute('status') === 'canceled') {
|
||||
$this->finalizeCanceledDeployment($deployment->getId(), $dbForProject, $queueForRealtime);
|
||||
@@ -1134,14 +1146,19 @@ class Builds extends Action
|
||||
return;
|
||||
}
|
||||
|
||||
Span::add('build.error.stage', 'deployment');
|
||||
Span::add('build.error.type', $th::class);
|
||||
Span::add('build.error.message', $th->getMessage());
|
||||
Span::add('build.error.file', $th->getFile());
|
||||
Span::add('build.error.line', $th->getLine());
|
||||
$isUserFacing = $th instanceof BuildException;
|
||||
$message = $isUserFacing
|
||||
? $th->getMessage()
|
||||
: 'An internal error occurred while building. Please try again, and contact support if the problem persists.';
|
||||
|
||||
// Record user-facing failures on the span here, since they're not
|
||||
// re-raised to the harness (which records internal errors via setError).
|
||||
if ($isUserFacing) {
|
||||
Span::add('build.exception.type', $th->getType());
|
||||
Span::add('build.exception.message', $th->getMessage());
|
||||
}
|
||||
|
||||
// Color message red
|
||||
$message = $th->getMessage();
|
||||
if (! \str_contains($message, '')) {
|
||||
$message = '[31m' . $message;
|
||||
}
|
||||
@@ -1181,6 +1198,11 @@ class Builds extends Action
|
||||
if ($isVcsEnabled) {
|
||||
$this->runGitAction('failed', $github, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform, true);
|
||||
}
|
||||
|
||||
// Let the worker harness record internal errors via the span and logger.
|
||||
if (! $isUserFacing) {
|
||||
throw $th;
|
||||
}
|
||||
} finally {
|
||||
$queueForRealtime
|
||||
->setPayload($deployment->getArrayCopy())
|
||||
@@ -1277,7 +1299,7 @@ class Builds extends Action
|
||||
default => null
|
||||
};
|
||||
if (\is_null($runtime)) {
|
||||
throw new \Exception('Runtime "' . $resource->getAttribute('runtime', '') . '" is not supported');
|
||||
throw new BuildException('Runtime "' . $resource->getAttribute('runtime', '') . '" is not supported');
|
||||
}
|
||||
|
||||
return $runtime;
|
||||
|
||||
Reference in new issue
Block a user