简体   繁体   English

Laravel处理用户生成的错误

[英]Laravel handling of user generated errors

I'm quite new to Laravel, and in past applications I would generate a user warning or notice, use a custom error handler, which would send an email. 我对Laravel还是很陌生,在过去的应用程序中,我会生成用户警告或通知,并使用自定义错误处理程序,该程序将发送电子邮件。

Often the errors are cases where a user enters invalid data that can't be validated, or when a third party API call returns no results. 错误通常发生在用户输入无法验证的无效数据或第三方API调用未返回结果的情况下。

My trouble is that Laravel halts execution and displays a whoops page for any error, so generating an error probably isn't the way I should handle this, but what is the best (correct?) way? 我的麻烦是Laravel会暂停执行并显示任何错误的页面,因此生成错误可能不是我应该处理的方式,但是最好的方法(正确吗?)是什么?

For development environment it's recommended to use debug => true in app/config/app.php file so you'll get informative error messages. 对于开发环境,建议在app/config/app.php文件中使用debug => true以获取有用的错误消息。 To disable it for production (recommended) just change this value to false , for example: 要将其禁用以进行生产(推荐),只需将此值更改为false ,例如:

/*
|--------------------------------------------------------------------------
| Application Debug Mode
|--------------------------------------------------------------------------
|
| When your application is in debug mode, detailed error messages with
| stack traces will be shown on every error that occurs within your
| application. If disabled, a simple generic error page is shown.
|
*/

'debug' => false

To register a custom error handler you may declare that handler in your app/start/global.php file, for example: 要注册自定义错误处理程序,您可以在app/start/global.php文件中声明该处理程序,例如:

// For 404 not found error
App::missing(function($e){
    return Redirect::to('/');
});

This will catch all 404 not found errors but notice another error handler available in that file by default and it's: 这将捕获所有404 not found错误,但默认情况下会注意到该文件中另一个可用的错误处理程序,它是:

App::error(function(Exception $exception, $code)
{
Log::error($exception->getMessage());
});

This is the most generic error handler in Laravel means that any un-handaled error will be handled by this one and that's why declare your specific error handlers bottom of this error handler because, error handlers bubbles up from the bottom to top and this one is the parent so at last (if any custom handler was not registered or didn't catch the error) this handler will take the responsibility to handle an error if you didn't handle the error using any specific handler and didn't return any response when handling an error. 这是Laravel最通用的错误处理程序, Laravel意味着任何un-handaled错误都将由该错误处理程序处理,这就是为什么在此错误处理程序的底部声明特定的错误处理程序,因为错误处理程序从底部向上冒泡,而这个错误处理程序parent ,最后(如果未注册任何自定义处理程序或未捕获错误),如果您未使用任何特定的处理程序处理错误并且未返回任何响应,则此处理程序将负责处理错误处理错误时。

For example; 例如; to register ModelNotFoundException exception you may try this: 要注册ModelNotFoundException异常,您可以尝试以下操作:

App::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $e)
{
    return Response::make('Not Found', 404);
});

Now, since you returned a response from the handler then the parent error handler will not get fired and response will be sent to the browser. 现在,由于您从处理程序中返回了响应,因此父错误处理程序将不会被触发,并且响应将被发送到浏览器。 If you didn't return a response then the topmost parent handler will be fired like event bubling in JavaScript . 如果未返回响应,则将触发最顶层的父处理程序,就像JavaScript event冒泡一样。


Read more about Errors & Logging on Laravel website. Laravel网站上了解有关错误和登录Laravel信息。

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

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