5

私は Kohana 3.3 を使用しており、このフォーラムの投稿を読んでいます。

スタック トレースがエンド ユーザーに表示されないようにするには、Kohana_Exception::_handler() をオーバーライドして、浸透するエラーに対して別のことを行う必要があると書かれています。それは、次の関数をオーバーライドKohana_Exceptionして追加することを意味しますか?

public static function _handler(Exception $e)
{
    try
    {
        // Log the exception
        Kohana_Exception::log($e);

        // Generate the response
        //instead of below line:
        //$response = Kohana_Exception::response($e);
        $response = //what do I do here, return a 404 page or custom 500 page?
        return $response;
    }

    //rest of function
}

もしそうなら、私はそこに何を返しますか?

編集:

ブートストラップ.php

/**
 * Attach the file write to logging. Multiple writers are supported.
 */
Kohana::$log->attach(new Log_File(APPPATH.'logs'));

/**
 * Attach a file reader to config. Multiple readers are supported.
 */
Kohana::$config->attach(new Config_File);

/**
 * Attach customer error handler if we are in production
 */
if(Kohana::$environment == Kohana::PRODUCTION || Kohana::$environment == Kohana::STAGING)
{
    set_exception_handler(array('My_Exception', 'handler'));
    throw new Exception('text'); //this works, if removed however my  exception handler does not do anything
}

My_Exception.php (classes/My/Exception.php 内)

<?php   
    class My_Exception extends Kohana_Exception 
    {
        public static function handler(Exception $e)
        {
            try 
            {
                // Log the exception
                Kohana_Exception::log($e);

                // Let's try and load an error View             
                $class = get_class($e);

                if($class == 'HTTP_Exception_404')
                {
                    $view = View::factory('errors/404');
                    $view->set('error_code', 404);
                }
                else
                {
                    $view = View::factory('errors/general');
                    $view->set('error_code', $e->getCode()); // alternatively use $e->getCode()
                    $view->set('error_message', $e->getMessage()); // alternatively use $e->getMessage();
                }

                // Let's render the output and send it to the browser
                $response = $view->render();
                echo $response;
            }
            catch (Exception $e)
            {
                /**
                 * Things are going *really* badly for us, We now have no choice
                 * but to bail. Hard.
                 */
                // Clean the output buffer if one exists
                ob_get_level() AND ob_clean();

                // Set the Status code to 500, and Content-Type to text/plain.
                header('Content-Type: text/plain; charset='.Kohana::$charset, TRUE, 500);

                // This will output the Exceptiones error message
                // You may want to display something else
                echo $e->getMessage();

                exit(1);
            }
        }
    }
4

2 に答える 2

9

私は実際にこの問題をさらに調査し、この分野でのコハナの行動をより完全に理解したので、回答を最初から書き直しました。

目標を達成するには、次の 2 つのことを行う必要があります。

  1. デフォルトのエラー表示を変更します ( 内APPPATH/bootstrap.php):

    /**
     * Change default error View
     */
    if(Kohana::$environment == Kohana::PRODUCTION || Kohana::$environment == Kohana::STAGING)
    {
        Kohana_Exception::$error_view = 'errors/general';
    }
    

    テンプレート ファイルでは、Kohana のネイティブの変数名と同じ (それらの変数のみ) を使用する必要があることに注意してください。つまり、次のようになります。

    • $クラス
    • $コード
    • $メッセージ
    • $ファイル
    • $line
    • $トレース

2.コメントでリンクしたチュートリアル  に従って、カスタム HTTP エラー ページを作成します。

これらの手順に従うことで、次のことが保証されます。

  • Kohana のすべてのエラー ページに対する独自のビューがあります。
  • さまざまな HTTP エラーのカスタム ビューを作成できます。
  • Kohana のデフォルトの Exception Handler をオーバーライドする必要はありません (この演習で証明されたように、これは実装が簡単ではありません)。

私は上記のアプローチをテストしましたが、それは私にとって魅力的でした。

于 2013-02-20T08:43:50.843 に答える
0

ブートストラップ ファイルで Kohana_Exception::$error_view を設定し、対応するビューを作成しました。他に何も必要ありませんでした。すべてのエラーは自動的に魔法のようにリダイレクトされました...

于 2013-12-01T21:48:47.077 に答える