final public static function toArray(Throwable $throwable, bool $traceAsString = false, int $depth = 512) : array
    {
        $result = [
            'type' => get_class($throwable),
            'message' => $throwable->getMessage(),
            'code' => $throwable->getCode(),
            'file' => $throwable->getFile(),
            'line' => $throwable->getLine(),
            'trace' => $traceAsString ? $throwable->getTraceAsString() : $throwable->getTrace(),
            'previous' => null,
        ];

        if ($throwable->getPrevious() !== null && --$depth) {
            $result['previous'] = self::toArray($throwable->getPrevious(), $traceAsString, $depth);
        }

        return $result;
    }