【问题标题】:Session store error when retrieving and saving data to the session at controller and retrieving at handler Laravel 5.5在控制器处检索数据并将其保存到会话并在处理程序 Laravel 5.5 处检索时,会话存储错误
【发布时间】:2019-10-22 08:15:30
【问题描述】:

我正在处理Handler.php 的异常,但是当我故意引发某些异常时,report 方法不仅会写入该异常,而且还会写入至少 24 次相同的Session store error,因为我正在使用会话以检索自定义错误消息。我的报告方法只有默认的parent::report($exception);。日志写了我故意犯的查询错误,还有很多Session store error

//render method at Handler.php 

public function render($request, Exception $exception)
    {

        if($request->session()->has('errorOrigin'))
        {
            $errorOrigin = $request->session()->pull('errorOrigin');
        } else{
            $errorOrigin = "";
        }

        //session()->forget('errorOrigin');
        //return dd(session());
        //return parent::render($request, $exception);

        //this return is just some trying
        //return parent::render($request, $exception);
        //return dd($exception);
        //return parent::render($request, $exception);

        //just in case someone throws it but don´t has the try catch at the code
        if($exception instanceof \App\Exceptions\CustomException) {
            //$request->session()->flash('message_type', 'negative');
            \Session::flash('message_type', 'negative');
            \Session::flash('message_icon', 'hide');
            \Session::flash('message_header', 'Success');
            \Session::flash('error', '¡Ha ocurrido un error ' . $errorOrigin . "!" 
            .' Si este persiste contacte al administrador del sistema');
            return redirect()->back();

        }elseif($exception instanceof \Illuminate\Database\QueryException) {
            \Session::flash('message_type', 'negative');
            \Session::flash('message_icon', 'hide');
            \Session::flash('message_header', 'Success');
            \Session::flash('error', '¡Ha ocurrido un error en la consulta ' . $errorOrigin);//this is he customized error message
            return back();

 }else{/*Original error handling*/
            return parent::render($request, $exception);
        }
    }


//Method at my controller
public function index(Request $request)
    {


            //custom message if this methods throw an exception
            \Session::put('errorOrigin', " mostrando los clientes");

                //on purpose error, that table doesn´t exist, so it causes the QueryException error
        DB::table('shdhgjd')->get();
}

我认为检索到的所有\SessionerrorOrigin 变量都会创建错误Session store error,但我需要它们,是否存在逻辑/语法错误?我需要一些帮助,因为日志越来越大,保存所有这些错误没有意义,也不保存它们。

还发现这个错误发生在每个页面上,登录时的事件。我使用用户名而不是电子邮件登录(当然登录已经有效,并且使用用户名正确登录)。它与它有什么关系吗?我没有做任何事情就刷新了登录页面,而不是尝试登录的事件,它还在我的日志中保存了一个错误,但我的应用程序继续工作。

这次我故意从不存在的数据库表中引发错误,这是我一次引发错误时保存到日志中的错误摘要。正如您将看到的,会话存储错误或类似的重复,但有些显示未捕获的RunTimeException。还添加了最后一个的堆栈跟踪。会话存储错误至少重复 24 次甚至更多。

[2019-06-06 19:55:59] local.ERROR: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'prisma.shdhgjd' doesn't exist (SQL: select * from `shdhgjd`) 

[2019-06-06 19:56:00] local.ERROR: Session store not set on request. {"exception":"[object] (RuntimeException(code: 0): Session store not set on request. at C:\\ProyectoPrisma_BS3\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Request.php:419)
[stacktrace]
"#0" C:\\ProyectoPrisma_BS3\\app\\Exceptions\\Handler.php(69): Illuminate\\Http\\Request->session()

[2019-06-06 19:56:00] local.ERROR: Session store not set on request. {"exception":"[object] (RuntimeException(code: 0): Session store not set on request. at C:\\ProyectoPrisma_BS3\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Request.php:419)
[stacktrace]

[2019-06-06 19:56:00] local.ERROR: Uncaught RuntimeException: Session store not set on request. in C:\ProyectoPrisma_BS3\vendor\laravel\framework\src\Illuminate\Http\Request.php:419
Stack trace:
"#0" C:\ProyectoPrisma_BS3\app\Exceptions\Handler.php(69): Illuminate\Http\Request->session()

【问题讨论】:

  • “会话存储错误”是生成的确切错误消息吗?如果没有,你能分享一下确切的信息吗?
  • @AkenRoberts,当我只导致一次查询异常时,已经上传了一小部分错误。感谢您的帮助!
  • 听起来该类无法访问 Session 并抛出一个错误循环,试图使用 back 将数据闪存到会话中。相反,请检查是否有 $request->session()->has('_previous') 然后您可以重定向到 $request->session()->get('_previous')['url'] 请参阅本节中的警告:laravel.com/docs/5.8/redirects#creating-redirects
  • @j.steelman 谢谢,但遗憾的是没有工作。但我发现一些有趣的事情发生在所有页面上,甚至在登录时(只是进入登录视图,而不是尝试记录的事件)。我会在问题本身更新一些信息。

标签: php laravel session report session-variables


【解决方案1】:

调用$request->session() 引发了错误,因为它是doesn't have a session object

通过StartSession middleware 为请求对象提供会话。此中间件包含在 web 中间件组中,它会自动分配给 routes/web.php 内的所有路由。

您可能正在使用尚未建立任何会话使用的路由,例如 API 路由或您忘记了 Web 中间件组的路由。

因为错误处理程序中发生了错误,所以它变成了一个循环。错误 > 处理程序 > 错误 > 处理程序 > 等等...

通常,我建议在控制器中处理所有预期的场景 - 包​​括可能的异常。这样你就不会调试错误处理程序,想知道为什么控制器会给出你可能没想到的重定向。

也就是说,在错误处理程序中处理特定于应用程序的异常并返回重定向或其他自定义响应是可以的,只要您针对特定异常而不是针对所有异常使用重定向和会话。

一个你可以如何处理的例子:

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $exception
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $exception)
{
    // Create a specific path for your custom exception.
    if ($exception instanceof MyCustomException) {
        // Want to use sessions? Check if they're available.
        if ($request->hasSession()) {
            // ...
            return \redirect()->back();
        }

        // Sessions weren't available on this request. Create a different response.
        return view('errors.no-session-view');
    }

    // Use the default render response for everything else.
    return parent::render($request, $exception);
}

【讨论】:

  • 我正在使用 laravel 5.5,我认为 web 中间件组适用于所有路由。我的都是像“get::('admin', 'AdminController@index'); 所以我认为所有路由都有会话对象,甚至是主页。我将创建一个独立的错误页面。我应该如何重定向到渲染中的那个视图?我不知道我是否不正确地使用渲染,我将它用作辅助,而不是在 catch 控制器方法中添加所有可能的异常,唯一的问题是我的控制器来自自定义异常。这是一种糟糕的编码习惯吗?非常感谢,非常感谢您的帮助
  • 如果您的路由都在默认的routes/web.php 文件中,那么除非您进行了更改,否则它们应该会自动应用 Web 中间件组。在应用该中间件组之前,您的错误可能已经发生。没有看到你的代码就不能肯定地说。我还编辑了我的答案以反映您的自定义异常处理。可以在处理程序中执行,只是在某些情况下可能会产生副作用。并且您应该始终对不使用任何特殊功能的意外异常进行后备。
  • 非常感谢!我一直在尝试并决定在异常处理方面最适合我的应用程序。我使处理程序尽可能独立,它失败的唯一可能方式是因为正在写入会话消息。我无法解决日志中的会话警告,即使在默认的 routes/web.php 中使用路由,所以我只是做了一个 if 使得这些特定警告不被写入在日志。再次感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2014-09-10
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
  • 2021-11-08
  • 1970-01-01
  • 2014-11-25
相关资源
最近更新 更多