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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog