function tripal_report_error
2.x tripal_core.tripal.api.inc | tripal_report_error($type, $severity, $message, $variables = array(), $options = array()) |
3.x tripal.notice.api.inc | tripal_report_error($type, $severity, $message, $variables = array(), $options = array()) |
Provide better error notice for Tripal.
If the environment variable 'TRIPAL_DEBUG' is set to 1 then this function will add backtrace information to the message.
Parameters
$type: The catagory to which this message belongs. Can be any string, but the general practice is to use the name of the module.
$severity: The severity of the message; one of the following values:
- TRIPAL_CRITICAL: Critical conditions.
- TRIPAL_ERROR: Error conditions.
- TRIPAL_WARNING: Warning conditions.
- TRIPAL_NOTICE: (default) Normal but significant conditions.
- TRIPAL_INFO: Informational messages.
- TRIPAL_DEBUG: Debug-level messages.
$message: The message to store in the log. Keep $message translatable by not concatenating dynamic values into it! Variables in the message should be added by using placeholder strings alongside the variables argument to declare the value of the placeholders. See t() for documentation on how $message and $variables interact.
$variables: Array of variables to replace in the message on display or NULL if message is already translated or not possible to translate.
$options: An array of options. Some available options include:
- print: prints the error message to the terminal screen. Useful when display is the command-line
Related topics
- chado_add_admin_form_set_title in legacy/
tripal_core/ api/ tripal_core.chado_nodes.title_and_path.inc - Generic "Set Node Title" sub-form for setting the title of any chado node
- chado_add_admin_form_set_url in legacy/
tripal_core/ api/ tripal_core.chado_nodes.title_and_path.inc - Generic "Set Node URL" sub-form for setting the url of any chado node
- chado_add_mview in tripal_chado/
api/ tripal_chado.mviews.api.inc - Add a materialized view to the chado database to help speed data access. This function supports the older style where postgres column specifications are provided using the $mv_table, $mv_specs and $indexed variables. It also supports the newer…
- chado_add_node_form_properties in legacy/
tripal_core/ api/ tripal_core.chado_nodes.properties.api.inc - chado_add_node_form_relationships in legacy/
tripal_core/ api/ tripal_core.chado_nodes.relationships.api.inc - Provides a form for adding to BASE_relationship and relationship tables
- tripal_core_report_error in legacy/
tripal_core/ api/ tripal_core.DEPRECATED.inc - @todo Remove. This was not in Tripal 1.x.
File
- tripal/
api/ tripal.notice.api.inc, line 64 - Provides an application programming interface (API) for improved user notifications. These API functions can be used to set messages for end-users, administrators, or simple logging.
Code
function tripal_report_error($type, $severity, $message, $variables = array(), $options = array()) {
$suppress = getenv('TRIPAL_SUPPRESS_ERRORS');
if (strtolower($suppress) === 'true') {
return;
}
// Get human-readable severity string
$severity_string = '';
switch ($severity) {
case TRIPAL_CRITICAL:
$severity_string = 'CRITICAL';
break;
case TRIPAL_ERROR:
$severity_string = 'ERROR';
break;
case TRIPAL_WARNING:
$severity_string = 'WARNING';
break;
case TRIPAL_NOTICE:
$severity_string = 'NOTICE';
break;
case TRIPAL_INFO:
$severity_string = 'INFO';
break;
case TRIPAL_DEBUG:
$severity_string = 'DEBUG';
break;
}
// If we are not set to return debugging information and the severity string
// is debug then don't report the error.
if (($severity == TRIPAL_DEBUG) AND (getenv('TRIPAL_DEBUG') != 1)) {
return FALSE;
}
// Get the backtrace and include in the error message, but only if the
// TRIPAL_DEBUG environment variable is set.
if (getenv('TRIPAL_DEBUG') == 1) {
$backtrace = debug_backtrace();
$message .= "\nBacktrace:\n";
$i = 1;
for ($i = 1; $i < count($backtrace); $i++) {
$function = $backtrace[$i];
$message .= " $i) " . $function['function'] . "\n";
}
}
// Send to watchdog.
try {
watchdog($type, $message, $variables, $severity);
}
catch (Exception $e) {
print "CRITICAL (TRIPAL): Unable to register error message with watchdog: " . $e->getMessage() . "\n.";
$options['print'] = TRUE;
}
// Format the message for printing (either to the screen, log or both).
if (sizeof($variables) > 0) {
$print_message = str_replace(array_keys($variables), $variables, $message);
}
else {
$print_message = $message;
}
// If print option supplied then print directly to the screen.
if (isset($options['print'])) {
print $severity_string . ' (' . strtoupper($type) . '): ' . $print_message . "\n";
}
// Print to the Tripal error log but only if the severity is not info.
if (($severity != TRIPAL_INFO)) {
tripal_log('[' . strtoupper($type) . '] ' . $print_message . "\n", $severity_string);
}
}