Skip to content

Commit

Permalink
Merge pull request #131 from amalodev/main
Browse files Browse the repository at this point in the history
Added option to configure number of backtraces in error message
  • Loading branch information
aarondfrancis authored May 15, 2024
2 parents d37804f + 6d706ff commit 5eb84ec
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Results/SettledResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,21 @@ public function toResponse($request)
}

/**
* @param ?int $numberOfBacktraces
*
* Throw an exception if there was an error, otherwise do nothing.
*
* @throws Exception
*/
public function throw()
public function throw(?int $numberOfBacktraces)
{
if (!$this->isError()) {
return $this;
}

throw new LambdaExecutionException(sprintf('Lambda Execution Exception for %s: "%s".', ...[
get_class($this->function),
$this->errorAsString()
$this->errorAsString($numberOfBacktraces)
]));
}

Expand Down Expand Up @@ -189,16 +191,16 @@ public function trace()
return Arr::get($this->body(), 'trace', []);
}

public function errorAsString()
public function errorAsString(int $numberOfBacktraces = 2)
{
if (!$this->isError()) {
return '';
}

$message = Arr::get($this->body(), 'errorMessage', 'Unknown error.');

// Only the first two backtraces (plus the error) for the string.
$trace = array_slice($this->trace(), 0, 3);
// Only the number of backtraces (plus the error) for the string.
$trace = array_slice($this->trace(), 0, $numberOfBacktraces + 1);
$trace = implode(' ', array_map('trim', $trace));

if ($trace) {
Expand Down

0 comments on commit 5eb84ec

Please sign in to comment.