Render an exception into an HTTP response.

@param  \Illuminate\Http\Request  $request
@param  \Exception  $e
@return \Illuminate\Http\Response
966003
public function render($request, Exception $e)
	{
		if ($e instanceof HttpException) {
			$response = $e->response();
			
			if ($request->header('Origin')) {
				Cors::attachHeaders($response);
				$response->headers->set('Access-Control-Allow-Origin', $request->header('Origin'));
			}
			
			return $response;
		}
		
		return parent::render($request, $e);
	}
52461
protected function getErrorResponse(Exception $e)
    {
        switch (get_class($e)) {
            case ForbiddenException::class:
                $response = $this->forbidden(
                    new ErrorBag([new Error('Forbidden', $e->getMessage())])
                );
                break;
            default:
                $response = $this->errorResponse(
                    new ErrorBag([new Error('Bad Request', 'Request could not be served.')])
                );
        }

        return $response;
    }
1303570
protected function handleException(Exception $e)
    {
        $handler = $this->app->make(ExceptionHandler::class);

        $handler->report($e);

        try {
            $handler->renderForConsole(
                $this->app->make(OutputInterface::class), $e
            );
        } catch (Throwable $e) {
            //
        }
    }
972104
public function render($request, Exception $exception)
    {
        $exception = $this->prepareException($exception);

        if ($exception instanceof HttpResponseException) {
            return $exception->getResponse();
        } elseif ($exception instanceof AuthenticationException) {
            return $this->unauthenticated($request, $exception);
        } elseif ($exception instanceof ValidationException) {
            return $this->convertValidationExceptionToResponse($exception, $request);
        } elseif ($this->isHttpException($exception) && $this->customErrorHtmlTemplateExists($exception)) {
            return $this->prepareResponse($request, $exception);
        }

        $flattened = FlattenException::create($exception);

        $code = $flattened->getStatusCode();
        $headers = $flattened->getHeaders();
        $response = $this->getContent($exception, $code, $request);

        // If it's already a response, return that.
        if (!$response instanceof BaseResponse) {
            $response = new Response($this->getContent($exception, $code, $request), $code, $headers);
        }

        return $response;
    }
112396
public function render($e)
    {
        if ($e instanceof Throwable) {
            $e = $this->throwableToException($e);
        }
        
        if ($this->shouldntBacktrace($e)) {
            return with(new ExceptionHandler(env('APP_DEBUG')))->handle($e);
        }
        
        return with(new SymfonyExceptionHandler(env('APP_DEBUG')))->handle($e);
    }
131375
protected function renderException($output, \Exception $e)
    {
        $this->laravel[ExceptionHandler::class]->renderForConsole($output, $e);
    }
737008
public static function run( \Exception $e )
	{
		$config = config('system.errors');
		if (($config['display'] || is_a( $e, Message::class )) && !($e instanceof QueryException)) {
			$obj = new Response( self::getErrorObject($e), ($e->getCode() ? $e->getCode() : 500) );
		} else {
			$obj = new Response( $e->getMessage(), 500 );
		}
		if ($config['log']) {
			self::logObject($e);
		}
		self::sendResponse( $obj );
	}
336685
protected function renderHttpException(HttpExceptionInterface $e)
    {
        $status = $e->getStatusCode();

        if ($this->isCmsWebRequest() &&  view()->exists("cms::errors.{$status}")) {
            $flatException = FlattenException::create($e);

            return response()
                ->view(
                    "cms::errors.{$status}",
                    [ 'exception' => $flatException ],
                    $status,
                    $e->getHeaders()
                );
        }

        return parent::renderHttpException($e);
    }
447101
protected function renderHttpException(HttpException $e)
    {
        $status = $e->getStatusCode();

        $paths = collect(view()->getFinder()->getPaths());

        view()->replaceNamespace('errors', $paths->map(function ($path) {
            return "{$path}/errors";
        })->push(__DIR__.'/views')->all());

        if (view()->exists($view = "errors::{$status}")) {
            return response()->view($view, [
                'exception' => $e
            ], $status, $e->getHeaders());
        }

        return $this->convertExceptionToResponse($e);
    }
652014
public function render($request, Exception $e)
    {
        // Verificar se deve responser como API
        if (app()->runningWebApi()) {
            return $this->renderApi($request, $e);
        }

        // Exceções Http
        if ($e instanceof HttpException) {
            if (! is_null($this->viewService)) {
                // Veriifcar se foi implemetado uma view no app
                $view = 'errors.' . $e->getStatusCode();
                if ($this->getViewService()->exists($view)) {
                    return $this->getViewService()->make($view)->render();
                }

                // Verificar se foi implememtado uma view no theme
                $view = 'theme::errors.' . $e->getStatusCode();
                if ($this->getViewService()->exists($view)) {
                    return $this->getViewService()->make($view)->render();
                }
            } else {
                $message = $e->getMessage();
                $message = ($message == '') ? $e->getStatusCode() : $message;
                return 'error: ' . $message;
            }
        }

        // ErrorsException
        if ($e instanceof ErrorsException) {
            $back = redirect()->back()->withInput()->withErrors($e->getErrors(), $e->getMessage());

            return $back;
        }


        return 'error: ' . $e->getMessage();
    }
121050
public function render($request, Exception $e)
    {
        $packageResponse = [];
        $statusCode = 500;
        if (($message = $e->getMessage())) {
            $packageResponse['title'] = $message;
        }
        if ($e instanceof OAuthException) {
            $e = new AccessDeniedHttpException($e->getMessage());
        }
        if ($e instanceof HttpException) {
            $statusCode = $e->getStatusCode();
        }
        if ($e instanceof ErrorMessageBagException && $e->hasErrors()) {
            $packageResponse['errors'] = $e->getErrors();
        }

        return response()->json($packageResponse, $statusCode);
    }
206663
private function handleForBrowser(Exception $e)
    {
//        $request = request();
//        if ($e instanceof ModelNotFoundException) {
//            $e = new NotFoundHttpException($e->getMessage(), $e);
//        }
//        elseif ($e instanceof AuthorizationException) {
//            $e = new HttpException(403, $e->getMessage());
//        }
//
//        if ($e instanceof HttpResponseException) {
//            return $e->getResponse();
//        }
//        if ($e instanceof AuthenticationException) {
//            header('Location: ' . url('auth/login'));
//            exit;
//        }
//        else if ($e instanceof ValidationException) {
//            return $this->convertValidationExceptionToResponse($e, $request);
//        }
//        else if ($request->isAjax() || $request->wantsJson()) {
//            /** @var HttpException $e */
//            return response()->json($e->getMessage(), $this->isHttpException($e) ? $e->getStatusCode() : 400);
//        }

        $data = $this->convertException($e);

        // set error title and subtitle

        if ($e instanceof HttpException) {
            // set the HTTP Response Status Text as error title
            $data['title'] = http_status_text($data['status']);

            // set the Exception class and the HTTP Response Status Code as subtitle
            $data['subtitle'] = get_class($e) . ', Status Code ' . $data['status'];
        }
        else if ($e instanceof QueryException) {
            // set title
            $data['title'] = 'SQL Error';

            // set the Exception class and error codes as subtitle
            $data['subtitle'] = get_class($e) . ' (Exception code: ' . $data['code'] . ')';
            if ($data['ansiCode'] !== null) {
                $data['subtitle'] .= ', ANSI SQLSTATE Error Code: ' . $data['ansiCode'];
            }
            if ($data['driverCode'] !== null) {
                $data['subtitle'] .= ', Driver-specific Error Code: ' . $data['driverCode'];
            }
        }
        else {
            // set error title
            $data['title'] = 'Error';

            // // set the Exception class and the Exception code as subtitle
            $data['subtitle'] = get_class($e) . ' (Exception code: ' . $data['code'] . ')';
        }

        // render the source code

        if ($e instanceof QueryException) {
            // parse the sql line from the message if exists
            if (preg_match('/at line (\d)$/', $data['message'], $match)) {
                $sqlLine = $match[1];
            }
            else {
                $sqlLine = 0;
            }
            // set the SQL Statement as source
            $data['source'] = $this->renderSQL($data['sql'], $sqlLine);
        }
        else {
            // set the PHP Code as source
            $data['source'] = $this->renderPHP($data['file'], $data['line']);
        }

        // render the trace

        $data['trace'] = $this->renderTrace($data['trace']);

        // strip the filename

        $data['file'] = $this->stripFile($data['file']);

        // choose the template

        $view = config('app.debug') ? 'errors.debug' : 'errors.default';
        if (!config('app.debug') && view()->exists('errors.' . $data['status'])) {
            $view = 'errors.' . $data['status'];
        }

        // send the response

        $headers = ($e instanceof HttpException) ? $e->getHeaders() : [];

        return response()->view($view, $data, $data['status'], $headers)->send();
    }
1273652
public function render($request, Exception $e)
    {
        if (app()->environment() == 'testing') {
            throw $e;
        }
        if ($e instanceof NotFoundHttpException) {
            if ($request->method() == "GET") {
                return array_key_exists('extension', pathinfo(array_key_exists('path', $parsed = parse_url($request->url()))
                        ? $parsed['path']
                        : null))
                    ? errors(['Not Found'], 404)
                    : html()->generate(); 
            } 
            return errors(['Not Found'], 404);
        }
        if ($e instanceof MethodNotAllowedHttpException) {
            if ($request->method() == "GET") {
                return html()->generate();
            }  
            return parent::render($request, $e);          
        }
        if (app()->environment() == 'local') {
            return errors([$e->getMessage(), $e->getFile() . ': line ' . $e->getLine()], 500);
        }
        return errors([$e->getMessage()], 500);
    }
332912
public static function sendException(\Exception $e, ServiceRequestInterface $request = null)
    {
        $response = static::exceptionToServiceResponse($e);

        return ResponseFactory::sendResponse($response, $request);
    }
335505
protected function renderException($output, \Exception $e)
    {
        $this->app[ExceptionHandler::class]->renderForConsole($output, $e);
    }
860943
protected function prepareJsonResponse($request, \Exception $e)
    {
        $headers = $this->isHttpException($e) ? $e->getHeaders() : [];

        return JsonResponse::create(
            $this->message,
            $this->statusCode ?: 500,
            $headers
        );
    }
292029
protected function editActionException(\Exception $e)
    {
        $this->handleException($e);
        $id = $this->get('request')->get($this->admin->getIdParameter());
        $object = $this->admin->getObject($id);

        return $this->redirectTo($object);
    }