Prepare the replacements array by gathering the keys and values.

@param \Exception $exception

@return array
191799
protected function prepareReplacements(Exception $exception)
    {
        $statusCode = $this->getStatusCode($exception);

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

        $replacements = [
            ':message' => $message,
            ':status_code' => $statusCode,
        ];

        if ($exception instanceof MessageBagErrors && $exception->hasErrors()) {
            $replacements[':errors'] = $exception->getErrors();
        }

        if ($exception instanceof ValidationException) {
            $replacements[':errors'] = $exception->errors();
            $replacements[':status_code'] = $exception->status;
        }

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

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

            // Attach trace of previous exception, if exists
            if (! \is_null($exception->getPrevious())) {
                $currentTrace = $replacements[':debug']['trace'];

                $replacements[':debug']['trace'] = [
                    'previous' => \explode("\n", $exception->getPrevious()->getTraceAsString()),
                    'current' => $currentTrace,
                ];
            }
        }

        return \array_merge($replacements, $this->replacements);
    }
589842
protected function getCode(\Exception $exception)
    {
        return $exception instanceof \ErrorException ?
            $this->getHumanErrorCode($exception->getSeverity()) :
            $exception->getCode()
        ;
    }
437746
public function transform(\Throwable $exception): \Throwable
    {
        if (false === $exception instanceof BadResponseException) {
            return new ApiException($exception);
        }

        try {
            /** @var BadResponseException $exception */
            $errorResponse = $exception->getResponse();
            if (null === $errorResponse) {
                return new ApiException($exception);
            }
            $errorStatusCode = $errorResponse->getStatusCode();
            $bodyString = (string) $errorResponse->getBody();
            $bodyArray = json_decode($bodyString, true);
            $errorStatusName = '';
            if (\is_array($bodyArray) && array_key_exists('error', $bodyArray)) {
                $errorStatusName = $bodyArray['error'];
            }
        } catch (\Exception $e) {
            return new ApiException($exception);
        }

        return $this->transformByStatusCodeAndName($exception, $errorStatusCode, $errorStatusName);
    }
293889
protected function _addException(Exception $exception)
    {
        $this->_hasErrors = true;
        $this->_errorsList[$exception->getCode()] = [
            'message'   => $exception->getMessage(),
            'line'      => $exception->getLine(),
            'file'      => $exception->getFile(),
            'trace'     => $exception->getTraceAsString(),
        ];

        return $this;
    }
33468
private function getExceptionHandlerMethods(Exception $exception)
    {
        $class = new ReflectionClass($this);

        $handlerMethods = [];
        foreach ($class->getMethods(ReflectionMethod::IS_PROTECTED) as $method) {
            $param = current($method->getParameters());
            if ($param && $param->getClass() && $param->getClass()->isInstance($exception)) {
                $handlerMethods[] = $method;
            }
        }

        return $handlerMethods;
    }
961593
static function asArray(Exception $e)
    {
        return [
            'exception'   => get_class($e),
            'code'        => $e->getCode(),
            'message'     => $e->getMessage(),
            'file'        => $e->getFile(),
            'line'        => $e->getLine(),
            'hash'        => self::hash($e),
            'status_code' => self::statusCode($e)
        ];
    }
272452
public function formatException(\Exception $exception, array $record = array())
    {
        $record['msg'] = $this->normalizeException($exception);

        return $this->format($record);
    }
696291
public function getExceptions(\Throwable $exception): array
    {
        $exc = $exception;
        do {
            $exc_data = [
                'value' => $this->serializer->serialize($exc->getMessage()),
                'type' => get_class($exc),
            ];

            /**'exception'
             * Exception::getTrace doesn't store the point at where the exception
             * was thrown, so we have to stuff it in ourselves. Ugh.
             */
            $trace = $exc->getTrace();
            $frame_where_exception_thrown = [
                'file' => $exc->getFile(),
                'line' => $exc->getLine(),
            ];

            array_unshift($trace, $frame_where_exception_thrown);

            $exc_data['stacktrace'] = [
                'frames' => $this->getStackTraceFrames($trace),
            ];

            $exceptions[] = $exc_data;
        } while ($exc = $exc->getPrevious());

        return array_reverse($exceptions);
    }
365762
protected function getErrorType(\Exception $exception)
    {
        $errorType = get_class($exception);

        $pos = strrpos($errorType, '\\');
        if ($pos !== false) {
            $errorType = substr($errorType, $pos);
        }

        $errorType = Str::replaceLast($errorType, 'Exception', '');

        if (!$errorType) {
            $errorType = 'Unknown';
        }

        return $errorType;
    }
604586
protected static function makeExceptionContent(\Exception $exception)
    {
        if ($exception instanceof DfException) {
            return ['error' => $exception->toArray()];
        }

        $errorInfo['code'] = ($exception->getCode()) ?: ServiceResponseInterface::HTTP_INTERNAL_SERVER_ERROR;
        $errorInfo['message'] = htmlentities($exception->getMessage());

        if (config('app.debug', false)) {
            $trace = $exception->getTraceAsString();
            $trace = str_replace(["\n", "#"], ["", "<br>"], $trace);
            $traceArray = explode("<br>", $trace);
            $cleanTrace = [];
            foreach ($traceArray as $k => $v) {
                if (!empty($v)) {
                    $cleanTrace[] = $v;
                }
            }
            $errorInfo['trace'] = $cleanTrace;
        }

        return ['error' => $errorInfo];
    }
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());
    }
570962
protected function genericResponse(Exception $exception)
    {
        $replacements = $this->prepareReplacements($exception);

        $response = $this->newResponseArray();

        array_walk_recursive($response, function (&$value, $key) use ($exception, $replacements) {
            if (starts_with($value, ':') && isset($replacements[$value])) {
                $value = $replacements[$value];
            }
        });

        $response = $this->recursivelyRemoveEmptyReplacements($response);

        return new Response($response, $this->getExceptionStatusCode($exception), $this->getHeaders($exception));
    }
1290047
public function setException(Exception $exception)
    {
        $this->exception = $exception;
        return $this->raiseStatus(self::Status_Exception);
    }
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,
        ];
    }
172636
protected function convertExceptionToArray(Exception $e)
    {
        return config('app.debug') ? [
            'message' => $e->getMessage(),
            'exception' => get_class($e),
            'file' => $e->getFile(),
            'line' => $e->getLine(),
            'trace' => collect($e->getTrace())->map(function ($trace) {
                return Arr::except($trace, ['args']);
            })->all(),
        ] : [
            'message' => $this->isHttpException($e) ? $e->getMessage() : 'Server Error',
        ];
    }
431174
public function track(Exception $exception)
    {
        return $this->getModel()->newQuery()->firstOrCreate([
            'code'    => $this->getCode($exception),
            'message' => $exception->getMessage(),
        ])->getKey();
    }
810995
public function handleException( Exception $exception )
    {
        // restore previous exception handler
        restore_exception_handler();

        if ( $exception instanceof Exception )
        {
            $error = [
                'file'    => $exception->getFile(),
                'line'    => $exception->getLine(),
                'message' => $exception->getMessage(),
                'trace'   => $this->getBacktrace( $exception->getTrace() ),
            ];
        }
        else
        {
            return;
        }

        if ( NULL !== $this->exceptionCallback )
        {
            call_user_func_array( $this->exceptionCallback, $error );
        }

        return TRUE;
    }