Handler.php 3.07 KB
<?php namespace App\Exceptions;

use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

use Config;
use Auth;
use Request;
use Mail;
use Cache;

class Handler extends ExceptionHandler {

	/**
	 * A list of the exception types that should not be reported.
	 *
	 * @var array
	 */
	protected $dontReport = [
		AuthorizationException::class,
		HttpException::class,
		ModelNotFoundException::class,
		ValidationException::class,
	];

	/**
	 * Report or log an exception.
	 *
	 * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
	 *
	 * @param  \Exception  $e
	 * @return void
	 */
	public function report(Exception $e)
	{
		if(!empty($e))
		{
		  $errorStr='';
		  if ($this->isHttpException($e) && $e->getStatusCode() == 404)$errorStr='MISSING';
		  else if(strstr(json_encode($e->getTrace(),true),"VerifyCsrfToken"))$errorStr='IGNORE';
		  else $errorStr='ERROR';
		  
		  $accesslog=Config::get('runtime.accesslog_obj');
		  if($accesslog)
		  {
		    $postdata=(array)json_decode($accesslog->postdata);
		    $postdata["ErrorType"]=$errorStr;
		    $postdata["ErrorMsg"]=$e->getMessage();
		    $accesslog->postdata=json_encode($postdata, true);
		    $accesslog->stopLog();
		  }
		
		  if($errorStr=='ERROR')
		  {
		    $email=Config::get("app.email");
		    $user="guest";if(Auth::check())$user=Auth::user()->id." ".Auth::user()->dispname()." ".Auth::user()->username;
		    $ip=Request::getClientIp();

				$key=$user.$ip.'ajaxerror'.Request::path();
				if(Cache::get($key,'')!=$e->getMessage().json_encode($e->getTrace(),true)&&Cache::get($key.'cnt',0)<10)
				{
						Cache::put($key,$e->getMessage().json_encode($e->getTrace(),true), 24*60);//minutes in 1 day
						Cache::put($key.'cnt',Cache::get($key.'cnt',0)+1, 24*60);
						
						Mail::send('emails.ajaxerror', array('xhr'=>"app-fatal",'status'=>$e->getMessage(),'error'=>json_encode($e->getTrace(),true),'user'=>$user,'ip'=>$ip), function($message) use ($email)
						{
							$message->to($email, $email)->subject(Config::get("app.name")." : Error");
						});
						
				}
		  }
		  else if($errorStr=='MISSING'){}
		  else if($errorStr=='IGNORE'){}
		}
	
		return parent::report($e);
	}

	/**
	 * Render an exception into an HTTP response.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  \Exception  $e
	 * @return \Illuminate\Http\Response
	 */
	public function render($request, Exception $e)
	{
		if(!empty($e))
		{
		  $errorStr='';
		  if ($this->isHttpException($e) && $e->getStatusCode() == 404)$errorStr='MISSING';
		  else $errorStr='ERROR';
		  
		  if($errorStr=='ERROR')
		  {
				if(!Config::get('app.debug')) return response()->view("errors.exception");
		  }
		  else if($errorStr=='MISSING')
		  {
		    return response()->view('errors.missing', array());
		  }
		}
	
		return parent::render($request, $e);
	}

}