ThinkPHP Source Code Analysis series-error handling ThinkPHP has a global function, halt, which is used to format and display errors. it can be found in the system function library. This function ensures the consistency of error handling in applications. First paste the code: functionhalt ($ erro ThinkPHP Source Code Analysis series-error handling
ThinkPHP has a global function, halt, which is used to format output and display errors. it can be found in the system function library. This function ensures the consistency of error handling in applications.
Paste the code first:
Function halt ($ error) {if (IS_CLI) exit ($ error); $ e = array (); if (C ('app _ debug ')) {// output error message if (! Is_array ($ error) {$ trace = debug_backtrace (); // returns a multidimensional array $ e ['message'] = $ error; $ e ['file'] = $ trace [0] ['file']; $ e ['class'] = $ trace [0] ['class']; $ e ['function'] = $ trace [0] ['function']; $ e ['line'] = $ trace [0] ['line']; $ traceInfo = ''; $ time = date (" y-m-d H: I: m ", $ _ SERVER [" REQUEST_TIME "]); foreach ($ trace as $ t) {$ traceInfo. = '['. $ time. ']'. $ t ['file']. '('. $ t ['line']. ')'; $ traceInfo. = $ t ['class']. $ t ['type']. $ t ['function']. '('; $ traceInfo. = implode (',', $ t ['args']); $ traceInfo. = ")
";}$ E ['track'] = $ traceInfo;} else {$ e = $ error ;} // contains the exception PAGE template include C ('tmpl _ EXCEPTION_FILE ');} else {// otherwise, it is redirected to the error page $ error_page = C ('error _ page '); if (! Empty ($ error_page) {redirect ($ error_page); // custom error page} else {if (C ('show _ ERROR_MSG ')) $ e ['message'] = is_array ($ error )? $ Error ['message']: $ error; else $ e ['message'] = C ('error _ message '); // include the exception page template include C ('tmpl _ EXCEPTION_FILE ') ;}} exit; // exit the program when an error occurs}
?
The halt function first checks PHP's SAPI (server-side application programming interface). If CLI is used, it exits the program directly. Currently, the popular method is that PHP is loaded into Apache as a module. in this case, PHP's SAPI is apache2handler. ThinkPHP provides a debugging mechanism to facilitate application development. When you are in the development stage, you can enable the debugging mechanism for troubleshooting. When the environment is deployed, the debugging mechanism is disabled to improve the efficiency and security of applications. In both cases, error handling is different.
When debugging is enabled, if the transmitted error data is not stored in an array, the data processed by debug_backtrace, the PHP built-in backtracking function, is stored in an array, including error information, file name, class, function, and other data. If the transmitted error data is stored as an array, you can create an index for the array. Finally, the error message is displayed through formatting the configured exception page template. the default exception page template is placed in the framework Template directory, and the key content is posted here:
If a system error occurs, you can select ["> retry] [return] or ["> Return to homepage].
Error location:FILE:
LINE:
[Error message]
[TRACE]
?
When the debugging mechanism is disabled, the custom error page is displayed in the production environment. If the system cannot find the custom error display template, the above error page template will be rendered.
?
(End)