douna1895 2014-06-25 20:19
浏览 62
已采纳

如何访问Pear Cache_Lite错误对象?

I'm playing around with Pear's Cache_Lite and it looks really easy to use. One aspect which I can't quite get a handle on is how I can throw Exceptions to find out what the error was. As per the docs, in my $options array below, I've tried 'pearErrorMode' => CACHE_LITE_ERROR_DIE, which will stop the script and show me the error (namely, that the cacheDir doesn't exist, as in the code below).

However, I don't necessarily want to show this error to the user (i.e. I'll probably log it, and give them a custom message). I then thought that I could use 'pearErrorMode' => CACHE_LITE_ERROR_RETURN which is supposed to return the Pear error object. My impression (obviously incorrect) was that echo $e->getMessage(); would then access the Pear error object and print it out. However, all I got was a blank screen. My question then, is how I can do a standard try/catch, and then access the error object? My code snippet (more or less a copy from the Pear Manual) is presented below:

<?php
    require_once('Cache/Lite.php');
    $id = '123';
    $options = array(
        'cacheDir' => '/oops_I_am_not_a_directory/', //this is the problem line!
        'lifeTime' => 3600
    );
    try {
       $Cache_Lite = new Cache_Lite($options);
       if ($data = $Cache_Lite->get($id)) {
       echo $data;
    } else { 
       $data = "blah";
       $Cache_Lite->save($data);
    }   
    } catch (Exception $e) {
       echo $e->getMessage();   
    }
    ?>
  • 写回答

1条回答 默认 最新

  • dongyi1996 2014-06-26 12:09
    关注

    _RETURN returns the error object as return value from the functions:

    $error = $cache->get(..);
    $error = $cache->save(..);
    

    You can check if it's an error by using

    $retval = $cache->get(..);
    if (PEAR::isError($retval)) {
        echo $retval->getMessage() . "
    ";
    }
    

    Alternatively, you can make it throw exceptions by setting CACHE_LITE_ERROR_EXCEPTION or PEAR_ERROR_EXCEPTION

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么