【问题标题】:Returning useful error messages with PHP使用 PHP 返回有用的错误消息
【发布时间】:2016-05-31 18:57:22
【问题描述】:

我不明白如何使用 PHP 正确创建有用的错误消息并将其返回到网络。

我有课

class Foo {
    const OK_IT_WORKED = 0;
    const ERR_IT_FAILED = 1;
    const ERR_IT_TIMED_OUT = 3;

    public function fooItUp(){
        if(itFooed)
          return OK_IT_WORKED;
        elseif(itFooedUp)
          return ERR_IT_FAILED;
        elseif(itFooedOut)
          return ERR_IT_TIMED_OUT;
    }
}

另一个类使用这个类做一些有用的事情,然后将结果返回给用户。我只是想知道我将所有错误消息的字符串值放在哪里。

class Bar {
    public function doFooeyThings(stuff){
        $res = $myFoo->fooItUp();
        // now i need to tell the user what happened, but they don't understand error codes
        if($res === Foo::OK_IT_WORKED)
           return 'string result here? seems wrong';
        elseif ($res === Foo::ERR_IT_FAILED)
           return Foo::ERR_IT_FAILED_STRING; // seems redundant?
        elseif($res === Foo:ERR_IT_TIMED_OUT)
           return $res; // return number and have an "enum" in the client (js) ?
    }

}

【问题讨论】:

    标签: php


    【解决方案1】:

    您应该尽可能避免返回错误状态。改用异常。如果您在阅读之前从未使用过异常here

    您可以通过多种方式在示例中利用异常。您可以为每个错误或每个错误类别创建自定义异常。有关自定义异常 here 的更多信息,或者您可以创建默认 Exception 类的实例,将错误消息作为字符串提供给它。

    下面的代码遵循第二种方法:

    class Foo {
        const OK_IT_WORKED = 0;
        const ERR_IT_FAILED = 1;
        const ERR_IT_TIMED_OUT = 3;
    
        public function fooItUp(){
            if(itFooed)
              return OK_IT_WORKED;
            else if(itFooedUp)
               throw new Exception("It failed")
            else if(itFooedOut)
               throw new Exception("Request timed out");
        }
    }
    

    我相信你能想到一些比我使用的更优雅的信息。无论如何,您可以继续使用 try/catch 块处理调用方方法上的这些异常:

    class Bar {
        public function doFooeyThings(stuff){
            try
            {
               $res = myFoo->fooItUp();
            }
            catch(Exception $e)
            {
               //do something with the error message
            }
    
        }
    
    }
    

    fooItUp 抛出的任何异常都将被 catch 块“捕获”并由您的代码处理。

    您还应该考虑两件事:

    • 最好不要向用户显示有关错误的详细信息,因为这些信息可能会被恶意用户使用

    • 理想情况下,您应该有某种全局异常处理

    【讨论】:

      【解决方案2】:

      一种解决方案是将异常与set_exception_handler() 结合使用。

      <?php
      
      set_exception_handler(function($e) {
          echo "Error encountered: {$e->getMessage()}";
      });
      
      class ErrorMessageTest
      {
          public function isOk()
          {
              echo "This works okay. ";
          }
      
          public function isNotOkay()
          {
              echo "This will not work. ";
              throw new RuntimeException("Violets are red, roses are blue!! Wha!?!?");
          }
      }
      
      $test = new ErrorMessageTest();
      
      $test->isOk();
      $test->isNotOkay();
      

      set_exception_handler() 方法接受一个接受异常作为其参数的可调用对象。如果 try/catch 没有捕获到抛出的异常,这让您可以提供自己的逻辑。

      Live Demo

      另见:set_exception_handler() documentation

      【讨论】:

        猜你喜欢
        • 2012-09-19
        • 1970-01-01
        • 1970-01-01
        • 2012-10-21
        • 1970-01-01
        • 2018-07-09
        • 2014-03-20
        • 1970-01-01
        • 2011-04-21
        相关资源
        最近更新 更多