如何在 Laravel 中启用详细的错误消息?
How enable detailed error messages in Laravel?
我用的是Laravel 5.5,发现这个版本的错误好像有点'user-friendly'...如何重新获取详细的错误信息?
例如:之前,使用此代码:
abort(500, 'The server is on fire');
我看到了这条消息 "The server is on fire"。现在,我只看到 "Whoops, looks like something went wrong."
提前致谢!
您收到此错误的原因是您使用 500 代码中止。 Laravel 最终会做的是检查是否存在与该错误代码相关的错误视图(存在),然后显示该视图。
开箱即用 Laravel 附带 404、419、429、500、503 错误视图。
我建议使用更适合您情况的代码(如果可能)。这是一些错误代码的列表 https://httpstatuses.com/.
如果失败,您可以通过将以下内容添加到 app/Exceptions/Handler.php
class 来覆盖默认行为:
/**
* Render the given HttpException.
*
* @param \Symfony\Component\HttpKernel\Exception\HttpException $e
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function renderHttpException(HttpException $e)
{
$status = $e->getStatusCode();
if ($status == 500 && config('app.debug')) {
return $this->convertExceptionToResponse($e);
}
return parent::renderHttpException($e);
本质上,这将绕过寻找 500.blade.php
错误视图,然后应该显示堆栈。请注意,这没有针对实际的 500 错误进行测试,所以我不确定它是否适用于此(我想不出为什么它不会,但我只是想提一下)。
我用的是Laravel 5.5,发现这个版本的错误好像有点'user-friendly'...如何重新获取详细的错误信息?
例如:之前,使用此代码:
abort(500, 'The server is on fire');
我看到了这条消息 "The server is on fire"。现在,我只看到 "Whoops, looks like something went wrong."
提前致谢!
您收到此错误的原因是您使用 500 代码中止。 Laravel 最终会做的是检查是否存在与该错误代码相关的错误视图(存在),然后显示该视图。
开箱即用 Laravel 附带 404、419、429、500、503 错误视图。
我建议使用更适合您情况的代码(如果可能)。这是一些错误代码的列表 https://httpstatuses.com/.
如果失败,您可以通过将以下内容添加到 app/Exceptions/Handler.php
class 来覆盖默认行为:
/**
* Render the given HttpException.
*
* @param \Symfony\Component\HttpKernel\Exception\HttpException $e
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function renderHttpException(HttpException $e)
{
$status = $e->getStatusCode();
if ($status == 500 && config('app.debug')) {
return $this->convertExceptionToResponse($e);
}
return parent::renderHttpException($e);
本质上,这将绕过寻找 500.blade.php
错误视图,然后应该显示堆栈。请注意,这没有针对实际的 500 错误进行测试,所以我不确定它是否适用于此(我想不出为什么它不会,但我只是想提一下)。