0
A
回答
1
配置項對異常沒有很好的支持。您需要做的是設置適當的異常處理。
現在你所有的數據庫錯誤都會自動拋出異常。作爲獎勵,您在整個CI應用程序中都有良好的異常處理。
註冊該轉換PHP錯誤到異常的自定義的ErrorHandler,比如把這個在你的config/config.php文件
function my_error_handler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno))
{
// This error code is not included in error_reporting
return;
}
log_message('error', "$errstr @$errfile::$errline($errno)");
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("my_error_handler");
註冊未捕獲的異常處理程序的頂部,把這樣的事情在你的config/config.php
function my_exception_handler($exception)
{
echo '<pre>';
print_r($exception);
echo '</pre>';
header("HTTP/1.0 500 Internal Server Error");
}
set_exception_handler("my_exception_handler");
設置終止處理程序:
function my_fatal_handler()
{
$errfile = "unknown file";
$errstr = "Fatal error";
$errno = E_CORE_ERROR;
$errline = 0;
$error = error_get_last();
if ($error !== NULL)
{
echo '<pre>';
print_r($error);
echo '</pre>';
header("HTTP/1.0 500 Internal Server Error");
}
}
register_shutdown_function("my_fatal_handler");
集,其將自定義斷言處理程序斷言爲例外,把這樣的事情在你的config/config.php
:
function my_assert_handler($file, $line, $code)
{
log_message('debug', "assertion failed @$file::$line($code)");
throw new Exception("assertion failed @$file::$line($code)");
}
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_BAIL, 0);
assert_options(ASSERT_QUIET_EVAL, 0);
assert_options(ASSERT_CALLBACK, 'my_assert_handler');
使用包裝像這樣在你的控制器
public function controller_method()
{
try
{
// normal flow
}
catch(Exception $e)
{
log_message('error', $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine());
// on error
}
}
您可以調整和定製整個事情你的喜好!
希望這會有所幫助。
您還需要攔截CI show_error方法。將此放在application/core/MY_exceptions.php
:
class MY_Exceptions extends CI_Exceptions
{
function show_error($heading, $message, $template = 'error_general', $status_code = 500)
{
log_message('debug', print_r($message, TRUE));
throw new Exception(is_array($message) ? $message[1] : $message, $status_code);
}
}
而且在application/config/database.php
此設置離開FALSE
有轉化爲異常的數據庫錯誤。
$db['default']['db_debug'] = TRUE;
相關問題
- 1. 異常查詢
- 2. 異常使用CopyToDataTable與「新{..}」LINQ查詢
- 3. QueryDSL查詢異常
- 4. Informix查詢異常
- 5. SPARQL查詢異常
- 6. Hibernate查詢異常
- 7. 使用子查詢的PostgreSQL查詢出現異常
- 8. 使用JPQL創建查詢 - 查詢語法異常
- 9. Mysql查詢中的異常
- 10. LINQ查詢拋出異常
- 11. XPath查詢給出異常
- 12. Parse.com查詢getFirst()異常
- 13. 休眠時查詢異常
- 14. RavenDB - DateTime.Subtract查詢異常
- 15. 點燃查詢異常
- 16. freebase mqlread查詢異常
- 17. 異常SQLite中查詢
- 18. SQLite異常查詢錯誤
- 19. nhibernate查詢compositeid異常
- 20. C++異常處理查詢
- 21. 查詢工作異常
- 22. Mongodb查詢異常緩慢
- 23. 異常與LINQ2SQL查詢
- 24. Java異常處理查詢
- 25. Java的Hibernate查詢,異常
- 26. Solr查詢解析異常
- 27. MS Access查詢「WHERE」異常
- 28. Android Fql查詢異常
- 29. ClassCastException異常,當我查詢
- 30. C#SQL查詢異常