Correcciones

This commit is contained in:
Juan Pablo Vial
2025-03-03 21:41:43 -03:00
parent 5f69069aa0
commit aeeca65d94
9 changed files with 122 additions and 43 deletions

View File

@ -16,9 +16,10 @@ trait withJson
return [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'file' => $this->parseFilename($exception->getFile()),
'line' => $exception->getLine(),
'trace' => $exception->getTraceAsString()
'trace' => $this->parseStack($exception->getTraceAsString()),
'previous' => ($exception->getPrevious() instanceof Throwable) ? $this->parseError($exception->getPrevious()) : ''
];
}
public function withError(ResponseInterface $response, Throwable $exception): ResponseInterface
@ -37,4 +38,46 @@ trait withJson
return $response;
}
protected function parseFilename(string $filename): string
{
return str_replace('/code/', '', $filename);
}
protected function parseStack(string|array $stack): array
{
if (is_string($stack)) {
$stack = explode(PHP_EOL, $stack);
}
$output = [];
foreach ($stack as $line) {
$index = substr($line, 1, strpos($line, ' ') - 1);
$content = substr($line, strpos($line, ' ') + 1);
if (str_contains($line, '{main}')) {
$output [] = [
'stack' => $index,
'message' => $content
];
continue;
}
if (str_starts_with($content, '[internal function]')) {
$content = substr($content, strlen('[internal function]: '));
$output [] = [
'stack' => $index,
'type' => 'internal function',
'message' => $content
];
continue;
}
$fileData = substr($content, 0, strpos($content, ' '));
$file = substr($fileData, 0, strrpos($fileData, '('));
$fileLine = (int) substr($fileData, strrpos($fileData, '(') + 1, -2);
$error = substr($content, strlen($fileData) + 1);
$output []= [
'stack' => $index,
'file' => $this->parseFilename($file),
'line' => $fileLine,
'error' => $error
];
}
return $output;
}
}