繁体   English   中英

检索并将数据保存到控制器的会话并在处理程序Laravel 5.5中检索时,会话存储错误

[英]Session store error when retrieving and saving data to the session at controller and retrieving at handler Laravel 5.5

我正在处理Handler.php异常,但是当我故意引发某些异常时,报告方法不仅会写出该异常,它还会写入至少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();
}

我认为所有那些\\Session或者errorOrigin变量检索都会产生错误Session store error ,但我需要它们,是否存在逻辑/语法错误? 我需要一些帮助,因为日志变得越来越大,并且没有意义来保存所有这些错误,也没有保存它们。

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

这次我故意挑出来自非存在数据库表的错误,这是当i ONCE引发错误时保存到日志的错误的摘要。 正如您将看到的,会话存储错误或类似的重复,但有些显示未捕获的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()

调用$request->session()抛出错误,因为它没有会话对象

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

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

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

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

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

一个如何处理它的例子:

/**
 * 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM