[英]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.