管理错误和成功消息和代码的好方法是什么?

发布于 2024-07-26 12:11:19 字数 187 浏览 7 评论 0原文

目前我没有一个很好的系统来管理错误或成功消息,它们几乎是与代码内联编写的。 有人可以建议最佳实践或您正在做的事情,以便能够正确管理整个应用程序中的大量错误和成功消息。

想想国际化......一些应用程序将有语言文件,然后应用程序只是从特定文件中提取字符串......我正在考虑为错误和成功消息实现类似的概念,并且很好奇其他人做了什么线?

Currently I don't have a very good system for managing error or success messages, they are pretty much written inline with the code. Can someone suggest a best practice or something you are doing to be able to properly manage a large list of error and success messages through out an application.

Think internationalization ... some applications will have language files, then the app just pulls the strings from the specific file ... I'm thinking of implementing a similar concept for error and success messages and am curious what others have done along the same lines?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抠脚大汉 2024-08-02 12:11:19

这是我使用的一个函数:

function checkErrors($type, $msg, $file, $line, $context) {
   echo "<h1>Error!</h1>";
   echo "An error occurred while executing this script. Please contact the <a href=mailto:[email protected]>webmaster</a> to report this error.";
   echo "<p />";
   echo "Here is the information provided by the script:";
   echo "<hr><pre>";
   echo "Error code: $type<br />";
   echo "Error message: $msg<br />";
   echo "Script name and line number of error: $file:$line<br />";
   $variable_state = array_pop($context);
   echo "Variable state when error occurred: ";
   print_r($variable_state);
   echo "</pre><hr>";
} 
set_error_handler('checkErrors'); 

它将向您提供 PHP 代码产生的所有错误和警告,并在有人访问包含错误的页面时为您创建一个错误页面。

希望这可以帮助!

Here is a function that I use:

function checkErrors($type, $msg, $file, $line, $context) {
   echo "<h1>Error!</h1>";
   echo "An error occurred while executing this script. Please contact the <a href=mailto:[email protected]>webmaster</a> to report this error.";
   echo "<p />";
   echo "Here is the information provided by the script:";
   echo "<hr><pre>";
   echo "Error code: $type<br />";
   echo "Error message: $msg<br />";
   echo "Script name and line number of error: $file:$line<br />";
   $variable_state = array_pop($context);
   echo "Variable state when error occurred: ";
   print_r($variable_state);
   echo "</pre><hr>";
} 
set_error_handler('checkErrors'); 

it will give you all the errors and warnings that your PHP code produces, along with create an error page for you in the case that someone visits a page that contains an error.

Hope this helps!

陈独秀 2024-08-02 12:11:19

由于我使用单访问点样式系统来引导应用程序并包含正确的文件(它基本上是一个控制器系统),因此我可以使用:

<?php
try {
    require 'bootstrap.php';
    // dispatch controller
    require "controllers/$controller.php";
} catch (Exception $e) {
    // echo info
} catch (LibraryException $le) {
    // library specific exception
}

然后,当我想抛出错误时,我只需:

throw new Exception('An error occurred.');

有人可以建议最佳实践或您正在做的事情,以便能够通过应用程序正确管理大量错误和成功消息。

这是我批量管理错误的方法,但不是管理列表的方法。 我可以 grep 查找我发现的错误消息,但它并不是很有组织。

Since I use a single-access-point style system that bootstraps the application and includes the correct file (it's basically a controller system) I can use:

<?php
try {
    require 'bootstrap.php';
    // dispatch controller
    require "controllers/$controller.php";
} catch (Exception $e) {
    // echo info
} catch (LibraryException $le) {
    // library specific exception
}

Then when I want to throw an error I just:

throw new Exception('An error occurred.');

Can someone suggest a best practice or something you are doing to be able to properly manage a large list of error and success messages through out an application.

This is how I manage errors by bulk, but not how I could manage a list. I could grep for error messages I find, it's not really very organised though.

痴者 2024-08-02 12:11:19

您可以使用 PHP Documentor 之类的工具,并在每个类的文档中包含错误和消息。

http://www.phpdoc.org/

You might use something like PHP Documentor and include errors and messages in the docs for each class.

http://www.phpdoc.org/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文