Kohana3中的自定义异常处理 [英] Custom Exception Handling in Kohana3

查看:167
本文介绍了Kohana3中的自定义异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我的应用程序/模块生成的异常创建自定义异常.

I have a requirement to create custom exception for the exceptions generated by my app/module.

  • 我想在一个地方合并所有异常类,并在一个地方处理异常.
  • 我可能有一个通用的例外,如下所述,我想在一个常见的地方
    • 输入无效
    • 内部错误(数据库错误,smtp错误,其他故障)
    • 权限被拒绝
    • 会话错误
    • I want to consolidate all the exception classes in one place and handle the exceptions in one place.
    • I might have generic exceptions, like mentioned below, which I would like in one common place
      • input invalid
      • internal error (database errors, smtp errors, other failures)
      • permission denied
      • session error

      我可能有特定的例外情况,如下所述

      I might have specific exceptions, like mentioned below

      • 电子邮件无效等

      在某些情况下,特定异常可能是通用异常的子类,例如电子邮件无效"可能属于输入无效"异常.

      Specific exceptions might be a subclass of generic exceptions in cases, like "email not valid" could fall under "input invalid" exception.

      我应该能够在抛出时与异常消息一起发送数据. (如果可行,数据将以数组或对象的形式出现)

      I Should be able to send data along with the exception message while throwing. (Data will be in arrays or objects if feasible)

      最好的解决方法是什么?
      组织自定义异常的最佳方法是什么?
      如何以一种方式编码,使我们不必在每个地方都捕获常见异常,但同时用户也会收到有意义的错误.
      调用方法后,我们应仅捕获该方法可能引发的特定异常.

      Whats the best way to go about it?
      What is the best way to organize custom exceptions?
      How to code in such a way that we don't have to catch common exceptions every where but at the same time user gets a meaningful error.
      After calling a method we should only catch specific exceptions that the method can throw.

      推荐答案

      我建议您转到Kohana 3.2,因为Kohana在该新的稳定版本中处理异常的方式有所变化.假设您将使用v3.2,这是管理自定义异常的方法:

      I would suggest you to move to Kohana 3.2 as there is a change in the way Kohana handles exception in that new stable version. Assuming you are going to use v3.2, this is how you could manage custom exceptions:

      首先,您需要修改bootstrap.php并确保Kohana :: init()调用中的错误"为true.这将确保Koahana将处理您或系统抛出的所有未处理的异常.如果您检查\ classes \ kohana \ core.php,Kohana使用下面的php调用注册其异常处理程序类Kohana_Exception

      First of all, you need to modify bootstrap.php and make sure 'errors' is to true in the Kohana::init() call. This will make sure that Koahana will handle all unhandled exceptions thrown by you or the system. if you check \classes\kohana\core.php, Kohana registers its exception handler class Kohana_Exception using php call below

      set_exception_handler(array('Kohana_Exception', 'handler'));
      

      默认异常处理程序可以很好地处理所有类型的异常,并将消息写入日志文件夹并显示基本错误页面.如果您查看Kohana_Exception内部,则它是Kohana_Kohana_Exception类的子类,是编写逻辑的地方.

      The default exception handler does a nice job of handling all types of Exceptions and writing the message to the log folder and displaying a basic error page. If you look inside Kohana_Exception, it is a subclass of Kohana_Kohana_Exception class, which is where the logic is written.

      现在,要自定义内容:

      • 如果您只想显示用于显示错误的自定义页面,则只需创建一个名为application/views/kohana/error.php的视图,然后在其中放置自定义错误页面即可.它将覆盖在system/views/kohana/error.php中找到的系统默认错误视图文件.

      • If you are looking for just showing a custom page for showing your errors, just create a view named application/views/kohana/error.php and put your custom error page there. it will override the system's default error view file found at system/views/kohana/error.php.

      如果要更改记录错误的方式或根据特定类型的错误进行一些自定义处理,则需要重写Kohana_Exception类或通过在以下位置调用set_exception_handler()注册自己的派生异常处理程序. bootstrap.php的结尾.

      If you are looking for changing the way you log the error or do some custom processing based on specific type of errors, you need to override Kohana_Exception class or register your own derived exception handler by calling set_exception_handler() at the end of bootstrap.php.

      • 要覆盖Kohana_Exception,只需将粘贴/system/classes/kohana/exception.php复制到application/classes/kohana/exception.php并覆盖handler()和/或text()方法.例如在下面,我是自定义处理404错误的对象,还包括user_id到错误日志中进行调试.

      :

      class Kohana_Exception extends Kohana_Kohana_Exception 
      {
          /**
           * Overriden to show custom page for 404 errors
           */
          public static function handler(Exception $e)
          {
              switch (get_class($e))
              {
                  case 'HTTP_Exception_404':
                      $response = new Response;
                      $response->status(404);
                      $view = new View('error/report_404');
                      $view->message = $e->getMessage();
                      echo $response->body($view)->send_headers()->body();
                      if (is_object(Kohana::$log))
                      {
                          // Add this exception to the log
                          Kohana::$log->add(Log::ERROR, $e);
                          // Make sure the logs are written
                          Kohana::$log->write();
                      }                
                      return TRUE;
                      break;
      
                  default:
                      return Kohana_Kohana_Exception::handler($e);
                      break;
              }
          }
      
        /**
          * Override if necessary.  E.g. below include logged in user's info in the log
         */
         public static function text(Exception $e)
         {
      
          $id = <get user id from session>;
          return sprintf('[user: %s] %s [ %s ]: %s ~ %s [ %d ]',
                  $id, get_class($e), $e->getCode(), strip_tags($e->getMessage()), Debug::path($e->getFile()), $e->getLine());        
      
         }
      }
      

      有用的外部链接和参考:

      Helpful external links and references:

      http://kohana.sebicas.com/index.php/guide/kohana/errors

      http://kohanaframework.org/3.1/guide/kohana/tutorials/错误页面

      这篇关于Kohana3中的自定义异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆