2014-05-01 36 views
2

Laravel 4.1當拋出和調試中的異常設置爲false時,會顯示一個普通異常。如何在Laravel 4中設置「普通」異常處理程序

消息是沿着線:

哎呦,出了問題

我已經通過代碼拖網,它似乎是普通的異常處理程序被登記:

Illuminate\Exception\ExceptionServiceProvider.php 
經由以下行

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

我在哪裏設置自己的自定義普通處理程序?我不喜歡「哎呦」的消息,我希望能夠表現出一個位置特定信息

乾杯

回答

4

這是明擺着的異常處理程序;,在app/start/global.php默認情況下已經上市,修改爲如下(順便說一句,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); 
}); 

創建一個視圖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 

另外,還要'debug' => falseapp/config/app.php文件:

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

,可以有以下方法$exception對象使用:

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) 

如果您離開'debug' => true,,那麼您的異常處理程序仍然可以正常工作,但在某些情況下,它可能會在異常未在您處理程序中捕獲時顯示whoops,而在通用Exception處理程序之前的另一個特定處理程序中顯示。

還要記住,Exception類是最通用的異常類型,如果在此之後定義了其他更具體的異常處理程序,那麼如果從該特定處理程序返回任何響應,則不會觸發它。

+1

男人簡直太容易了,不要拿掉答案。謝謝@ WereWolf - 阿爾法 – Chris

1

在您的應用程序/啓動/ global.php文件添加

App::missing(function($exception) 
{ 
    return 'Your custom message'; // you can also specify a view to render. 
}); 

請參觀http://laravel.com/docs/errors瞭解更多信息

+0

這隻能處理404我相信。我正在尋找所有其他例外。通常抓到App :: error(函數(Exception $ exception,$ code) { \t Log :: error($ exception); }); – Chris