什么会导致错误在 Laravel 中没有消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36624864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:36:48  来源:igfitidea点击:

What would cause an error to have no message in Laravel

phplaravel

提问by cchapman

So this question is based more on why only part of an error is showing that the error itself (although it would be great if someone could also solve the error as well).

所以这个问题更多地基于为什么只有部分错误显示错误本身(尽管如果有人也能解决错误会很好)。

However, I have a logging function in Laravel that emails an error notification as well as logs it. (Admittedly, it's probably not the best practice, although it tends to serve its purpose). My code for sending the error email is as shown below...

但是,我在 Laravel 中有一个日志记录功能,可以通过电子邮件发送错误通知并将其记录下来。(诚​​然,这可能不是最佳实践,尽管它往往能达到目的)。我发送错误电子邮件的代码如下所示......

Exceptions\Handler.php

异常\Handler.php

public function report(Exception $e)
{
    \Mail::send('emails.error', ['error' => $e], function ($m) use ($e) {


        $m->from('[email protected]', 'MMC API | Laravel Error ');

        $m->to('[email protected]')
        ->cc('[email protected]')
        ->subject($e->getMessage());
    });

    parent::report($e);
}

resources\views\emails\error.blade.php

资源\视图\电子邮件\error.blade.php

<p><strong>Message: </strong>{{ $error->getMessage() }}</p>
<p><strong>File: </strong>{{ $error->getFile() }}</p>
<p><strong>Trace: </strong>{{$error->getTraceAsString()}}</p>

However, I just recieved and error without the Message from the getMessage()section of the email (as shown below)...

但是,我刚刚收到并没有收到来自getMessage()电子邮件部分的消息(如下所示)的错误......

Message: 
File: C:\api\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php
Trace: 
    #0 C:\api\vendor\laravel\framework\src\Illuminate\Routing\Router.php(823): Illuminate\Routing\RouteCollection->match(Object(Illuminate\Http\Request)) 
    #1 C:\api\vendor\laravel\framework\src\Illuminate\Routing\Router.php(691): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request)) 
    #2 C:\api\vendor\laravel\framework\src\Illuminate\Routing\Router.php(675): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request)) 
    #3 C:\api\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(246): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request)) 
    #4 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request)) 
    #5 C:\api\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) 
    #6 C:\api\vendor\barryvdh\laravel-cors\src\HandleCors.php(34): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) 
    #7 [internal function]: Barryvdh\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure)) 
    #8 C:\api\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array) 
    #9 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
    #10 C:\api\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) 
    #11 C:\api\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(44): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) 
    #12 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure)) 
    #13 C:\api\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array) 
    #14 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
    #15 C:\api\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) 
    #16 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) 
    #17 C:\api\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) 
    #18 C:\api\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure)) 
    #19 C:\api\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request)) 
    #20 C:\api\public\index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request)) 
    #21 {main}

It seems like there might have possibly been something wrong with the Barryvdh\Cors\HandleCors-which I use to enable a cross-domain API or possibly a web crawler trying to poke around a different route than what I allow.

似乎Barryvdh\Cors\HandleCors-我用来启用跨域 API 的方法可能有问题,或者可能是网络爬虫试图绕过与我允许的路径不同的路径。

For my original question though:is it possible to have an error without a Message by which I could get it with getMessage()

不过,对于我最初的问题:是否有可能在没有消息的情况下出现错误,我可以通过该消息获得它getMessage()

And bonus question:is it possible to diagnose the Route error I'm getting with the limited information I have available?

还有一个额外的问题:是否可以使用我可用的有限信息来诊断我得到的 Route 错误?

回答by patricus

The answer to your original question is yes, you can have an Exception that does not have a message.

您原来的问题的答案是肯定的,您可以有一个没有消息的异常。

The answer to your bonus question is yes, you can get an educated guess based on the current information. When you do this, this actually shows you the answer to your original question.

您的奖金问题的答案是肯定的,您可以根据当前信息进行有根据的猜测。当您这样做时,这实际上会向您显示原始问题的答案。

From the stacktrace, it shows that the last method called (from line #0) was Illuminate\Routing\RouteCollection->match. If you go into the codeand look at this method, you can see that, if no route is matched, it calls throw new NotFoundHttpException;.

从堆栈跟踪中,它显示调用的最后一个方法(从第 0 行开始)是Illuminate\Routing\RouteCollection->match. 如果你进入代码查看这个方法,你会发现,如果没有匹配到路由,它会调用throw new NotFoundHttpException;.

Given that this is the only Exception explicitly thrown by this method, as well as the fact that it is thrown without setting a message, it is a pretty good guess that this is your error.

鉴于这是此方法显式抛出的唯一异常,以及它在没有设置消息的情况下抛出的事实,可以很好地猜测这是您的错误。

So, basically, someone attempted to go to a route that didn't exist.

所以,基本上,有人试图去一条不存在的路线。

回答by Ani Emmanuel Arinzechukwu

I have seen this kind of error before but not for mailing. But I can tell you that the error is from your Route its either you are trying to access a Route method that does not exist or you are using the wrong Route method

我以前见过这种错误,但不是邮寄。但我可以告诉你,错误来自你的 Route,要么你试图访问一个不存在的 Route 方法,要么你使用了错误的 Route 方法