简体   繁体   English

尝试捕获php pdo,以正确的方式进行

[英]try catch in php pdo, doing it right way

I am using php pdo with postgres. 我在postgres中使用php pdo。 I am using core php, no mvc, no classes and objects. 我正在使用核心php,没有mvc,没有类和对象。 I have some doubts in my mind since i am new to development, :- 因为我是开发新手,所以我心中有一些疑问:-

  • Should i use try catch on every query execution. 我应该在每个查询执行中使用try catch吗。
  • And if i use try catch then should i exit the running script on exception 如果我使用try catch,那么我是否应该异常退出运行脚本

  • And is there a way to use one try catch for all queries, i mean making a common function for executing queries. 有没有一种方法可以对所有查询使用try try catch,我的意思是为执行查询提供一个通用函数。

  • And how to handle exceptions on ajax. 以及如何处理ajax上的异常。

please some one clear my doubts about this. 请有人清除我对此的怀疑。

There are too many questions and they are too generic. 问题太多了,它们太笼统了。 But in most cases: 但在大多数情况下:

  1. No way 没门
  2. it's up to you, your app's logic and particular case. 这取决于您,您应用的逻辑和特殊情况。 Say if there is water leak somewhere in the city is it expected to stop water supply system completely? 假设城市中某处漏水,是否将完全停止供水系统? Typically there is concrete module/part that does not make sense to process further once DB query fails. 通常,一旦数据库查询失败,则没有具体的模块/部分无法进行进一步处理。 And only that module/part should exit/stop. 并且只有该模块/部件应该退出/停止。
  3. it's better not doing your own wrapping around PDO. 最好不要自行包装PDO。 in most cases you will just make code harder to maintain without any real benefit. 在大多数情况下,您只会使代码难以维护而没有任何实际好处。
  4. typically the best solution is to return some HTTP error(404 Not Found or 500 Internal Server Error) letting client code to know "sorry, you cannot get this information yet"; 通常,最好的解决方案是返回一些HTTP错误(“ 404 Not Found”或“ 500 Internal Server Error”),让客户端代码知道“对不起,您还无法获取此信息”; how client code will handle such an error - it depends on your client code's logic/flow 客户端代码将如何处理此类错误-取决于客户端代码的逻辑/流程
  1. Only if you have a certain scenario to handle a particular error. 仅当您具有处理特定错误的特定方案时。 For example, a duplicated unique key, for which you have a fall-back scenario. 例如,您有一个后备方案的重复唯一键。 (note that you should always check for this particular error, and re-throw the exception otherwise). (请注意,您应始终检查此特定错误,否则请重新引发异常)。
    But for a regular query, only to handle an arbitrary error - just like @skyboyer said - no way. 但是对于常规查询,只能处理任意错误-就像@skyboyer所说的那样-没办法。 The exception should bubble up to a site-wide handler. 异常应该冒泡到站点范围的处理程序。
  2. Again, it depends. 再次,这取决于。 If is a special handling scenario, do whatever you have in mind for the scenario. 如果是特殊的处理方案,请针对该方案进行任何考虑。 But for the arbitrary error, it is agreed upon to halt the code execution, as your code could yield unpredictable results. 但是对于任意错误,由于您的代码可能会产生不可预测的结果,因此同意停止执行代码。
  3. That's two questions in one. 这是两个问题合二为一。
    • it's perfectly OK to have a global try catch which will act as a site-wide exception handler, wrapping the whole code. 最好有一个全局 try catch,它将用作站点范围的异常处理程序,包装整个代码。
    • as of the function to run all your queries - it's a brilliant idea by itself, but it has nothing to do with error handling. 从运行所有查询的功能开始,这本身就是一个绝妙的主意,但与错误处理无关。 Simply because database errors are no different from any other error on your site and thus should be treated exactly the same way. 仅仅是因为数据库错误与您站点上的任何其他错误都没有不同,因此应该以完全相同的方式进行处理。 Therefore it makes no sense to write a dedicated try-catch for the query function. 因此,为查询功能编写专用的try-catch没有任何意义。 Just let it throw an Exception and then handle that exception as any other error on your site. 只要让它抛出一个异常,然后将该异常作为您网站上的其他任何错误进行处理即可。
  4. Pretty much the same way as non-AJAX errors. 与非AJAX错误几乎相同。 Again - just like @skyboyer said - make your handler to return 500 HTTP status and then your AJAX calling code will know there was an error. 再次-就像@skyboyer所说的-使您的处理程序返回500 HTTP状态,然后您的AJAX调用代码将知道存在错误。 That's enough 够了

I wrote an article where tried to explain all there matters, PHP Error reporting which you may find interesting. 我写了一篇文章,试图解释所有问题, PHP错误报告您可能会发现有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM