Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
/**
4
 * Slim Framework (https://slimframework.com)
5
 *
6
 * @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
7
 */
8
 
9
declare(strict_types=1);
10
 
11
namespace Slim\Error\Renderers;
12
 
13
use Slim\Error\AbstractErrorRenderer;
14
use Throwable;
15
 
16
use function get_class;
17
use function json_encode;
18
 
19
use const JSON_PRETTY_PRINT;
20
use const JSON_UNESCAPED_SLASHES;
21
 
22
/**
23
 * Default Slim application JSON Error Renderer
24
 */
25
class JsonErrorRenderer extends AbstractErrorRenderer
26
{
27
    public function __invoke(Throwable $exception, bool $displayErrorDetails): string
28
    {
29
        $error = ['message' => $this->getErrorTitle($exception)];
30
 
31
        if ($displayErrorDetails) {
32
            $error['exception'] = [];
33
            do {
34
                $error['exception'][] = $this->formatExceptionFragment($exception);
35
            } while ($exception = $exception->getPrevious());
36
        }
37
 
38
        return (string) json_encode($error, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
39
    }
40
 
41
    /**
42
     * @return array<string|int>
43
     */
44
    private function formatExceptionFragment(Throwable $exception): array
45
    {
46
        /** @var int|string $code */
47
        $code = $exception->getCode();
48
        return [
49
            'type' => get_class($exception),
50
            'code' => $code,
51
            'message' => $exception->getMessage(),
52
            'file' => $exception->getFile(),
53
            'line' => $exception->getLine(),
54
        ];
55
    }
56
}