Skip to content

Commit

Permalink
Add JSON data to initialization error logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Aug 14, 2023
1 parent 434ffd4 commit e042ba8
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Runtime/LambdaRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,29 +243,36 @@ private function signalFailure(string $invocationId, Throwable $error): void
*
* @phpstan-return never-returns
*/
public function failInitialization(string|Throwable $error, $lambdaInitializationReason = 'Runtime.UnknownReason'): void
{
public function failInitialization(
string|Throwable $error,
string $lambdaInitializationReason = 'Runtime.UnknownReason',
): void {
// Log the exception in CloudWatch
if ($error instanceof Throwable) {
$traceAsArray = explode(PHP_EOL, $error->getTraceAsString());
$data = [
'errorMessage' => $error->getMessage(),
'errorType' => get_class($error),
'stackTrace' => explode(PHP_EOL, $error->getTraceAsString()),
'stackTrace' => $traceAsArray,
];
printf(
"Fatal error: %s in %s:%d\nStack trace:\n%s",
"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 "$error\n";
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";
Expand Down

0 comments on commit e042ba8

Please sign in to comment.