silverstripe-framework/docs/en/02_Developer_Guides/07_Debugging/01_Error_Handling.md

73 lines
2.3 KiB
Markdown
Raw Normal View History

2014-10-13 10:52:19 +02:00
title: Error Handling
summary: Trap, fire and report user exceptions, warnings and errors.
# Error Handling
2014-10-13 10:52:19 +02:00
SilverStripe has its own error trapping and handling support. On development sites, SilverStripe will deal harshly with
any warnings or errors: a full call-stack is shown and execution stops for anything, giving you early warning of a
potential issue to handle.
2014-10-13 10:52:19 +02:00
## Triggering the error handler.
2014-10-13 10:52:19 +02:00
You should use [user_error](http://www.php.net/user_error) to throw errors where appropriate.
2014-10-13 10:52:19 +02:00
:::php
if(true == false) {
user_error("I have an error problem", E_USER_ERROR);
}
2014-10-13 10:52:19 +02:00
if(0 / 0) {
user_error("This time I am warning you", E_USER_WARNING);
}
2014-10-13 10:52:19 +02:00
## Error Levels
2014-10-13 10:52:19 +02:00
* **E_USER_WARNING:** Err on the side of over-reporting warnings. Throwing warnings provides a means of ensuring that
developers know:
* Deprecated functions / usage patterns
* Strange data formats
* Things that will prevent an internal function from continuing. Throw a warning and return null.
* **E_USER_ERROR:** Throwing one of these errors is going to take down the production site. So you should only throw
E_USER_ERROR if it's going to be **dangerous** or **impossible** to continue with the request.
## Filesystem Logs
2014-10-13 10:52:19 +02:00
You can indicate a log file relative to the site root.
2014-10-13 10:52:19 +02:00
**mysite/_config.php**
:::php
2014-10-13 10:52:19 +02:00
if(!Director::isDev()) {
// log errors and warnings
SS_Log::add_writer(new SS_LogFileWriter('/my/logfile/path'), SS_Log::WARN, '<=');
2014-10-13 10:52:19 +02:00
// or just errors
SS_Log::add_writer(new SS_LogFileWriter('/my/logfile/path'), SS_Log::ERR);
}
2014-10-13 10:52:19 +02:00
<div class="info" markdown="1">
In addition to SilverStripe-integrated logging, it is advisable to fall back to PHPs native logging functionality. A
script might terminate before it reaches the SilverStripe error handling, for example in the case of a fatal error. Make
sure `log_errors` and `error_log` in your PHP ini file are configured.
</div>
## Email Logs
You can send both fatal errors and warnings in your code to a specified email-address.
2014-10-13 10:52:19 +02:00
**mysite/_config.php**
:::php
2014-10-13 10:52:19 +02:00
if(!Director::isDev()) {
// log errors and warnings
SS_Log::add_writer(new SS_LogEmailWriter('admin@domain.com'), SS_Log::WARN, '<=');
// or just errors
SS_Log::add_writer(new SS_LogEmailWriter('admin@domain.com'), SS_Log::ERR);
}
## API Documentation
* [api:SS_Log]