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

时间:2021-04-16 17:04:38

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?

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

3 个解决方案

#1


1  

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:webmaster@somedomain.com>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.

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

Hope this helps!

希望这可以帮助!

#2


0  

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.

这是我通过批量管理错误的方式,但不是我如何管理列表。我可以找到我发现的错误消息,但它并不是非常有组织。

#3


0  

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

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

http://www.phpdoc.org/

#1


1  

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:webmaster@somedomain.com>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.

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

Hope this helps!

希望这可以帮助!

#2


0  

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.

这是我通过批量管理错误的方式,但不是我如何管理列表。我可以找到我发现的错误消息,但它并不是非常有组织。

#3


0  

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

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

http://www.phpdoc.org/