简体   繁体   English

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

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

I'm handling exceptions at Handler.php , but when I provoke certain exception on purpose, the report method not only writes that exception, it also writes at least 24 time the same Session store error , because I'm using the session to retrieve the a customize error message. 我正在处理Handler.php异常,但是当我故意引发某些异常时,报告方法不仅会写出该异常,它还会写入至少24次相同的Session store error ,因为我正在使用会话来检索自定义错误消息。 My report method only has the default parent::report($exception); 我的报告方法只有默认的parent::report($exception); . The log writes the Query error I'm making on purpose, but also a lot of those Session store error 日志写了我正在故意制作的查询错误,但也有很多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();
}

I think all those \\Session or the errorOrigin variable retrieved create the error Session store error , but I need them, is there a logic/syntax error? 我认为所有那些\\Session或者errorOrigin变量检索都会产生错误Session store error ,但我需要它们,是否存在逻辑/语法错误? I need some help, because the log is growing giant, and it doesn't have sense to save all those errors, neither just don't save them. 我需要一些帮助,因为日志变得越来越大,并且没有意义来保存所有这些错误,也没有保存它们。

Also found out that this error is happening on every page, event at login. 还发现此错误发生在每个页面,登录时的事件。 I login with username instead of email (of course the login already works and it logins properly with username). 我使用用户名而不是电子邮件登录(当然登录已经有效,并且使用用户名正确登录)。 Does it have something to do about it? 它有什么关系吗? I refresh login page without doing anything, not event trying to log in and it also saves an error at my log, but my app keep working. 我没有做任何事情刷新登录页面,没有尝试登录的事件,它也在我的日志中保存错误,但我的应用程序继续工作。

This time I'm provoking on purpose the error from the non exist database table, this is a summary of the errors that are saved to the log when i ONCE provoke the error. 这次我故意挑出来自非存在数据库表的错误,这是当i ONCE引发错误时保存到日志的错误的摘要。 As you will see, the session store error or similar repeats, but some show a uncaught RunTimeException . 正如您将看到的,会话存储错误或类似的重复,但有些显示未捕获的RunTimeException Also added the stack-trace of the last one. 还添加了最后一个的堆栈跟踪。 The session store error repeats at least like 24 times or even more. 会话存储错误至少重复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()

The error is being thrown by the call to $request->session() because it doesn't have a session object . 调用$request->session()抛出错误,因为它没有会话对象

A request object is given a session via the StartSession middleware . 通过StartSession中间件为请求对象提供会话。 This middleware is included in the web middleware group, that's automatically given to all routes inside routes/web.php . 此中间件包含在web中间件组中,它自动提供给routes/web.php所有路由。

It's possible you're using a route that hasn't established any session use, like an API route or just one where you forgot the web middleware group. 您可能正在使用未建立任何会话使用的路由,例如API路由或只是忘记Web中间件组的路由。

And because an error is occurring in the error handler, it's becoming a loop. 并且因为错误处理程序中发生错误,所以它变成了循环。 ERROR > HANDLER > ERROR > HANDLER > and so on... ERROR> HANDLER> ERROR> HANDLER>等等......

Typically I'd recommend handling all expected scenarios - including possible exceptions - in the controller. 通常我建议在控制器中处理所有预期的场景 - 包​​括可能的例外。 That way you aren't debugging the error handler wondering why a controller is giving a redirect you might not be expecting. 这样你就不会调试错误处理程序,想知道控制器为什么会给你一个你可能没想到的重定向。

That said, handling app-specific exceptions in the error handler and returning redirects or other custom responses is okay, so long as you're targeting specific exceptions and not using redirects and sessions for ALL exceptions. 也就是说,在错误处理程序中处理特定于应用程序的异常并返回重定向或其他自定义响应是可以的,只要您针对特定异常并且不对所有异常使用重定向和会话。

An example of how you might handle that: 一个如何处理它的例子:

/**
 * 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