package WebGUI::Session::ErrorHandler; =head1 LEGAL ------------------------------------------------------------------- WebGUI is Copyright 2001-2006 Plain Black Corporation. ------------------------------------------------------------------- Please read the legal notices (docs/legal.txt) and the license (docs/license.txt) that came with this distribution before using this software. ------------------------------------------------------------------- http://www.plainblack.com info@plainblack.com ------------------------------------------------------------------- =cut use strict; use Log::Log4perl; use Apache2::RequestUtil; use JSON; =head1 NAME Package WebGUI::Session::ErrorHandler =head1 DESCRIPTION This package provides simple but effective error handling, debugging, and logging for WebGUI. =head1 SYNOPSIS use WebGUI::Session::ErrorHandler; my $errorHandler = WebGUI::ErrorHandler->new($session); $errorHandler->audit($message); $errorHandler->debug($message); $errorHandler->error($message); $errorHandler->fatal($message); $errorHandler->info($message); $errorHandler->security($message); $errorHandler->warn($message); $logger = $errorHandler->getLogger; $text = $errorHandler->getStackTrace; $html = $errorHandler->showDebug; =head1 METHODS These methods are available from this class: =cut #------------------------------------------------------------------- =head2 audit ( message ) A convenience function that wraps info() and includes the current username and user ID in addition to the message being logged. =head3 message Whatever message you wish to insert into the log. =cut sub audit { my $self = shift; my $message = shift; $self->info($self->session->user->username." (".$self->session->user->userId.") ".$message); } #------------------------------------------------------------------- =head2 canShowDebug ( ) Returns true if the user meets the condition to see debugging information and debug mode is enabled. =cut sub canShowDebug { my $self = shift; return 0 unless ($self->session->setting->get("showDebug")); return 0 unless (substr($self->session->http->getMimeType(),0,9) eq "text/html"); return 1 if ($self->session->setting->get("debugIp") eq ""); my $ips = $self->session->setting->get("debugIp"); $ips =~ s/\s+//g; my @ips = split(",", $ips); my $ok = WebGUI::Utility::isInSubnet($self->session->env->getIp, [ @ips] ); return $ok; } #------------------------------------------------------------------- =head2 canShowPerformanceIndicators ( ) Returns true if the user meets the conditions to see performance indicators and performance indicators are enabled. =cut sub canShowPerformanceIndicators { my $self = shift; my $mask = $self->session->setting->get("debugIp"); my $ip = $self->session->env->getIp; return ( ( $self->session->setting->get("showPerformanceIndicators") ) && ( $ip =~ /^$mask/ || $self->session->setting->get("debugIp") eq "" ) ); } #------------------------------------------------------------------- =head2 debug ( message ) Adds a DEBUG type message to the log. These events should be things that are only used for diagnostic purposes. =head3 message The message you wish to add to the log. =cut sub debug { my $self = shift; my $message = shift; $self->getLogger->debug($message); $self->{_debug_debug} .= $message."\n"; } #------------------------------------------------------------------- =head2 DESTROY ( ) Deconstructor. =cut sub DESTROY { my $self = shift; $Log::Log4perl::caller_depth--; undef $self; } #------------------------------------------------------------------- =head2 error ( message ) Adds a ERROR type message to the log. These events should be things that are errors that are not fatal. For instance, a non-compiling plug-in or erroneous user input. =head3 message The message you wish to add to the log. =cut sub error { my $self = shift; my $message = shift; $self->getLogger->error($message); $self->getLogger->debug("Stack trace for ERROR ".$message."\n".$self->getStackTrace()); $self->{_debug_error} .= $message."\n"; } #------------------------------------------------------------------- =head2 fatal ( message [, flags] ) Adds a FATAL type message to the log, outputs an error message to the user, and forces a close on the session. This should only be called if the system cannot recover from an error, or it would be unsafe to recover from an error like database connectivity problems. =head3 message The message to use. =cut sub fatal { my $self = shift; my $message = shift; $self->session->http->setStatus("500","Server Error"); Apache2::RequestUtil->request->content_type('text/html') if ($self->session->request); $self->getLogger->fatal($message); $self->getLogger->debug("Stack trace for FATAL ".$message."\n".$self->getStackTrace()); $self->session->http->sendHeader if ($self->session->request); if ($self->session->dbNotAvailable) { # We can't even _determine_ whether we can show the debug text. Punt. $self->session->output->print("
Something unexpected happened that caused this system to fault.
\n",1); $self->session->output->print("".$message."
\n",1); $self->session->output->print($self->getStackTrace(), 1); $self->session->output->print($self->showDebug(),1); } else { # NOTE: You can't internationalize this because with some types of errors that would cause an infinite loop. $self->session->output->print("