Convert a Exception to a simple array.

@param Exception $e
@return array
772740
private function convertException(Exception $e)
    {
        $data = [];
        $data['message']  = $e->getMessage();
        $data['code']     = $e->getCode();
        $data['trace']    = $this->filterTrace($e->getTrace());
        $data['status']   = Response::HTTP_INTERNAL_SERVER_ERROR;

        // determine the relevant file
        $firstTraceEntry = !empty($data['trace']) ? reset($data['trace']) : null;
        if ($firstTraceEntry !== null && isset($firstTraceEntry['file']) && isset($firstTraceEntry['line'])) {
            $data['file'] = $firstTraceEntry['file'];
            $data['line'] = $firstTraceEntry['line'];
        }
        else {
            $data['file'] = $e->getFile();
            $data['line'] = $e->getLine();
        }

        if ($e instanceof HttpException) {
            $data['status'] = $e->getStatusCode() ?: Response::HTTP_INTERNAL_SERVER_ERROR;
        }
        else if ($e instanceof QueryException) {
            $data['ansiCode']   = isset($e->errorInfo[0]) ? $e->errorInfo[0] : null;
            $data['driverCode'] = isset($e->errorInfo[1]) ? $e->errorInfo[1] : null;
            if (!empty($e->errorInfo[2])) {
                $data['message'] = $e->errorInfo[2];
            }
            $data['sql'] = $e->getDump();
        }

        return $data;
    }
309418
public function getExceptionArray($exception)
    {
        $_message = 'Uknonwn exception object, not instance of \Exception.';
        $_file = 'unknown';
        $_line = 0;
        $_trace = [];
        $_previousException = [];
        
        if (is_object($exception)) {
            $prev = $exception->getPrevious();
            
            if (!empty($prev)) {
                $_previousException = [
                    'message' => $prev->getMessage(),
                    'file' => $prev->getFile(),
                    'line' => $prev->getLine(),
                    'trace' => $this->buildTrace($prev),
                ];
            }
            
            $_trace = $this->buildTrace($exception);
            $_message = $exception->getMessage();
            $_file = $exception->getFile();
            $_line = $exception->getLine();
        } elseif (is_string($exception)) {
            $_message = 'exception string: ' . $exception;
        } elseif (is_array($exception)) {
            $_message = isset($exception['message']) ? $exception['message'] : 'exception array dump: ' . print_r($exception, true);
            $_file = isset($exception['file']) ? $exception['file'] : __FILE__;
            $_line = isset($exception['line']) ? $exception['line'] : __LINE__;
        }

        return [
            'message' => $_message,
            'file' => $_file,
            'line' => $_line,
            'requestUri' => isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null,
            'serverName' => isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null,
            'date' => date('d.m.Y H:i'),
            'trace' => $_trace,
            'previousException' => $_previousException,
            'ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null,
            'get' => isset($_GET) ? ArrayHelper::coverSensitiveValues($_GET, $this->sensitiveKeys) : [],
            'post' => isset($_POST) ? ArrayHelper::coverSensitiveValues($_POST, $this->sensitiveKeys) : [],
            'bodyParams' => Yii::$app instanceof Application ? ArrayHelper::coverSensitiveValues(Yii::$app->request->bodyParams) : [],
            'session' => isset($_SESSION) ? ArrayHelper::coverSensitiveValues($_SESSION, $this->sensitiveKeys) : [],
            'server' => isset($_SERVER) ? ArrayHelper::coverSensitiveValues($_SERVER, $this->sensitiveKeys) : [],
            'profiling' => Yii::getLogger()->profiling,
            'logger' => Yii::getLogger()->messages,
        ];
    }
831336
protected function buildFromException(\Exception $e)
    {
        $rs = [
            'return' => false, 'status' => 500,
            'exception' => [
                'msg'  => $e->getMessage(),
                'code' => $e->getCode(),
                'file' => $e->getFile(),
                'line' => $e->getLine(),
            ],
        ];

        return $this->packResult($rs);
    }
139494
protected function mailException(Exception $exception)
    {
        $data = [
            'exception' => $exception,
            'toEmail' => config('laravelEmailExceptions.ErrorEmail.toEmailAddress'),
            'fromEmail' => config('laravelEmailExceptions.ErrorEmail.fromEmailAddress'),
        ];

        Mail::send('laravelEmailExceptions::emailException', $data, function ($message) {

            $default = 'An Exception has been thrown on '.
                config('app.name', 'unknown').' ('.config('app.env', 'unknown').')';
            $subject = config('laravelEmailExceptions.ErrorEmail.emailSubject') ?: $default;

            $message->from(config('laravelEmailExceptions.ErrorEmail.fromEmailAddress'))
                ->to(config('laravelEmailExceptions.ErrorEmail.toEmailAddress'))
                ->subject($subject);
        });
    }
449373
protected function extractExceptionData(Exception $exception)
    {
        $data = [
            'exception' => get_class($exception),
            'message'   => $exception->getMessage(),
            'code'      => $exception->getCode(),
            'file'      => $exception->getFile(),
            'line'      => $exception->getLine(),
            'trace'     => []
        ];

        foreach ($exception->getTrace() as $item) {
            if (isset($item['args']) && is_array($item['args'])) {
                $item['args'] = $this->cleanTraceArgs($item['args']);
            }
            $data['trace'][] = $item;
        }

        return $data;
    }
347904
protected function convertExceptionToResponse(Exception $e)
    {
        $headers = $this->isHttpException($e) ? $e->getHeaders() : [];
        $statusCode = $this->isHttpException($e) ? $e->getStatusCode() : 500;

        try {
            $content = config('app.debug') && class_exists(Whoops::class)
                ? $this->renderExceptionWithWhoops($e)
                : $this->renderExceptionWithSymfony($e, config('app.debug'));
        } catch (Exception $e) {
            $content = $content ?? $this->renderExceptionWithSymfony($e, config('app.debug'));
        }

        return SymfonyResponse::create(
            $content,
            $statusCode,
            $headers
        );
    }
563634
protected function normalizeException(Throwable $e, int $depth = 0): array
    {
        $data = [
            'class' => Utils::getClass($e),
            'message' => $e->getMessage(),
            'code' => $e->getCode(),
            'file' => $e->getFile().':'.$e->getLine(),
        ];

        if ($this->includeStacktraces) {
            $trace = $e->getTrace();
            foreach ($trace as $frame) {
                if (isset($frame['file'])) {
                    $data['trace'][] = $frame['file'].':'.$frame['line'];
                } elseif (isset($frame['function']) && $frame['function'] === '{closure}') {
                    // We should again normalize the frames, because it might contain invalid items
                    $data['trace'][] = $frame['function'];
                } else {
                    // We should again normalize the frames, because it might contain invalid items
                    $data['trace'][] = $this->normalize($frame);
                }
            }
        }

        if ($previous = $e->getPrevious()) {
            $data['previous'] = $this->normalizeException($previous, $depth + 1);
        }

        return $data;
    }
1092446
protected function exceptionMessage(\Exception $e)
    {
        return get_class($e) . ' in ' . $e->getFile() . ':' . $e->getLine() . ' "' . $e->getMessage() . '"';
    }
252609
public function parseException($e, $message = '')
    {
        $response = [
            'code' => $e->getCode(),
            'response' => $message . ' ' . $e->getMessage(),
            'error' => (bool)true
        ];
        return $response;
    }
1127824
protected function exception(array $e): \Closure
    {
        return function () use ($e) {
            $exClass = $e[0];
            $message = $e[1];

            throw new $exClass($message);
        };
    }
1006427
private function serializeException(\Exception $exception)
    {
        return [
            'message' => $exception->getMessage(),
            'code' => $exception->getCode(),
            'trace' => $exception->getTrace(),
            'file' => $exception->getFile(),
            'line' => $exception->getLine(),
            'previous' => null !== $exception->getPrevious() ? $this->serializeException($exception->getPrevious()) : null,
        ];
    }
670455
protected function getExceptionExamplePosition(Exception $exception)
    {
        $refl = $exception->getCause();
        foreach ($exception->getTrace() as $call) {
            if (!isset($call['file'])) {
                continue;
            }

            if (!empty($refl) && $refl->getFilename() === $call['file']) {
                return array($call['file'], $call['line']);
            }
        }

        return array($exception->getFile(), $exception->getLine());
    }
772599
public function convert(\Exception $ex)
    {
        $exceptions = array();

        do {
            $exceptions[] = $this->convertException($ex);
        } while (null !== $ex = $ex->getPrevious());

        return $exceptions;
    }
1057340
protected function __getExceptionTemplate($e)
        {
            $config = $this->sm->get('Config');
            $config = isset($config['view_manager'])
            && (
                is_array($config['view_manager'])
                || $config['view_manager'] instanceof \ArrayAccess
            )
                ? $config['view_manager']
                : array();

            // TODO get default layout by static method or via config
            $exceptionTemplate = 'spec/error'; //default

            $exClass = get_class($e);
            while($exClass) {
                if (isset($config['layout_exception']) && isset($config['layout_exception'][$exClass])) {
                    $exceptionTemplate = $config['layout_exception'][$exClass];
                    break;
                }

                $exClass = get_parent_class($exClass);
            }

            return $exceptionTemplate;
        }
1230024
protected function convertExceptionToArray($exception)
    {
        if (!YII_DEBUG && !$exception instanceof UserException && !$exception instanceof HttpException) {
            $exception = new HttpException(500, Yii::t('yii', 'An internal server error occurred.'));
        }

        $array = [
            'code' => $exception->getCode(),
            'data' => new \stdClass(),
            'message' => $exception->getMessage(),
        ];
        if ($exception instanceof HttpException) {
            $array['code'] = $exception->statusCode;
        }
        if($exception instanceof ApiException && !empty($exception->model)){
            $array = [
                'code' => 422,
                'data' => $exception->model->getErrors(),
                'message' => 'Data Validation Failed.',
            ];
        }
        if (YII_DEBUG) {
            $array['type'] = get_class($exception);
            if (!$exception instanceof UserException) {
                $array['file'] = $exception->getFile();
                $array['line'] = $exception->getLine();
                $array['stack-trace'] = explode("\n", $exception->getTraceAsString());
                if ($exception instanceof \yii\db\Exception) {
                    $array['error-info'] = $exception->errorInfo;
                }
            }
            if (($prev = $exception->getPrevious()) !== null) {
                $array['previous'] = $this->convertExceptionToArray($prev);
            }
        }

        return $array;
    }
705538
protected function prepareReplacements(Exception $exception): array
    {
        $statusCode = $this->getStatusCode($exception);

        if (!$message = $exception->getMessage()) {
            $message = sprintf('%d %s', $statusCode, Response::$statusTexts[$statusCode]);
        }

        $replacements['message'] = $message;

        if ($code = $exception->getCode()) {
            $replacements['code'] = $code;
        }

        $replacements['status_code'] = $statusCode;


        if ($exception instanceof ApiException && $exception->apiMessage->getErrors()) {
            $replacements['errors'] = $exception->apiMessage->getErrors();
        }


        if ($exception instanceof ApiException && null !== $exception->apiMessage->getHypermedia()) {
            $replacements += $exception->apiMessage->getHypermedia();
        }



        if ($this->runningInDebugMode()) {
            $replacements['debug'] = [
                'line'  => $exception->getLine(),
                'file'  => $exception->getFile(),
                'code'  => $exception->getCode(),
                'class' => \get_class($exception),
                'trace' => explode("\n", $exception->getTraceAsString()),
            ];
        }

        return array_merge($replacements, $this->replacements);
    }
216748
private function linearize(Throwable $e): array
    {
        $previous = $e->getPrevious();

        if (is_null($previous)) return [$e];

        return array_merge($this->linearize($previous), [$e]);
    }