Skip to content

Commit

Permalink
Merge pull request #1616 from brefphp/fix-1520
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli authored Aug 14, 2023
2 parents 3a4a335 + 05bc84d commit 7b7c336
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/ConsoleRuntime/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function run(): void
$appRoot = getenv('LAMBDA_TASK_ROOT');
$handlerFile = $appRoot . '/' . getenv('_HANDLER');
if (! is_file($handlerFile)) {
$lambdaRuntime->failInitialization("Handler `$handlerFile` doesn't exist");
$lambdaRuntime->failInitialization("Handler `$handlerFile` doesn't exist", 'Runtime.NoSuchHandler');
}

/** @phpstan-ignore-next-line */
Expand Down
5 changes: 3 additions & 2 deletions src/FpmRuntime/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Bref\Bref;
use Bref\LazySecretsLoader;
use Bref\Runtime\LambdaRuntime;
use RuntimeException;
use Throwable;

/**
Expand All @@ -27,14 +28,14 @@ public static function run(): void
$appRoot = getenv('LAMBDA_TASK_ROOT');
$handlerFile = $appRoot . '/' . getenv('_HANDLER');
if (! is_file($handlerFile)) {
$lambdaRuntime->failInitialization("Handler `$handlerFile` doesn't exist");
$lambdaRuntime->failInitialization("Handler `$handlerFile` doesn't exist", 'Runtime.NoSuchHandler');
}

$phpFpm = new FpmHandler($handlerFile);
try {
$phpFpm->start();
} catch (Throwable $e) {
$lambdaRuntime->failInitialization('Error while starting PHP-FPM', $e);
$lambdaRuntime->failInitialization(new RuntimeException('Error while starting PHP-FPM: ' . $e->getMessage(), 0, $e));
}

/** @phpstan-ignore-next-line */
Expand Down
2 changes: 1 addition & 1 deletion src/FunctionRuntime/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function run(): void
try {
$handler = $container->get(getenv('_HANDLER'));
} catch (Throwable $e) {
$lambdaRuntime->failInitialization($e->getMessage());
$lambdaRuntime->failInitialization($e, 'Runtime.NoSuchHandler');
}

$loopMax = getenv('BREF_LOOP_MAX') ?: 1;
Expand Down
52 changes: 34 additions & 18 deletions src/Runtime/LambdaRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,42 +239,57 @@ private function signalFailure(string $invocationId, Throwable $error): void
*
* @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html#runtimes-api-initerror
*
* @phpstan-param 'Runtime.NoSuchHandler'|'Runtime.UnknownReason' $lambdaInitializationReason
* @phpstan-return never-returns
*/
public function failInitialization(string $message, ?Throwable $error = null): void
{
public function failInitialization(
string|Throwable $error,
string $lambdaInitializationReason = 'Runtime.UnknownReason',
): void {
// Log the exception in CloudWatch
echo "$message\n";
if ($error) {
if ($error instanceof Exception) {
$errorMessage = get_class($error) . ': ' . $error->getMessage();
} else {
$errorMessage = $error->getMessage();
}
if ($error instanceof Throwable) {
$traceAsArray = explode(PHP_EOL, $error->getTraceAsString());
$data = [
'errorMessage' => $error->getMessage(),
'errorType' => get_class($error),
'stackTrace' => $traceAsArray,
];
printf(
"Fatal error: %s in %s:%d\nStack trace:\n%s",
$errorMessage,
"Fatal error: %s in %s:%d\n %s\n",
get_class($error) . ': ' . $error->getMessage(),
$error->getFile(),
$error->getLine(),
$error->getTraceAsString()
json_encode([
'message' => $error->getMessage(),
'type' => get_class($error),
'stackTrace' => $traceAsArray,
], JSON_THROW_ON_ERROR),
);
} else {
$data = [
'errorMessage' => $error,
'errorType' => 'Internal',
'stackTrace' => [],
];
echo "Fatal error: $error\n";
}

echo "The function failed to start. AWS Lambda will restart the process, do not be surprised if you see the error message twice.\n";

$url = "http://$this->apiUrl/2018-06-01/runtime/init/error";
$this->postJson($url, [
'errorMessage' => $message . ' ' . ($error ? $error->getMessage() : ''),
'errorType' => $error ? get_class($error) : 'Internal',
'stackTrace' => $error ? explode(PHP_EOL, $error->getTraceAsString()) : [],
$this->postJson($url, $data, [
"Lambda-Runtime-Function-Error-Type: $lambdaInitializationReason",
]);

exit(1);
}

/**
* @throws ResponseTooBig
* @param string[] $headers
* @throws Exception
* @throws ResponseTooBig
*/
private function postJson(string $url, mixed $data): void
private function postJson(string $url, mixed $data, array $headers = []): void
{
/** @noinspection JsonEncodingApiUsageInspection */
$jsonData = json_encode($data);
Expand All @@ -296,6 +311,7 @@ private function postJson(string $url, mixed $data): void
curl_setopt($this->curlHandleResult, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData),
...$headers,
]);

$body = curl_exec($this->curlHandleResult);
Expand Down

0 comments on commit 7b7c336

Please sign in to comment.