Handle a model not found exception and build return data.

@param Exception $e
@return mixed
173233
protected function handleModelNotFoundException(Exception $e)
    {
        // Get the status code and build the message.
        $statusCode = 404;
        $reflection = new \ReflectionClass($e->getModel());
        $message = $reflection->getShortName() . ' not found';

        return $this->generateResponse('', null, $statusCode, Status::FAIL, $message);
    }
887984
public function getErrorResponse(Exception $e, ErrorBag $errorBag)
    {
        if (config('app.debug')) {
            throw $e;
        }

        return parent::getErrorResponse($e, $errorBag);
    }
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();
    }
215871
protected function convertExceptionToResponse(Exception $e)
    {
        return SymfonyResponse::create(
            $this->renderExceptionContent($e),
            $this->isHttpException($e) ? $e->getStatusCode() : 500,
            $this->isHttpException($e) ? $e->getHeaders() : []
        );
    }
867985
protected function prepareException(Exception $e)
    {
        $e = parent::prepareException($e);

        if ($e instanceof MethodNotAllowedHttpException) {
            $e = new HttpException(405, 'Method not allowed', $e->getPrevious(), $e->getHeaders(), $e->getCode());
        }

        return $e;
    }
1016695
public function handle(Exception $e)
    {
        $this->exception = $e;
        $this->notify($e);
        return $this->record($e);
    }
25028
protected function returnException(Exception $e)
    {
        $key = snake_case(class_basename($e));
        $message = $this->translate($key, $e->getMessage());

        return new ExceptionReply($message, [], $e);
    }
920663
protected function renderException($request, Exception $e)
    {
        return $this->app[ExceptionHandler::class]->render($request, $e);
    }
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
        );
    }
1073310
protected function handleValidationException(Exception $e)
    {
        // Get the status code and build the message.
        $statusCode = 422;
        $message = $e->getMessage();
        $errorData = $e->getResponse()->getData();

        return $this->generateResponse('errors', $errorData, $statusCode, Status::FAIL, $message);
    }
37948
protected function renderExceptionContent(Exception $e)
    {
        try {
            return config('app.debug') && class_exists(Whoops::class)
                        ? $this->renderExceptionWithWhoops($e)
                        : $this->renderExceptionWithSymfony($e, config('app.debug'));
        } catch (Exception $e) {
            return $this->renderExceptionWithSymfony($e, config('app.debug'));
        }
    }
386197
public function findModel($model)
    {
        $file = $this->basePath("app/Model/$model.php");
        if($this->findFile($file)) {
            return $file;
        }
        else {
            throw new Exception($file." Model file is missing.");
            exit;
        }
    }
743585
public function handleMicroServiceException(Exception $e)
    {
        // Determine the status code and message to return.
        if ($e instanceof HttpException) {
            return $this->handleHttpException($e);
        } elseif ($e instanceof ModelNotFoundException) {
            return $this->handleModelNotFoundException($e);
        } elseif ($e instanceof ValidationException) {
            return $this->handleValidationException($e);
        } else {
            return $this->handleGenericException($e);
        }
    }
1356978
protected static function getErrors (\Exception $e) {
        $errors = $e->getMessage();
        while ($e = $e->getPrevious()) {
            $errors .= "<br>" . $e->getMessage();
        }

        return $errors;
    }
1035668
private function handleException($app, Exception $e)
    {
        $status = $e->getCode();
        $statusText = \Slim\Http\Response::getMessageForCode($status);

        if ($statusText === null) {
            $status = 500;
            $statusText = 'Internal Server Error';
        }

        $app->response->setStatus($status);
        $app->response->headers->set('Content-Type', 'application/json');
        $app->response->setBody(json_encode(array(
            'status' => $status,
            'statusText' => preg_replace('/^[0-9]+ (.*)$/', '$1', $statusText),
            'description' => $e->getMessage(),
        )));

        $app->response()->finalize();

        return $app->response();
    }
875600
protected function convertDatabaseException(\Exception $e)
	{
		if($e instanceof DatabaseException)
		{
			return $e;
		}
		
		// Check for PDO exception in order to determine FK violation:
		$cause = $e;
		
		if(!$cause instanceof \PDOException)
		{
			while(NULL !== ($cause = $cause->getPrevious()))
			{
				if($cause instanceof \PDOException)
				{
					break;
				}
			}
		}
		
		if($cause instanceof \PDOException)
		{
			switch($this->driverName)
			{
				case DB::DRIVER_MYSQL:
					return $this->convertMySqlException($cause, $e);
				case DB::DRIVER_POSTGRESQL:
					return $this->convertPostgresException($cause, $e);
				case DB::DRIVER_SQLITE:
					return $this->convertSqliteException($cause, $e);
			}
		}
		
		return new DatabaseException($e->getMessage(), 0, $e);
	}
196912
private function handleException(\Exception $e)
    {
        trigger_error($e->getMessage() . "\n" . $e->getTraceAsString(), E_USER_WARNING);
        if ($e instanceof Error) {
            return '<strong>' . $e->getRawMessage() . '</strong>';
        }

        return '';
    }