douchi0028 2010-08-15 04:26
浏览 244
已采纳

我应该如何处理Codeigniter应用程序中的错误

Ok so again my question is kind of a best practices/techniques question so I'm assuming there will be a few different ways to handle this.

So, to what degree should I factor in error handling? As an example, say I havee a basic function in my model which retrieves a customers record ie

function get_customer($customer_id) {

    $this->where('id',$customer_id);
    $query = $this->db->get('customers');

    return $query->result();

}

Should I put in something to check whether the parameter $customer_id, exists and is the correct type? or does it depend on the logic of the application? For instance, in some cases this function would never be called unless $customer_id is set and that might be checked in the controller, but is it best practice to include the error checking here as well?

Also, should I use something like try/catch and throw an exception or can it just be as simple as if ($customer_id!='') etc ?

  • 写回答

1条回答 默认 最新

  • douzhendi4559 2010-08-15 05:20
    关注

    You are using the above function from a controller right? The best place to make sure that the variable $customer_id is of correct type and contains what you expect, you need to check it from within the controller before sending it to the model. The controller puts you in a position to show the error by redirecting or whatever.

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

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)