duangan4070 2014-05-01 23:02
浏览 47
已采纳

如何在Laravel 4中设置“普通”异常处理程序

In Laravel 4.1 a plain exception is shown when an exception in thrown and debug is set to false.

The message is along the lines of:

Whoops, something has gone wrong

I've trawled through the code, and it seems that the plain exception handler is registered in:

Illuminate\Exception\ExceptionServiceProvider.php

via the following line

protected function registerHandler()
{
    $this->app['exception'] = $this->app->share(function($app)
    {
        return new Handler($app, $app['exception.plain'], $app['exception.debug']);
    });
}

Where do I set my own custom plain handler? I don't like the "Whoops" message, I want to be able to show a site specific message

Cheers

  • 写回答

2条回答 默认 最新

  • doulongdan2264 2014-05-02 09:20
    关注

    This is the plain exception handler;, already available in app/start/global.php by default, modify it as given below (Btw, Whoops! PHP Errors only for Cool Kids):

    App::error(function(Exception $exception)
    {
        Log::error($exception);Log::error($exception->getMessage());
        return View::make('errors.index')->with('exception', $exception);
    });
    

    Create a view view/errors/index.blade.php

    @extends('layouts.master')
    
    @section('content')
    
        <div class="page-header">
            <h1>Oops!</h1>
        </div>
    
        <div class='well'>ERROR: {{ $exception->getMessage() }}</div>
    
    @stop
    

    Also make 'debug' => false in your app/config/app.php file:

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

    You have following methods to use in $exception object:

    array (size=10)
      //0 => string '__construct' (length=11)
      1 => string 'getSeverity' (length=11)
      2 => string 'getMessage' (length=10)
      3 => string 'getCode' (length=7)
      4 => string 'getFile' (length=7)
      5 => string 'getLine' (length=7)
      6 => string 'getTrace' (length=8)
      7 => string 'getPrevious' (length=11)
      8 => string 'getTraceAsString' (length=16)
      //9 => string '__toString' (length=10)
    

    If you leave 'debug' => true, then still your exception handler will work but in some cases it may display whoops when the exception is not caught in you handler but in another specific handler before your generic Exception handler.

    Also remember that, Exception class is the most generic exception type, if you have other more specific exception handlers defined after that then it'll not get triggered if any response returned from that specific handler.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了